import ConfigFileDifferenceAPI from '../../api/restApis/ConfigFileDifferenceAPI'
import HelperUtil from '../../utils/HelperUtil'
import Vue from 'vue'
import {CONFIG_FILE_DIFFERENCE_KEY, DEVICE_KEY} from '../../utils/constantCollection'
import DeviceStateSocketApi from '../../api/websocketApis/DeviceStateSocketApi'

// 静态方法
let staticMethods = {
  // 获取所有配置差异性信息
  getConfigFileDifferenceCollection: function () {
    return ConfigFileDifferenceAPI.getConfigFileDifferenceCollection().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, CONFIG_FILE_DIFFERENCE_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 根据不同type获取不同配置差异文件
  getConfigFileView: function (fileDiffererntMessage,fileType) {
    return ConfigFileDifferenceAPI.viewFileByUser(fileDiffererntMessage,fileType).then(result => {
      return HelperUtil.viewFile(result)
    })
  },

  //
  handleConfigFile: function (fileDiffererntMessage,userOption) {
    return ConfigFileDifferenceAPI.handleFileByUser(fileDiffererntMessage,userOption).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, CONFIG_FILE_DIFFERENCE_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
};

function ConfigFileDifferenceService (websocketService = null) {
  function subscribeUpdateConfigFileDifference (configFileDifferences, mutator = (configFileDifferences) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.FILE_DIFFERENCE_MESSAGE_UPDATE, configFileDifferences, (returnObject) => {
      configFileDifferences = HelperUtil.arrayToObject(returnObject, DEVICE_KEY)
      mutator(configFileDifferences)
    })
  }

  function unsubscribeUpdateConfigFileDifference (configFileDifferences) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.FILE_DIFFERENCE_MESSAGE_UPDATE, configFileDifferences)
  }

  function subscribeAddConfigFileDifference (configFileDifferences, mutator = (configFileDifferences) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.FILE_DIFFERENCE_MESSAGE_ADD, configFileDifferences, (returnObject) => {
      console.log('ConfigFileDifferenceService添加操作')
      console.log(returnObject)
      Vue.set(configFileDifferences, returnObject.fileDifferentMessageKey, returnObject)
      mutator(configFileDifferences)
    })
  }

  function unsubscribeAddConfigFileDifference (configFileDifferences) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.FILE_DIFFERENCE_MESSAGE_ADD, configFileDifferences)
  }

  // 删除配置差异性文件
  function subscribeDeleteConfigFileDifference (configFileDifferences, mutator = (v) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.FILE_DIFFERENCE_MESSAGE_DELETE, configFileDifferences, (returnObject) => {
      console.log('ConfigFileDifferenceService删除操作')
      console.log(returnObject)
      // for (let i = 0; i < returnObject.length; i++) {
      //   let fileDifferenctMessageKey = returnObject[i].
      //   Vue.delete(configFileDifferences, fileDifferenctMessageKey)
      // }
      console.log(configFileDifferences)
      Vue.delete(configFileDifferences, returnObject.fileDifferentMessageKey)
      console.log(configFileDifferences)
      mutator(configFileDifferences)
    })
  }
  function unsubscribeDeleteConfigFileDifference (configFileDifferences) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.FILE_DIFFERENCE_MESSAGE_DELETE, configFileDifferences)
  }

  return Object.freeze(Object.assign(
    {
      // object methods comes here
      subscribeUpdateConfigFileDifference,
      unsubscribeUpdateConfigFileDifference,
      subscribeAddConfigFileDifference,
      unsubscribeAddConfigFileDifference,
      subscribeDeleteConfigFileDifference,
      unsubscribeDeleteConfigFileDifference,
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(ConfigFileDifferenceService, staticMethods)
export default ConfigFileDifferenceService
