// Alarm模型对象示例。
let AlarmStatus = {
  ALARM: 0,
  CONFIRMED: 1,
  CLEARED: 2,
  RECOVERED: 3
}

var alarmSample = {
  alarmKey: '', // 主键，字符串，唯一
  nodeType: -1, // 节点类型
  deviceType: '', // 设备类型,字符串
  deviceKey: '', // 设备主键，字符串
  slotKey: '', // 板卡主键，板卡ID，字符串
  portName: '', // 端口名称，字符串
  alarmLevel: -1, // 告警等级
  alarmCode: '', // 告警码
  cause: '', // 告警原因，字符串
  alarmCount: -1, // 频次
  alarmStatus: -1, // 告警状态：告警、已确认、未确认、已恢复
  firstTime: '', // 首次发生时间，字符串
  curTime: '', // 最近发生时间
  recovertime: '', // 恢复时间，字符串
  clearTime: '', // 清除时间，字符串
  confirmTime: '', // 确认时间，字符串
  confirmUser: '', // 确认用户，字符串
  note: '', // 其他信息，字符串
  deviceName: '' // 网元名称，字符串
}

class AlarmManagerImplement {
  // 根据告警等级，获取所有告警中最高等级，从告警配置中读取音频地址，并返回
  getCurAudioSrc (alarmCollection, alarmLevelConfig) {
    let alarmMaxLevel = -1
    // let alarmKey = ''
    this.alarmsAsArray(alarmCollection).forEach(function (currentAlarm) {
      if (currentAlarm.alarmLevel > alarmMaxLevel) {
        alarmMaxLevel = currentAlarm.alarmLevel
      }
    })
    var curAudioSrc = alarmLevelConfig[alarmMaxLevel].alarmAudioPath
    return curAudioSrc
  }

  // 根据key值查找alarm
  findAlarmByKey (alarmCollection, alarmKey) {
    return alarmCollection[alarmKey]
  }

  findAlarmBySubnet(alarmCollection,deviceList){
    let alarmLevelList = [];
    alarmCollection = this.alarmsAsArray(alarmCollection);
    deviceList = this.devicesAsArray(deviceList);
    for(let j = 0; j < alarmCollection.length; j++){
      for(let i = 0; i < deviceList.length; i++){
        if(alarmCollection[j].elementKey === deviceList[i].deviceKey){
          alarmLevelList.push(alarmCollection[j])
        }
      }
    }
    return alarmLevelList;
  }

  findAlarmByElement(alarmCollection,device){
    let alarmLevelList = [];
    alarmCollection = this.alarmsAsArray(alarmCollection);
    for(let j = 0; j < alarmCollection.length; j++){
        if(alarmCollection[j].elementKey === device){
          alarmLevelList.push(alarmCollection[j])
      }
    }
    return alarmLevelList;
  }

  // 查找对应status的告警
  getAlarmListByStatus (alarmCollection, status) {
    return this.alarmsAsArray(alarmCollection).filter(alarm => {
      return alarm.alarmStatus === status
    })
  }


  getNumberByAlarmLevel(subnetAlarmList) {
    if(subnetAlarmList===null||subnetAlarmList===undefined){
      return 0
    }
    let alarmLevelList = [];
    let urgentAlarm = 0;
    let seriousAlarm = 0;
    let waringAlarm = 0;
    let total = subnetAlarmList.length;
    for (let i = 0; i < total; i++) {
      if (subnetAlarmList[i].alarmLevel === 2) {
        urgentAlarm += 1
      }else if(subnetAlarmList[i].alarmLevel === 1){
        seriousAlarm += 1
      }else if(subnetAlarmList[i].alarmLevel === 0){
        waringAlarm += 1
      }
    }
    alarmLevelList[0]=waringAlarm;
    alarmLevelList[1]=seriousAlarm;
    alarmLevelList[2]=urgentAlarm;
    alarmLevelList[3]=total;
    return alarmLevelList
  }

  // 获取未确认告警，返回list
  getUnConfirmedAlarmList (alarmCollection) {
    return this.getAlarmListByStatus(alarmCollection, AlarmStatus.ALARM)
  }

  // 获取已确认告警，返回List
  getConfirmedAlarmList (alarmCollection) {
    return this.getAlarmListByStatus(alarmCollection, AlarmStatus.CONFIRMED)
  }

  // 获取已清除告警，返回List
  getClearedAlarmList (alarmCollection) {
    return this.getAlarmListByStatus(alarmCollection, AlarmStatus.CLEARED)
  }

  // 获取已恢复告警，返回List
  getRecoveredAlarmList (alarmCollection) {
    return this.getAlarmListByStatus(alarmCollection, AlarmStatus.RECOVERED)
  }

  // 获取以上四种告警
  getAlarmListsGroupByStatus (alarmCollection) {
    return {
      unconfirmedAlarmList: this.getUnConfirmedAlarmList(alarmCollection),
      confirmedAlarmList: this.getConfirmedAlarmList(alarmCollection),
      clearedAlarmList: this.getClearedAlarmList(alarmCollection),
      recoveredAlarmList: this.getRecoveredAlarmList(alarmCollection)
    }
  }

  // 根据alarmCollection和postStatusObject,gei deviceList添加status和alarmLevel
  /**
   * 1.添加status
   *   deviceList中的portName与postStatus的属性名对应的，该postName赋上对应的值
   * 2.添加alarmLevel
   *   根据deviceKey找到含有这个key的alarm，并将此alarm对应的portName的devicePortList的alarmLevel赋值此alarm的alarmLevel值
   *   devicePortList其余的alarmLevel赋值-1
   * @param alarmCollection
   * @param devicePortList
   * @param deviceKey
   * @param postStatusObject
   * @returns {*}
   */
  getDeviceListByAlarmCollection (alarmCollection, devicePortList, deviceKey, postStatusObject) {
    // 添加Status
    if (JSON.stringify(postStatusObject) === '{}') {
      for (var m = 0; m < devicePortList.length; m++) {
        devicePortList[m].status = 0
      }
    } else {
      for (var j = 0; j < postStatusObject.length; j++) {
        for (var i = 0; i < devicePortList.length; i++) {
          if ('eth-0-'.concat(devicePortList[i].resourceName) === postStatusObject[j].portName) {
            devicePortList[i].status = postStatusObject[j].value
          }
        }
      }
    }

    // 添加alarmLevel
    for (var alarmKey in alarmCollection) {
      let alarm = this.findAlarmByKey(alarmCollection, alarmKey)
      if (alarm.elementKey === deviceKey) {
        for (var j = 0; j < devicePortList.length; j++) {
          if (devicePortList[j].resourceName === alarm.resourceName && alarm.alarmStatus !== 3) {
            if (typeof (devicePortList[j].alarmLevel) === 'undefined') {
              devicePortList[j].alarmLevel = alarm.alarmLevel
            } else {
              if (alarm.alarmLevel > devicePortList[j].alarmLevel) {
                devicePortList[j].alarmLevel = alarm.alarmLevel
              }
            }
          }
        }
      }
    }

    for (var k = 0; k < devicePortList.length; k++) {
      if (typeof (devicePortList[k].alarmLevel) === 'undefined') {
        devicePortList[k].alarmLevel = -1
      }
    }
    return devicePortList
  }

  alarmsAsArray (alarmCollection) {
    if (alarmCollection){

      return Object.values(alarmCollection)
    } else {return []}
  }

  devicesAsArray (deviceList) {
    if (deviceList){

      return Object.values(deviceList)
    } else {return []}
  }

}
let implementMethods = new AlarmManagerImplement()

// 静态模型方法（模型类方法）。这些方法不需要创建模型对象即可调用。
var staticMethods = {
  createSampleAlarm: function () {
    return Object.freeze(Object.assign({}, alarmSample))
  }
}

// 模型对象构造函数，管理Alarm POJO集合。使用Factory Function模式实现。
function AlarmManager (alarmCollection, contextState = {}) {
  function getCurAudioSrc (alarmLevelConfig) {
    return implementMethods.getCurAudioSrc(alarmCollection, alarmLevelConfig)
  }

  function findAlarmByKey (alarmKey) {
    return implementMethods.findAlarmByKey(alarmCollection, alarmKey)
  }

  // 查找未确认告警
  function getUnConfirmedAlarmList () {
    return implementMethods.getUnConfirmedAlarmList(alarmCollection)
  }

  // 查找已确认告警
  function getConfirmedAlarmList () {
    return implementMethods.getConfirmedAlarmList(alarmCollection)
  }

  // 查找已清除告警
  function getClearedAlarmList () {
    return implementMethods.getClearedAlarmList(alarmCollection)
  }

  // 查找配置差异信息
  function getConfigDifferencesList () {
    return 0
  }

  // 查找已恢复告警
  function getAlarmListsGroupByStatus () {
    return implementMethods.getAlarmListsGroupByStatus(alarmCollection)
  }

  // 根据alarmCollection和postStatusObject,gei deviceList添加status和alarmLevel
  function getDeviceListByAlarmCollection (devicePortList, deviceKey, portStatusObject) {
    return implementMethods.getDeviceListByAlarmCollection(alarmCollection, devicePortList, deviceKey, portStatusObject)
  }

  function asArray () {
    return implementMethods.alarmsAsArray()
  }

  function findAlarmByKey() {
    return implementMethods.findAlarmByKey()
  }

  function getNumberByAlarmLevel(subnetAlarmList) {
    return implementMethods.getNumberByAlarmLevel(subnetAlarmList)
  }

  function findAlarmBySubnet(deviceList) {
    return implementMethods.findAlarmBySubnet(alarmCollection,deviceList)
  }

  function findAlarmByElement(device) {
    return implementMethods.findAlarmByElement(alarmCollection,device)
  }

  return Object.freeze(Object.assign(
    {
      // 在此列出模型对象方法
      getCurAudioSrc,
      findAlarmByKey,
      getUnConfirmedAlarmList,
      getConfirmedAlarmList,
      getClearedAlarmList,
      getConfigDifferencesList,
      getAlarmListsGroupByStatus,
      getDeviceListByAlarmCollection,
      asArray,
      getNumberByAlarmLevel,
      findAlarmBySubnet,
      findAlarmByElement,

    },
    staticMethods // 将静态模型方法注入到模型对象方法中
  ))
}

// Object.assign(DeviceManager, implementMethods)
// 将静态模型方法注入模型对象构造函数中
Object.assign(AlarmManager, staticMethods)

// Object.assign(AlarmManager, implementMethods)

AlarmManager.AlarmStatus = Object.freeze(AlarmStatus)

// 导出模型对象构造函数
export default AlarmManager
