import SubnetAPI from '../../api/restApis/SubnetAPI'
import HelperUtil from '../../utils/HelperUtil'
import Vue from 'vue'
import {SUBNET_KEY, SUBNET_DOWNLOAD_FILE_NAME} from '../../utils/constantCollection'
import DeviceStateSocketApi from '../../api/websocketApis/DeviceStateSocketApi'
import WebSocketService from './WebSocketService'

// 静态方法
let staticMethods = {
  // 获取所有子网
  getAllSubnetCollection: function () {
    return SubnetAPI.getSubnetCollection().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, SUBNET_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 添加子网
  addSubent: function (subnet) {
    return SubnetAPI.postAddSubnet(subnet).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 删除子网
  deleteSubnets: function (subnetKeyList) {
    return SubnetAPI.deleteDeleteSubnet(subnetKeyList).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 修改子网
  modifySubnet: function (subnet) {
    return SubnetAPI.putModifySubnet(subnet).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 条件查询子网
  conditionQuerySubnet: function (conditionSet) {
    return SubnetAPI.putConditionQuerySubnet(conditionSet).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, SUBNET_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 导出子网
  downloadSubnet: function (conditionSet) {
    return SubnetAPI.putDownloadSubnet(conditionSet).then(result => {
      return HelperUtil.downFile(result, SUBNET_DOWNLOAD_FILE_NAME)
    })
  },

  // 导入子网
  uploadSubnet: function (fileFormData) {
    return SubnetAPI.postUploadSubnet(fileFormData).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  }
}

function SubnetService (websocketService = null) {
  // 新建子网
  function subscribeAddSubnet (subnets, topologys, mutator = (subnets, topologys) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.ADD_SUBNET, subnets, (returnObject) => {
      for (var i = 0; i < returnObject.length; i++) {
        Vue.set(returnObject[i], 'deviceList', {})
        Vue.set(returnObject[i], 'belongSubnet', returnObject[i].subnetKey)
        Vue.set(returnObject[i], 'elementKey', returnObject[i].subnetKey)
        Vue.set(returnObject[i], 'horizontalAxis', 0)
        Vue.set(returnObject[i], 'horizontalOffset', 0)
        Vue.set(returnObject[i], 'nodeType', 1)
        Vue.set(returnObject[i], 'status', -1)
        Vue.set(returnObject[i], 'timeStamp', null)
        Vue.set(returnObject[i], 'topologyName', returnObject[i].subnetName)
        Vue.set(returnObject[i], 'topologyKey', returnObject[i].subnetKey)
        Vue.set(returnObject[i], 'verticalAxis', 0)
        Vue.set(returnObject[i], 'verticalOffsets', 0)
        Vue.set(returnObject[i], 'new', true)
        Vue.set(subnets, returnObject[i].subnetKey, returnObject[i])

        // 将子网放入topology
        var topologyNode = {}
        // topologyNode.topologyKey = returnObject[i].deviceKey;
        topologyNode.elementKey = returnObject[i].subnetKey
        topologyNode.horizontalAxis = 0.0
        topologyNode.verticalAxis = 0.0
        topologyNode.belongSubnet = returnObject[i].subnetKey
        topologyNode.nodeType = 1
        topologyNode.topologyName = returnObject[i].subnetName
        topologyNode.new = true
        topologyNode.status = -1
        topologyNode.deviceList = {}
        Vue.set(topologys, returnObject[i].subnetKey, topologyNode)
      }
      mutator(subnets, topologys)
    })
  }
  function unsubscribeAddSubnet (subnets) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.ADD_SUBNET, subnets)
  }

  // 删除子网
  function subscribeDeleteSubnet (subnets, topologys, mutator = (subnets, topologys) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.DELETE_SUBNET, subnets, (returnObject) => {
      for (let i = 0; i < returnObject.length; i++) {
        let subnetkey = returnObject[i].subnetKey
        Vue.delete(subnets, subnetkey)
        // 先更新key 再删除
        Vue.delete(topologys, returnObject[i].subnetKey)
      }
      mutator(subnets, topologys)
    })
  }
  function unsubscribeDeleteSubnet (subnets) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.DELETE_SUBNET, subnets)
  }

  // 修改子网
  function subscribeModifySubnet (subnets, topologys, mutator = (subnets, topologys) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.MODIFY_SUBNET, subnets, (returnObject) => {
      for (var i = 0; i < returnObject.length; i++) {
        let subnetKey = returnObject[i].subnetKey
        // 更新子网集合
        subnets[subnetKey].subnetName = returnObject[i].subnetName
        subnets[subnetKey].domain = returnObject[i].domain
        topologys[subnetKey].topologyName = returnObject[i].subnetName
      }
      mutator(subnets, topologys)
    })
  }
  function unsubscribeModifySubnet (subnets) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.MODIFY_SUBNET, subnets)
  }

  return Object.freeze(Object.assign(
    {
      // object methods comes here
      subscribeAddSubnet,
      unsubscribeAddSubnet,
      subscribeDeleteSubnet,
      unsubscribeDeleteSubnet,
      subscribeModifySubnet,
      unsubscribeModifySubnet
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(SubnetService, staticMethods)
export default SubnetService
