import ServerAPI from "../../api/restApis/ServerAPI";
import HelperUtil from '../../utils/HelperUtil'
import {SERVER_KEY, DEVICE_KEY} from "../../utils/constantCollection";
import DeviceStateSocketApi from "../../api/websocketApis/DeviceStateSocketApi";
import Vue from "vue";
import WebSocketService from "./WebSocketService";

// 静态方法
let staticMethods = {
  // 获取所有服务器
  getAllServerCollection: function(){
    return ServerAPI.getServerCollection().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,SERVER_KEY);
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 添加服务器
  addServer: function (server) {
    return ServerAPI.postAddServer(server).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 删除服务器
  deleteServers: function (serverKeyList) {
    return ServerAPI.deleteDeleteServer(serverKeyList).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 修改服务器
  modifyServer: function (server) {
    return ServerAPI.putModifyServer(server).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 条件查询服务器
  conditionQueryServer: function (conditionSet) {
    return ServerAPI.putConditionQueryServer(conditionSet).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,SERVER_KEY);
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 获取SDNC服务器管辖设备列表
  getDevicesUnderServer: function (serverId) {
    return ServerAPI.putDevicesUnderServer(serverId).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,DEVICE_KEY);
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 获取所有SDNC服务器管辖设备数量集合
  getDeviceNumUnderServer: function () {
    return ServerAPI.getDeviceNumUnderServer().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,SERVER_KEY);
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  }

};

function ServerService (websocketService = null) {

  // 新建服务器
  function subscribeAddServer (servers,mutator = (servers) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.ADD_SERVER, servers, (returnObject) => {
      Vue.set(returnObject, 'xAixs', 0.0);
      Vue.set(returnObject, 'yAixs', 0.0);
      Vue.set(returnObject, 'status', -1);
      Vue.set(servers,returnObject.serverKey,returnObject);
      mutator(servers);
    });
  }
  function unsubscribeAddServer (servers) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.ADD_SERVER, servers);
  }

  // 删除服务器
  function subscribeDeleteServer (servers,mutator = (servers) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.DELETE_SERVER, servers, (returnObject) => {
      for(let i = 0;i<returnObject.length;i++){
        let serverKey = returnObject[i];
        Vue.delete(servers,serverKey);
      }
      mutator(servers);
    });
  }
  function unsubscribeDeleteServer (servers) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.DELETE_SERVER, servers);
  }

  // 修改服务器
  function subscribeModifyServer (servers,mutator = (servers) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.MODIFY_SERVER, servers, (returnObject) => {
      let serverKey = returnObject.serverKey;
      //更新服务器集合
      servers[serverKey].serverName = returnObject.serverName;
      servers[serverKey].serverType = returnObject.serverType;
      servers[serverKey].ipAddress = returnObject.ipAddress;
      servers[serverKey].snmpVersion = returnObject.snmpVersion;
      servers[serverKey].snmpPort = returnObject.snmpPort;
      servers[serverKey].trapPort = returnObject.trapPort;
      mutator(servers);
    });
  }
  function unsubscribeModifyServer (servers) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.MODIFY_SERVER, servers);
  }

  return Object.freeze(Object.assign(
    {
      // object methods comes here
      subscribeAddServer,
      unsubscribeAddServer,
      subscribeDeleteServer,
      unsubscribeDeleteServer,
      subscribeModifyServer,
      unsubscribeModifyServer
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(ServerService, staticMethods);
export default ServerService
