import AlarmLevelConfigAPI from '../../api/restApis/AlarmLevelConfigAPI'
import HelperUtil from '../../utils/HelperUtil'
import {ALARM_AUDIO} from "../../utils/RequestUrls";
import DeviceStateSocketApi from "../../api/websocketApis/DeviceStateSocketApi";
import WebSocketService from "./WebSocketService";
import {
  DISABLED_STATUS, ENABLED_STATUS, DISCOVER_STATUS, STATIC_URL,
  TOPOLOGY_IMAGE_TYPE, SIDEBAR_IMAGE_TYPE, CONTROLLER_IMAGE_TYPE,
  SERVER_IMAGE_TYPE, ALARM_FILTER_KEY, ALARM_LEVEL
} from "../../utils/constantCollection";

let helperMethods = {
  // 将读取过来的告警配置放到告警配置对象中
  addAlarmLevelConfigToObject: function (status,alarmLevelConfig,returnObj) {
    status = status+'';
    let newObj = {};
    newObj["status"] = status;
    newObj["alarmColor"] = alarmLevelConfig.alarmColor;
    newObj["topologyDeviceAlarmImagePath"] = STATIC_URL + alarmLevelConfig.alarmColor.substring(1) + TOPOLOGY_IMAGE_TYPE;
    newObj["sidebarDeviceAlarmImagePath"] = STATIC_URL + alarmLevelConfig.alarmColor.substring(1) + SIDEBAR_IMAGE_TYPE;
    newObj["controllerAlarmImagePath"] = STATIC_URL + alarmLevelConfig.alarmColor.substring(1) + CONTROLLER_IMAGE_TYPE;
    newObj["othersServerAlarmImagePath"] = STATIC_URL + alarmLevelConfig.alarmColor.substring(1) + SERVER_IMAGE_TYPE;
    newObj["alarmAudioPath"] = ALARM_AUDIO + alarmLevelConfig.alarmVoice;
    returnObj[status] = newObj;
  },
};

// 静态方法
let staticMethods = {

  // 获取告警配置
  getAlarmLevelConfig: function () {
    return AlarmLevelConfigAPI.getAlarmLevelConfig().then(result => {
      if (result.data.result) {
        let alarmLevelConfigList = result.data.returnValue;
        let returnObj = {};
        returnObj['-2'] = DISABLED_STATUS;
        returnObj['-1'] = ENABLED_STATUS;
        returnObj['3'] = DISCOVER_STATUS;
        for(let i = 0;i<alarmLevelConfigList.length;i++){
          switch(alarmLevelConfigList[i].alarmLevel){
            case 0: // 警告
              helperMethods.addAlarmLevelConfigToObject(0,alarmLevelConfigList[i],returnObj);
              break;
            case 1: // 严重
              helperMethods.addAlarmLevelConfigToObject(1,alarmLevelConfigList[i],returnObj);
              break;
            case 2: // 紧急
              helperMethods.addAlarmLevelConfigToObject(2,alarmLevelConfigList[i],returnObj);
              break;
            default:
          }
        }
        return returnObj;
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  //获取最常规的数据规格（大对象里包含若干个以KEY为名的小对象）
  getNormalAlarmLevelConfig:function (){
    return AlarmLevelConfigAPI.getAlarmLevelConfig().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,ALARM_LEVEL);
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  }
};

function AlarmLevelConfigService (websocketService = null) {
  // 修改告警等级配置
  function subscribeModifyAlarmLevelConfig (alarmLevelConfig,mutator = (alarmLevelConfig) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.MODIFY_ALARM_LEVEL_CONFIG, alarmLevelConfig, (returnObject) => {
      returnObject.forEach(result=>{
        switch(result.alarmLevel){
          case 0: // 警告
            helperMethods.addAlarmLevelConfigToObject(0,result,alarmLevelConfig);
            break;
          case 1: // 严重
            helperMethods.addAlarmLevelConfigToObject(1,result,alarmLevelConfig);
            break;
          case 2: // 紧急
            helperMethods.addAlarmLevelConfigToObject(2,result,alarmLevelConfig);
            break;
          default:
        }
      });
      mutator(alarmLevelConfig);
    });
  }
  function unsubscribeModifyAlarmLevelConfig (alarmLevelConfig) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.MODIFY_ALARM_LEVEL_CONFIG, alarmLevelConfig);
  }
  return Object.freeze(Object.assign(
    {
      // object methods comes here
      subscribeModifyAlarmLevelConfig,
      unsubscribeModifyAlarmLevelConfig
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(AlarmLevelConfigService, staticMethods);
export default AlarmLevelConfigService
