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

// 静态方法
let staticMethods = {
  /**
   * @Description  : 网元发现模板创建
   * @author       : ls
   * @date         : 2020/4/23 18:00
   * @param        :
   * @return       :
   */
  createTemplate: function (template) {
    return EdTemplateAPI.addTemplate(template).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 网元发现模板修改
   * @author       : ls
   * @date         : 2020/4/24 9:57
   * @param        :
   * @return       :
   */
  updateTemplate: function (template) {
    return EdTemplateAPI.putModifyTemplate(template).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : shanchu
   * @author       : ls
   * @date         : 2020/4/23 19:51
   * @param        :
   * @return       :
   */
  delateTemplate: function (keyList) {
    return EdTemplateAPI.deleteDeleteTemplate(keyList).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 查询所有Service
   * @author       : ls
   * @date         : 2020/4/22 20:30
   * @param        :
   * @return       :
   */
  getAllTemplateCollection: function () {
    return EdTemplateAPI.getTemplateCollection().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, TOPO_DISCOVERY_TEMPLATE_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 条件查询service
   * @author       : ls
   * @date         : 2020/4/22 17:39
   * @param        :
   * @return       :
   */
  queryTemplate: function (conditionSet) {
    return EdTemplateAPI.putTemplateCollection(conditionSet).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, TOPO_DISCOVERY_TEMPLATE_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 EdTemplateService (websocketService = null) {
  // 新建子网
  function subscribeAddSubnet (subnets, mutator = (subnets) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.ADD_SUBNET, subnets, (returnObject) => {
      for (var i = 0; i < returnObject.length; i++) {
        Vue.set(returnObject[i], 'deviceList', {})
        Vue.set(subnets, returnObject[i].subnetKey, returnObject[i])
      }
      mutator(subnets)
    })
  }
  function unsubscribeAddSubnet (subnets) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.ADD_SUBNET, subnets)
  }

  // 删除子网
  function subscribeDeleteSubnet (subnets, mutator = (subnets) => {}) {
    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)
      }
      mutator(subnets)
    })
  }
  function unsubscribeDeleteSubnet (subnets) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.DELETE_SUBNET, subnets)
  }

  // 修改子网
  function subscribeModifySubnet (subnets, mutator = (subnets) => {}) {
    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
      }
      mutator(subnets)
    })
  }
  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(EdTemplateService, staticMethods)
export default EdTemplateService
