'use strict';

const EventEmitter = require('events')
const ToggleElement = require('./ToggleElement')
const Alert = require('./Alert')
const util = require('./util')

const icons = {
  default: 'fa fa-info',
  warning: 'fa fa-warning',
  danger: 'fa fa-exclamation',
  success: 'fa fa-check',
  error: 'fa fa-code',
  progress: 'fa fa-circle-o-notch fa-spin'
}

class AlertManager extends EventEmitter {
  constructor(x, parent) {
    super()
    this._EGTYPE = ['alertmanager']
    this.max = Math.max(1, x || 0)
    this.alerts = []
    this.container = new ToggleElement(util.div('gui-alert-container'))
    this.addContainer(parent)
  }

  setMax(x) {
    if (x > 0) {
      this.max = x
      this._check()
    }
  }

  addContainer(parent) {
    this.container.appendTo(parent || document.getElementsByTagName('BODY')[0])
  }

  removeContainer() {
    this.container.remove()
  }

  add(body, status, sticky) {
    let alert
    let timeout = null
    if (Alert.is(body)) {
      alert = body
    } else {
      if (status === 'success') timeout = 3000
      if (status === 'default') timeout = 5000
      alert = new Alert({
        body: body,
        status: status || 'default',
        icon: icons[status || 'default'],
        sticky: (status === 'progress') || sticky,
        timeout: timeout
      })
    }

    this.alerts.push(alert)
    alert.on('remove', () => {
      this.alerts.splice(this.alerts.indexOf(alert), 1)
      this._check()
    })

    alert.appendTo(this.container)
    this._check()
    return alert
  }

  _check() {
    for (let i = this.alerts.length - 1; i >= 0; i--) {
      if (this.alerts.length - i <= this.max) this.alerts[i].show()
      else this.alerts[i].hide()
    }
  }

  clean() {
    this.alerts.map( (al) => {
      al.remove()
    })
  }

  static is(x) {
    if (x && x._EGTYPE && Array.isArray(x._EGTYPE) && x._EGTYPE.includes('alertmanager')) return true
    return false
  }



}



module.exports = AlertManager
