import ConfigFileAPI from '../../api/restApis/ConfigFileAPI'
import HelperUtil from '../../utils/HelperUtil'
import {CONFIG_FILE_KEY} from '../../utils/constantCollection'
// 静态方法
let staticMethods = {
  /**
   * @Description  : 配置
   * @author       : ls
   * @date         : 2020/6/11 15:10
   * @param        :
   * @return       :
   */
  copyProfile: function (val) {
    return ConfigFileAPI.putCopyProfile(val).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 分页
   * @author       : ls
   * @date         : 2020/7/16 14:56
   * @param        :
   * @return       :
   */
  queryConfigFileDataSplitNumber: function (beginItem, endItem, selectList) {
    return ConfigFileAPI.putConfigFileQueryterms(beginItem, endItem, selectList).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, CONFIG_FILE_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : 查询总数
   * @author       : ls
   * @date         : 2020/5/6 15:58
   * @param        :
   * @return       :
   */
  queryConfigFileTotalNumber: function (selectList) {
    return ConfigFileAPI.putConfigFileCounts(selectList).then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 配置文件Service 获取所有配置文件 3914
  getAllConfigFileCollection: function () {
    return ConfigFileAPI.getAllConfigFileCollection().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, CONFIG_FILE_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 配置文件Service 选择历史配置文件恢复到设备 3865
  downloadConfigToDevice: function (configFileKey, restoryToDeviceFlag, deviceKeyList) {
    return ConfigFileAPI.putDownloadConfigToDevice(configFileKey, restoryToDeviceFlag, deviceKeyList).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 配置文件Service 设备配置文件上传 3507
  batchUploadConfigFile: function (deviceKeyList) {
    return ConfigFileAPI.putBatchUploadConfigFile(deviceKeyList).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 配置文件的删除 2787
  batchDeleteConfigFile: function (configFileKeyList) {
    return ConfigFileAPI.deleteBatchDeleteConfigFile(configFileKeyList).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  /**
   * @Description  : oneKeyUploadConfig
   * @author       : ls
   * @date         : 2021/3/25 11:36
   * @param        :
   * @return       :
   */
  oneKeyUploadConfig: function (fileName, flag, deviceKey, fileFormData) {
    return ConfigFileAPI.oneKeyUploadConfigApi(fileName, flag, deviceKey, fileFormData).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 本地配置文件上传
  uploadLocalConfigFile: function (fileName, fileFormData) {
    return ConfigFileAPI.postUploadLocalConfigFile(fileName, fileFormData).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 修改配置文件
  modifyConfigFile: function (configFile) {
    return ConfigFileAPI.putModifyConfigFile(configFile).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 配置文件下载到本地
  downloadConfigFile: function (configFileKey, configFileName) {
    // return ConfigFileAPI.putDownloadConfigFile(configFileKey, conditionSet).then(result => {
    return ConfigFileAPI.putDownloadConfigFile(configFileKey).then(result => {
      return HelperUtil.downFile(result, configFileName)
    })
  },
  /**
   * @Description  : viewConfigFile
   * @author       : ls
   * @date         : 2020/11/16 15:26
   * @param        :
   * @return       :
   */
  viewConfigFile: function (key) {
    return ConfigFileAPI.putViewConfigFile(key).then(result => {
      return HelperUtil.viewFile(result)
    })
  },
  /**
   * @Description  : putDownloadConfigFile
   * @author       : ls
   * @date         : 2020/11/10 16:50
   * @param        :
   * @return       :
   */
  putDownConfFileDevice: function (deviceKey, configFileName) {
    return ConfigFileAPI.putDownConfFileDeviceApi(deviceKey).then(result => {
      return HelperUtil.downFile(result, configFileName)
    })
  }

}

function ConfigFileService (websocketService = null) {
  return Object.freeze(Object.assign(
    {
      // object methods comes here
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(ConfigFileService, staticMethods)
export default ConfigFileService
