// 静态方法

import HelperUtil from '../../utils/HelperUtil'
import {ALARM_FILTER_KEY} from '../../utils/constantCollection'
import AlarmFilterConfigAPI from '../../api/restApis/AlarmFilterConfigAPI'
import DeviceStateSocketApi from '../../api/websocketApis/DeviceStateSocketApi'

let staticMethods = {
  /**
   * @Description  : 分页查询
   * @author       : ls
   * @date         : 2020/7/23 9:52
   * @param        :
   * @return       :
   */
  queryAlarmFilterDataSplitNumber: function (beginItem, endItem, selectList) {
    return AlarmFilterConfigAPI.putAlarmFilterQueryterms(beginItem, endItem, selectList).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, ALARM_FILTER_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 查询总数
   * @author       : ls
   * @date         : 2020/7/23 9:52
   * @param        :
   * @return       :
   */
  queryAlarmFilterTotalNumber: function (selectList) {
    return AlarmFilterConfigAPI.putAlarmFilterCounts(selectList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 获取告警过滤列表 cxf/nms/alarm/alarmfilterconfig
  getAllAlarmFilterList: function () {
    return AlarmFilterConfigAPI.getAllAlarmFilterList().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, ALARM_FILTER_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 改
  modifyAlarmFilter: function (alarmfilter) {
    return AlarmFilterConfigAPI.modifyAlarmFilter(alarmfilter).then(result => {
      if (result.data.result) {

      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 增
  addAlarmFilter: function (alarmfilter) {
    return AlarmFilterConfigAPI.addAlarmFilter(alarmfilter).then(result => {
      if (result.data.result) {

      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 删
  deleteAlarmFilter: function (alarmfilter) {
    return AlarmFilterConfigAPI.deleteAlarmFilter(alarmfilter).then(result => {
      if (result.data.result) {

      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  }

}

function AlarmFilterConfigService (websocketService = null) {
  let listener = 'AlarmFilter'
  // 添加过滤配置
  function subscribeAddAlarmFilter (mutator) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.ADD_ALARM_FILTER, listener, (returnObject) => {
      mutator()
    })
  }
  function unsubscribeAddAlarmFilter () {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.ADD_ALARM_FILTER, listener)
  }

  // 删除过滤配置
  function subscribeDeleteAlarmFilter (mutator) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.DELETE_ALARM_FILTER, listener, (returnObject) => {
      mutator()
    })
  }
  function unsubscribeDeleteAlarmFilter () {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.DELETE_ALARM_FILTER, listener)
  }

  // 修改过滤配置
  function subscribeModifyAlarmFilter (mutator) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.MODIFY_ALARM_FILTER, listener, (returnObject) => {
      mutator()
    })
  }
  function unsubscribeModifyAlarmFilter () {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.MODIFY_ALARM_FILTER, listener)
  }
  return Object.freeze(Object.assign(
    {
      // object methods comes here
      subscribeAddAlarmFilter,
      unsubscribeAddAlarmFilter,
      subscribeDeleteAlarmFilter,
      unsubscribeDeleteAlarmFilter,
      subscribeModifyAlarmFilter,
      unsubscribeModifyAlarmFilter
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(AlarmFilterConfigService, staticMethods)
export default AlarmFilterConfigService
