import DeviceService from './DeviceService'
import SubnetService from './SubnetService'
import AlarmService from './AlarmService'
import LinkService from './LinkService'
import TopologyService from './TopologyService'
import HelperUtil from '../../utils/HelperUtil'
import Vue from 'vue'
import {
  DEVICE_DISABLED_STATUS_ENUM, ENABLED_STATUS_ENUM, DISABLED_STATUS_ENUM,
  DEVICE_ENABLED_STATUS_ENUM, DISCOVER_STATUS_ENUM, DEVICE_DISCOVER_STATUS_ENUM, RESPONSE_FAIL, TOPOLOGY_UPDATE_FLAG,
  USER_NAME
} from '../../utils/constantCollection'
import AlarmLevelConfigService from './AlarmLevelConfigService'
import ElineService from './Business/ElineService'
import ServerService from './ServerService'
import TopologyManager from '../models/TopologyManager'
import AlarmManager from '../models/AlarmManager'
import DeviceStateSocketApi from '../../api/websocketApis/DeviceStateSocketApi'
import AlarmFilterConfigService from './AlarmFilterConfigService'
import UserService from './UserService'
import WebsocketService from './WebSocketService'
import ConfigFileDifferenceService from './ConfigFileDifferenceService'
import ElementDiscoverService from './ElementDiscoverService'

var helperMethods = {

  // 有关用户Service的websocket操作
  userToLoginPage: function (returnObject, before = () => {}) {
    for (let i = 0; i < returnObject.length; i++) {
      if (returnObject[i] === window.sessionStorage.getItem(USER_NAME)) {
        window.sessionStorage.clear()
        window.localStorage.clear()
        before()
        break
      }
    }
  },


  // 给设备添加状态
  addDeviceAndTopologyStatus: function (devices, topologys, alarms) {
    for (var deviceKey in devices) {
      if (devices[deviceKey].deviceStatus === DEVICE_DISABLED_STATUS_ENUM) {
        Vue.set(devices[deviceKey], 'status', DISABLED_STATUS_ENUM)
        Vue.set(topologys[deviceKey], 'status', DISABLED_STATUS_ENUM)
      } else if (devices[deviceKey].deviceStatus === DEVICE_ENABLED_STATUS_ENUM) {
        Vue.set(devices[deviceKey], 'status', ENABLED_STATUS_ENUM)
        Vue.set(topologys[deviceKey], 'status', ENABLED_STATUS_ENUM)
      } else if (devices[deviceKey].deviceStatus === DEVICE_DISCOVER_STATUS_ENUM) {
        Vue.set(devices[deviceKey], 'status', DISCOVER_STATUS_ENUM)
        Vue.set(topologys[deviceKey], 'status', DISCOVER_STATUS_ENUM)
      }
    }
  },

  // 给服务器添加状态
  addServerStatus: function (servers) {
    for (let serverKey in servers) {
      Vue.set(servers[serverKey], 'status', ENABLED_STATUS_ENUM)
    }
  },

  // 给链路添加状态
  addLinkStatus: function (links) {
    for (let linkKey in links) {
      switch (links[linkKey].linkStatus) {
        case 0:
          Vue.set(links[linkKey], 'status', ENABLED_STATUS_ENUM)
          break
        case 1:
          Vue.set(links[linkKey], 'status', DISCOVER_STATUS_ENUM)
          break
        default:
      }
    }
  },

  // 给subnet 对象 添加 deviceList(该子网所含设备集合)
  addDeviceListToSubnet: function (devices, subnets) {
    for (var subnetKey in subnets) {
      Vue.set(subnets[subnetKey], 'deviceList', {})
    }
    for (var deviceKey in devices) {
      Vue.set(subnets[devices[deviceKey].subnetKey].deviceList, deviceKey, devices[deviceKey])
    }
  },

  // 给server添加deviceNum字段
  addDeviceNumToServer: function (servers, deviceNum) {
    if (deviceNum === undefined || deviceNum === null) {
      return
    }
    for (let serverKey in deviceNum) {
      Vue.set(servers[serverKey], 'deviceNum', deviceNum[serverKey].deviceNum)
    }
  },

  // 给link 添加自定义属性
  addAttributeToLink: function (links) {
    for (let linkKey in links) {
      Vue.set(links[linkKey], 'bandwidthUsed', '0')
      Vue.set(links[linkKey], 'bandwidthMax', '0')
    }
  },

  manageDefaultCoordinate: function (topologyCollection) {
    let coordinateX = 70
    let coordinateY = 70
    let column = 50
    let i = 0
    let j = 0
    for (let topologyKey in topologyCollection) {
      let topology = topologyCollection[topologyKey]
      if (topology.verticalAxis === 0 && topology.horizontalAxis === 0) {
        topology.horizontalAxis = coordinateX * i
        topology.verticalAxis = coordinateY * j
        i++
        if (i === column) {
          j++
          i = 0
        }
      }
      if (topology.nodeType === 1) {
        this.manageDefaultCoordinate(topology.deviceList)
      }
    }
  }
}

var staticMethods = {
  // 获取设备状态 --- 传统模式
  getAllDeviceState_TRAD: function () {
    let time = new Date()
    console.log('===========首页============' + time.getMinutes() + 'm' + time.getSeconds() + 's' + time.getMilliseconds() + 'ms')
    return Promise.all([
      DeviceService.getAllDeviceCollection(),
      SubnetService.getAllSubnetCollection(),
      AlarmService.getAllAlarmCollection(),
      LinkService.getAllLinkCollection(),
      TopologyService.getAllTopologyCollection(),
      AlarmLevelConfigService.getAlarmLevelConfig(),
      ConfigFileDifferenceService.getConfigFileDifferenceCollection(),
      // ElementDiscoverService.getSpeedAndProgressMessage(),
      // DiscoverElementService.getConfigFileDifferenceCollection(),
      // ServerService.getAllServerCollection()
    ]).then((post) => {
      let time = new Date()
      console.log('-----------------首页------------------' + time.getMinutes() + 'm' + time.getSeconds() + 's' + time.getMilliseconds() + 'ms')
      var resultObj = {}
      Vue.set(resultObj, 'deviceCollection', post[0])
      Vue.set(resultObj, 'subnetCollection', post[1])
      Vue.set(resultObj, 'alarmCollection', post[2])
      Vue.set(resultObj, 'linkCollection', post[3])
      Vue.set(resultObj, 'topologyCollection', post[4])
      Vue.set(resultObj, 'alarmLevelConfig', post[5])
      Vue.set(resultObj, 'configFileDifferenceCollection', post[6])
      // Vue.set(resultObj, 'elementDiscoverMessage', post[7])
	    // 给 device 设置 status 给 topology 设置 status 给 server 设置 status
      helperMethods.addDeviceAndTopologyStatus(resultObj.deviceCollection, resultObj.topologyCollection, resultObj.alarmCollection)
      // helperMethods.addServerStatus(resultObj.serverCollection);
      helperMethods.addLinkStatus(resultObj.linkCollection)
      helperMethods.addAttributeToLink(resultObj.linkCollection)
      // 给 link 设置 status
      for (var linkKey in resultObj.linkCollection) {
        LinkService.updateLinkStatusByAlarmCause(resultObj.linkCollection, resultObj.deviceCollection, resultObj.alarmCollection, resultObj.linkCollection[linkKey])
      }
      // 给subnet 对象 添加 deviceList(该子网所含设备集合)
      helperMethods.addDeviceListToSubnet(resultObj.deviceCollection, resultObj.subnetCollection)

      // topologyCollection 包含子网与设备，须将子网元素和设备处理为分级
      let topology = {}
      for (let elementKey in resultObj.topologyCollection) {
        if (resultObj.topologyCollection[elementKey].nodeType === 1) {
          resultObj.topologyCollection[elementKey].deviceList = {}
          Vue.set(topology,elementKey,resultObj.topologyCollection[elementKey])
        }
      }
      for (let elementKey in resultObj.topologyCollection) {
        if (resultObj.topologyCollection[elementKey].nodeType === 0) {
          let subnetKey = resultObj.topologyCollection[elementKey].belongSubnet
          for (let it in topology) {
            if (subnetKey === it) {
              Vue.set(topology[it].deviceList,elementKey,resultObj.topologyCollection[elementKey])
              break
            }
          }
        }
      }
      resultObj.topologyCollection = topology
      helperMethods.manageDefaultCoordinate(resultObj.topologyCollection)
      AlarmService.updateDeviceAndTopologyAndServerStatus(resultObj.alarmCollection, resultObj.deviceCollection, resultObj.topologyCollection,resultObj.subnetCollection)
      return resultObj
    })
  },
  // 获取设备状态 --- SDNC模式
  getAllDeviceState_SDNC: function () {
    return Promise.all([
      DeviceService.getAllDeviceCollection(),
      SubnetService.getAllSubnetCollection(),
      AlarmService.getAllAlarmCollection(),
      LinkService.getAllLinkCollection(),
      TopologyService.getAllTopologyCollection(),
      AlarmLevelConfigService.getAlarmLevelConfig(),
      ServerService.getAllServerCollection(),
      ElineService.getAllElineCollection(),
      ServerService.getDeviceNumUnderServer(),
    ]).then((post) => {
      var resultObj = {}
      Vue.set(resultObj, 'deviceCollection', post[0])
      Vue.set(resultObj, 'subnetCollection', post[1])
      Vue.set(resultObj, 'alarmCollection', post[2])
      Vue.set(resultObj, 'linkCollection', post[3])
      Vue.set(resultObj, 'topologyCollection', post[4])
      Vue.set(resultObj, 'alarmLevelConfig', post[5])
      Vue.set(resultObj, 'serverCollection', post[6])
      Vue.set(resultObj, 'elineCollection', post[7])
      // 给server添加deviceNum字段
      helperMethods.addDeviceNumToServer(resultObj.serverCollection, post[8])
      // topologyCollection 包含子网与设备，须将子网元素剔除
      for (let topologyKey in resultObj.topologyCollection) {
        if (resultObj.topologyCollection[topologyKey].nodeType === 1) {
          Vue.delete(resultObj.topologyCollection, topologyKey)
        }
      }
      // 处理网元位置为（0，0）的默认位置，按照一行排开
      let topologyMgr = TopologyManager(resultObj.topologyCollection)
      topologyMgr.manageDefaultCoordinate()
      // 给 device 设置 status 给 topology 设置 status 给 server 设置 status
      helperMethods.addDeviceAndTopologyStatus(resultObj.deviceCollection, resultObj.topologyCollection)
      helperMethods.addServerStatus(resultObj.serverCollection)
      helperMethods.addLinkStatus(resultObj.linkCollection)
      helperMethods.addAttributeToLink(resultObj.linkCollection)
      AlarmService.updateDeviceAndTopologyAndServerStatus(resultObj.alarmCollection, resultObj.deviceCollection, resultObj.topologyCollection, resultObj.serverCollection)
      // 给 link 设置 status
      for (var linkKey in resultObj.linkCollection) {
        LinkService.updateLinkStatusByAlarmCause(resultObj.linkCollection, resultObj.deviceCollection, resultObj.alarmCollection, resultObj.linkCollection[linkKey])
      }
      // 给subnet 对象 添加 deviceList(该子网所含设备集合)
      helperMethods.addDeviceListToSubnet(resultObj.deviceCollection, resultObj.subnetCollection)
      ElineService.clearFinishedService(resultObj.elineCollection)
      return resultObj
    })
  },
  // 获取websocket请求路径
  getWebSocketRequestURL: function () {
    return HelperUtil.getWebSocketRequestURL()
  },

  // 同步操作sdnc
  syncDeviceState_SDNC: function (_this) {
    // 将data数据初始化
    return DeviceStateService.getAllDeviceState_SDNC().then((result) => {
      _this.deviceCollection = result.deviceCollection
      _this.subnetCollection = result.subnetCollection
      _this.alarmCollection = result.alarmCollection
      _this.linkCollection = result.linkCollection
      _this.topologyCollection = result.topologyCollection
      _this.alarmLevelConfig = result.alarmLevelConfig
      _this.serverCollection = result.serverCollection
      _this.elineCollection = result.elineCollection
      // 将 alarmLevelConfig 存储到 Vuex 中
      _this.$store.commit('setAlarmLevelConfig', _this.alarmLevelConfig)

      let alarmManager = AlarmManager(_this.alarmCollection)
      _this.curAudioSrc = alarmManager.getCurAudioSrc(_this.alarmLevelConfig)
    })
  },
  // 同步操作trad
  syncDeviceState_TRAD: function (_this) {
    // 将data数据初始化
    return DeviceStateService.getAllDeviceState_TRAD().then((result) => {
      _this.deviceCollection = result.deviceCollection
      _this.subnetCollection = result.subnetCollection
      _this.alarmCollection = result.alarmCollection
      _this.linkCollection = result.linkCollection
      _this.topologyCollection = result.topologyCollection
      _this.alarmLevelConfig = result.alarmLevelConfig
      _this.configFileDifferenceCollection = result.configFileDifferenceCollection
      _this.discoveryCollection = result.discoveryCollection

      // _this.serverCollection = result.serverCollection;

      // _this.deviceCollection = result.deviceCollection;
      // _this.subnetCollection = result.subnetCollection;
      // _this.alarmCollection = result.alarmCollection;
      // _this.linkCollection = result.linkCollection;
      // _this.topologyCollection = result.topologyCollection;
      // _this.alarmLevelConfig = result.alarmLevelConfig;
      // _this.serverCollection = result.serverCollection;
      // 将 alarmLevelConfig 存储到 Vuex 中
      _this.$store.commit('setAlarmLevelConfig', _this.alarmLevelConfig)

      let alarmManager = AlarmManager(_this.alarmCollection)
      _this.curAudioSrc = alarmManager.getCurAudioSrc(_this.alarmLevelConfig)
    })
  },

  // 注册 ws 事件
  registerWebSocketEvent: function (_this, mutator = (_this) => {}) {
    let deviceWebsocket = DeviceService(_this.webSocket)
    let alarmWebsocket = AlarmService(_this.webSocket)
    let alarmLevelConfigWebsocket = AlarmLevelConfigService(_this.webSocket)
    let subnetWebsocket = SubnetService(_this.webSocket)
    let linkWebsocket = LinkService(_this.webSocket)
    let alarmFilterWebsocket = AlarmFilterConfigService(_this.webSocket)
    let UserServiceWebsocket = UserService(_this.webSocket)
    let serverWebsocket = ServerService(_this.webSocket)
    let configFileDifferenceWebsocket = ConfigFileDifferenceService(_this.webSocket)
    let elementDiscoverWebsocket = ElementDiscoverService(_this.webSocket)

    mutator(_this)

    serverWebsocket.subscribeAddServer(_this.serverCollection, (servers) => {
      _this.serverCollection = servers
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    serverWebsocket.subscribeModifyServer(_this.serverCollection, (servers) => {
      _this.serverCollection = servers
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    serverWebsocket.subscribeDeleteServer(_this.serverCollection, (servers) => {
      _this.serverCollection = servers
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })

    deviceWebsocket.subscribeAddDevice(_this.deviceCollection, _this.addDevice, _this.subnetCollection, _this.topologyCollection, _this.alarmCollection, (devices, addDevice, subnets, topologys) => {
      _this.deviceCollection = devices
      _this.addDevice = addDevice
      _this.subnetCollection = subnets
      _this.topologyCollection = topologys
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    deviceWebsocket.subscribeDeleteDevice(_this.deviceCollection, _this.deleteDevice, _this.subnetCollection, _this.linkCollection, _this.topologyCollection, (devices, deleteDevice, subnets, links, topologys) => {
      _this.deviceCollection = devices
      _this.deleteDevice = deleteDevice
      _this.subnetCollection = subnets
      _this.linkCollection = links
      _this.topologyCollection = topologys
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    deviceWebsocket.subscribeModifyDevice(_this.deviceCollection, _this.modifyDevice, _this.subnetCollection, _this.topologyCollection, _this.alarmCollection, _this.linkCollection, (devices, modifyDevice, subnets, topologys, alarms, links) => {
      _this.deviceCollection = devices
      _this.modifyDevice = modifyDevice
      _this.subnetCollection = subnets
      _this.topologyCollection = topologys
      _this.alarmCollection = alarms
      _this.linkCollection = links
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    deviceWebsocket.subscribeDiscoverDevice(_this.deviceCollection, _this.subnetCollection, _this.topologyCollection, (devices, subnets, topologys) => {
      _this.deviceCollection = devices
      _this.subnetCollection = subnets
      _this.topologyCollection = topologys
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    deviceWebsocket.subscribeMoveDeviceFromSubnet(_this.deviceCollection, _this.subnetCollection, _this.topologyCollection, _this.linkCollection, _this.alarmCollection, (devices, subnets, topologys, links, alarms) => {
      _this.deviceCollection = devices
      _this.subnetCollection = subnets
      _this.topologyCollection = topologys
      _this.alarmCollection = alarms
      _this.linkCollection = links
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })

    alarmWebsocket.subscribeAddAlarm(_this.alarmCollection, _this.deviceCollection, _this.topologyCollection, _this.linkCollection, _this.serverCollection,_this.subnetCollection, (alarms, devices, topologys, links, servers,subnets) => {
      _this.deviceCollection = devices
      _this.topologyCollection = topologys
      _this.alarmCollection = alarms
      _this.linkCollection = links
      _this.serverCollection = servers
      _this.subnetCollection = subnets
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    alarmWebsocket.subscribeDeleteAlarm(_this.alarmCollection, _this.deviceCollection, _this.topologyCollection, _this.linkCollection, (alarms, devices, topologys, links) => {
      _this.deviceCollection = devices
      _this.topologyCollection = topologys
      _this.alarmCollection = alarms
      _this.linkCollection = links
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    alarmWebsocket.subscribeModifyAlarm(_this.alarmCollection, _this.deviceCollection, _this.topologyCollection, _this.linkCollection, _this.subnetCollection ,(alarms, devices, topologys, links,subnets) => {
      _this.deviceCollection = devices
      _this.topologyCollection = topologys
      _this.alarmCollection = alarms
      _this.linkCollection = links
      _this.subnetCollection = subnets
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    alarmWebsocket.subscribeConfirmAlarm(_this.alarmCollection, (alarms) => {
      _this.alarmCollection = alarms
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    alarmWebsocket.subscribeClearAlarm(_this.alarmCollection, (alarms) => {
      _this.alarmCollection = alarms
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    alarmWebsocket.subscribeRecoverAlarm(_this.alarmCollection, _this.deviceCollection, _this.topologyCollection, _this.linkCollection, _this.serverCollection, _this.subnetCollection, (alarms, devices, topologys, links, servers,subnets) => {
      _this.deviceCollection = devices
      _this.topologyCollection = topologys
      _this.alarmCollection = alarms
      _this.linkCollection = links
      _this.serverCollection = servers
      _this.subnetCollection = subnets
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    alarmWebsocket.subscribeRefreshAlarm(_this.alarmCollection, _this.deviceCollection, _this.topologyCollection, _this.linkCollection, _this.subnetCollection, (alarms, devices, topologys, links, subnets) => {
      for (var i in _this.alarmCollection) {
        Vue.delete(_this.alarmCollection, i)
      }
      for (var j in alarms) {
        Vue.set(_this.alarmCollection, j, alarms[j])
      }
      _this.topologyCollection = topologys
      _this.deviceCollection = devices
      _this.linkCollection = links
      _this.subnetCollection = subnets
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })

    alarmLevelConfigWebsocket.subscribeModifyAlarmLevelConfig(_this.alarmLevelConfig, (alarmLevelConfig) => {
      _this.alarmLevelConfig = alarmLevelConfig
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })

    linkWebsocket.subscribeAddLink(_this.linkCollection, _this.deviceCollection, _this.alarmCollection, (links) => {
      _this.linkCollection = links
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    linkWebsocket.subscribeDeleteLink(_this.linkCollection, (links) => {
      _this.linkCollection = links
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    linkWebsocket.subscribeModifyLink(_this.linkCollection, _this.deviceCollection, _this.alarmCollection, (links) => {
      _this.linkCollection = links
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    linkWebsocket.subscribeDiscoverLink(_this.linkCollection, (links) => {
      _this.linkCollection = links
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })

    subnetWebsocket.subscribeAddSubnet(_this.subnetCollection, _this.topologyCollection, (subnets, topologys) => {
      _this.subnetCollection = subnets
      _this.topologyCollection = topologys
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    subnetWebsocket.subscribeDeleteSubnet(_this.subnetCollection,_this.topologyCollection, (subnets, topologys) => {
      _this.subnetCollection = subnets
      _this.topologyCollection = topologys
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    subnetWebsocket.subscribeModifySubnet(_this.subnetCollection, _this.topologyCollection, (subnets, topologys) => {
      _this.subnetCollection = subnets
      _this.topologyCollection = topologys
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })

    alarmFilterWebsocket.subscribeAddAlarmFilter(() => {
      _this.syncData()
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    alarmFilterWebsocket.subscribeDeleteAlarmFilter(() => {
      _this.syncData()
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    alarmFilterWebsocket.subscribeModifyAlarmFilter(() => {
      _this.syncData()
      _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })

    UserServiceWebsocket.subscribeModifyAuthority((returnObject) => {
      helperMethods.userToLoginPage(returnObject, () => {
        _this.InfoTip.messageTip(_this, HelperUtil.getCheckStatusCodeObjectByCode(_this.successCode.MODIFY_AUTHORITY_TO_LOGIN), () => { HelperUtil.toLoginPage() })
      })
    })
    UserServiceWebsocket.subscribeModifyUserPwd((returnObject) => {
      helperMethods.userToLoginPage(returnObject, () => {
        _this.InfoTip.messageTip(_this, HelperUtil.getCheckStatusCodeObjectByCode(_this.successCode.MODIFY_PWD_TO_LOGIN), () => { HelperUtil.toLoginPage() })
      })
    })
    UserServiceWebsocket.subscribeModifyUserGroup((returnObject) => {
      helperMethods.userToLoginPage(returnObject, () => {
        _this.InfoTip.messageTip(_this, HelperUtil.getCheckStatusCodeObjectByCode(_this.successCode.MODIFY_USER_TO_LOGIN), () => { HelperUtil.toLoginPage() })
      })
    })
    UserServiceWebsocket.subscribeSameUserLogin((returnObject) => {
      helperMethods.userToLoginPage(returnObject, () => {
        _this.InfoTip.messageTip(_this, HelperUtil.getCheckStatusCodeObjectByCode(_this.successCode.SAME_USER_TO_LOGIN), () => { HelperUtil.toLoginPage() })
      })
    })
    UserServiceWebsocket.subscribeDeleteUser((returnObject) => {
      helperMethods.userToLoginPage(returnObject, () => {
        _this.InfoTip.messageTip(_this, HelperUtil.getCheckStatusCodeObjectByCode(_this.successCode.DELETE_USER_TO_LOGIN), () => { HelperUtil.toLoginPage() })
      })
    })
    configFileDifferenceWebsocket.subscribeUpdateConfigFileDifference(_this.configFileDifferenceCollection, (configFileDifferences) => {
      _this.configFileDifferenceCollection = configFileDifferences;
      // _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    configFileDifferenceWebsocket.subscribeAddConfigFileDifference(_this.configFileDifferenceCollection, (configFileDifferences) => {
      _this.configFileDifferenceCollection = configFileDifferences;
      // _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
    configFileDifferenceWebsocket.subscribeDeleteConfigFileDifference(_this.configFileDifferenceCollection, (configFileDifferences) => {
      _this.configFileDifferenceCollection = configFileDifferences;
      // _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })

    // 可能不需要更新拓扑
    // elementDiscoverWebsocket.subscribeAddDiscoverDevice(_this.discoveryCollection, (elementDiscover) => {
    //   _this.discoveryCollection = elementDiscover
    //   // _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    // })
    elementDiscoverWebsocket.subscribeAddDiscoverDevice(_this.discoveryCollection, (elementDiscover) => {
      _this.discoveryCollection = elementDiscover
      // _this.setUpdateFlag(TOPOLOGY_UPDATE_FLAG)
    })
  },

  // 取消 ws 注册事件
  unregisterWebSocketEvent: function (_this, mutator = (_this) => {}) {
    let deviceWebsocket = DeviceService(_this.webSocket)
    let alarmWebsocket = AlarmService(_this.webSocket)
    let alarmLevelConfigWebsocket = AlarmLevelConfigService(_this.webSocket)
    let subnetWebsocket = SubnetService(_this.webSocket)
    let linkWebsocket = LinkService(_this.webSocket)
    let alarmFilterWebsocket = AlarmFilterConfigService(_this.webSocket)
    let UserServiceWebsocket = UserService(_this.webSocket)
    let configFileDifferenceWebsocket = ConfigFileDifferenceService(_this.webSocket)
    let elementDiscoverWebsocket = ElementDiscoverService(_this.webSocket)

    mutator(_this)

    deviceWebsocket.unsubscribeAddDevice(_this.deviceCollection, _this.subnetCollection, _this.topologyCollection)
    deviceWebsocket.unsubscribeDeleteDevice(_this.deviceCollection, _this.subnetCollection, _this.linkCollection, _this.topologyCollection)
    deviceWebsocket.unsubscribeModifyDevice(_this.deviceCollection, _this.subnetCollection, _this.topologyCollection, _this.alarmCollection, _this.linkCollection)
    deviceWebsocket.unsubscribeDiscoverDevice(_this.deviceCollection, _this.subnetCollection, _this.topologyCollection)
    deviceWebsocket.unsubscribeMoveDeviceFromSubnet()

    alarmWebsocket.unsubscribeAddAlarm(_this.alarmCollection)
    alarmWebsocket.unsubscribeDeleteAlarm(_this.alarmCollection)
    alarmWebsocket.unsubscribeModifyAlarm(_this.alarmCollection)
    alarmWebsocket.unsubscribeConfirmAlarm(_this.alarmCollection)
    alarmWebsocket.unsubscribeClearAlarm(_this.alarmCollection)
    alarmWebsocket.unsubscribeRecoverAlarm(_this.alarmCollection)
    alarmWebsocket.unsubscribeRefreshAlarm(_this.alarmCollection)

    alarmLevelConfigWebsocket.unsubscribeModifyAlarmLevelConfig(_this.alarmLevelConfig)

    linkWebsocket.unsubscribeAddLink(_this.linkCollection)
    linkWebsocket.unsubscribeDeleteLink(_this.linkCollection)
    linkWebsocket.unsubscribeModifyLink(_this.linkCollection)
    linkWebsocket.unsubscribeDiscoverLink(_this.linkCollection)

    subnetWebsocket.unsubscribeAddSubnet(_this.subnetCollection)
    subnetWebsocket.unsubscribeDeleteSubnet(_this.subnetCollection)
    subnetWebsocket.unsubscribeModifySubnet(_this.subnetCollection)

    alarmFilterWebsocket.unsubscribeAddAlarmFilter()
    alarmFilterWebsocket.unsubscribeDeleteAlarmFilter()
    alarmFilterWebsocket.unsubscribeModifyAlarmFilter()

    UserServiceWebsocket.unsubscribeModifyAuthority()
    UserServiceWebsocket.unsubscribeModifyUserPwd()
    UserServiceWebsocket.unsubscribeModifyUserGroup()
    UserServiceWebsocket.unsubscribeSameUserLogin()
    UserServiceWebsocket.unsubscribeDeleteUser()

    configFileDifferenceWebsocket.unsubscribeUpdateConfigFileDifference(_this.configFileDifferenceCollection)
    configFileDifferenceWebsocket.unsubscribeAddConfigFileDifference(_this.configFileDifferenceCollection)
    configFileDifferenceWebsocket.unsubscribeDeleteConfigFileDifference(_this.configFileDifferenceCollection)

    elementDiscoverWebsocket.unsubscribeAddDiscoverDevice(_this.discoveryCollection)
  },

  // 打开websocket 连接
  openWebsocketConnect: function (_this) {
    // _this.webSocket = WebsocketService(DeviceStateSocketApi, {URL: DeviceStateService.getWebSocketRequestURL()})
    _this.webSocket = WebsocketService(DeviceStateSocketApi, {URL: configSelf.webSocketUrl})
  },

  // 开启websocket 的 common 操作
  openWebSocektCommon: function (_this, register = () => {}) {
    // 注册 ws 事件
    register()
    _this.webSocket.subscribeToOpen(_this, () => {
      console.log('token')
      console.log(window.sessionStorage.getItem('token'))
      _this.webSocket.send(window.sessionStorage.getItem('token'))
      _this.webSocket.send(HelperUtil.obtainLanguage())
    })
    _this.webSocket.open()
  },

  // 关闭websocket 的 common 操作
  closeWebSocketCommon: function (_this, unregister = () => {}) {
    unregister()
    _this.webSocket.close()
  }
}

function DeviceStateService (websocketService = null) {
  return Object.freeze(Object.assign(
    {
      // object methods comes here
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(DeviceStateService, staticMethods)
export default DeviceStateService
