import DeviceStateService from './services/DeviceStateService'
import HelperUtil from '../utils/HelperUtil'
import AlarmManager from './models/AlarmManager'
import WebsocketService from './services/WebSocketService'
import DeviceStateSocketApi from '../api/websocketApis/DeviceStateSocketApi'
import DeviceService from './services/DeviceService'
import AlarmService from './services/AlarmService'
import AlarmLevelConfigService from './services/AlarmLevelConfigService'
import SubnetService from './services/SubnetService'
import LinkService from './services/LinkService'
import ElineService from './services/Business/ElineService'
import AlarmFilterConfigService from './services/AlarmFilterConfigService'
import ServerService from './services/ServerService'
import UserService from './services/UserService'
import {SERVICE_UPDATE_FLAG, TOPOLOGY_UPDATE_FLAG} from '../utils/constantCollection'
var SystemModeAdapter = {

  // 传统模式
  TRAD: {
    initAllDate: function (_this) {
      // 将data数据初始化
      let loadingInstance = _this.Loading.openLoading()
      DeviceStateService.syncDeviceState_TRAD(_this).then((result) => {
        // 开启webSocket连接
        _this.openWebSocket()
        loadingInstance.close()
        // _this.InfoTip.successTip(_this,HelperUtil.getStatusCodeObjectByCode(_this.successCode.QUERY_CODE));
      }).catch((err) => {
        loadingInstance.close()
        _this.InfoTip.errorTip(_this, err)
      })
    },

    // 同步化操作
    syncDeviceState: function (_this) {
      // 将data数据初始化
      let loadingInstance = _this.Loading.openLoading()
      DeviceStateService.syncDeviceState_TRAD(_this).then((result) => {
        loadingInstance.close()
        // _this.InfoTip.successTip(_this,HelperUtil.getStatusCodeObjectByCode(_this.successCode.QUERY_CODE));
      }).catch((err) => {
        loadingInstance.close()
        _this.InfoTip.errorTip(_this, err)
      })
    },

    registerWebSocketEvent: function (_this) {
      DeviceStateService.registerWebSocketEvent(_this)
    },

    unregisterWebSocketEvent: function (_this) {
      DeviceStateService.unregisterWebSocketEvent(_this)
    },

    openWebSocket: function (_this) {
      DeviceStateService.openWebsocketConnect(_this)
      DeviceStateService.openWebSocektCommon(_this, SystemModeAdapter.TRAD.registerWebSocketEvent(_this))
    },

    closeWebSocket: function (_this) {
      DeviceStateService.closeWebSocketCommon(_this, SystemModeAdapter.TRAD.unregisterWebSocketEvent(_this))
    }
  },

  // SDNC 模式
  SDNC: {
    initAllDate: function (_this) {
      // 将data数据初始化
      let loadingInstance = _this.Loading.openLoading()
      DeviceStateService.syncDeviceState_SDNC(_this).then((result) => {
        // 开启webSocket连接
        _this.openWebSocket()
        loadingInstance.close()
        // _this.InfoTip.successTip(_this,HelperUtil.getStatusCodeObjectByCode(_this.successCode.QUERY_CODE));
      }).catch((err) => {
        loadingInstance.close()
        _this.InfoTip.errorTip(_this, err)
      })
    },

    // 同步化操作
    syncDeviceState: function (_this) {
      // 将data数据初始化
      let loadingInstance = _this.Loading.openLoading()
      DeviceStateService.syncDeviceState_SDNC(_this).then((result) => {
        loadingInstance.close()
        // _this.InfoTip.successTip(_this,HelperUtil.getStatusCodeObjectByCode(_this.successCode.QUERY_CODE));
      }).catch((err) => {
        loadingInstance.close()
        _this.InfoTip.errorTip(_this, err)
      })
    },

    //
    registerWebSocketEvent: function (_this) {
      DeviceStateService.registerWebSocketEvent(_this, (_this) => {
        let elineWebsocket = ElineService(_this.webSocket)

        elineWebsocket.subscribeAddEline(_this.elineCollection, (elines) => {
          _this.elineCollection = elines
          _this.setUpdateFlag(SERVICE_UPDATE_FLAG)
        })
        elineWebsocket.subscribeDeleteEline(_this.elineCollection, (elines) => {
          _this.elineCollection = elines
          _this.setUpdateFlag(SERVICE_UPDATE_FLAG)
        })
        elineWebsocket.subscribeModifyEline(_this.elineCollection, (elines) => {
          _this.elineCollection = elines
          _this.setUpdateFlag(SERVICE_UPDATE_FLAG)
        })
      })
    },

    unregisterWebSocketEvent: function (_this) {
      DeviceStateService.unregisterWebSocketEvent(_this, (_this) => {
        let elineWebsocket = ElineService(_this.webSocket)
        let serverWebsocket = ServerService(_this.webSocket)

        serverWebsocket.unsubscribeAddServer(_this.serverCollection)
        serverWebsocket.unsubscribeModifyServer(_this.serverCollection)
        serverWebsocket.unsubscribeDeleteServer(_this.serverCollection)

        elineWebsocket.unsubscribeAddEline(_this.elineCollection)
        elineWebsocket.unsubscribeDeleteEline(_this.elineCollection)
        elineWebsocket.unsubscribeModifyEline(_this.elineCollection)
      })
    },

    openWebSocket: function (_this) {
      DeviceStateService.openWebsocketConnect(_this)
      DeviceStateService.openWebSocektCommon(_this, SystemModeAdapter.SDNC.registerWebSocketEvent(_this))
    },

    closeWebSocket: function (_this) {
      DeviceStateService.closeWebSocketCommon(_this, SystemModeAdapter.SDNC.unregisterWebSocketEvent(_this))
    }
  }

}

export default SystemModeAdapter
