
// 静态方法
import HelperUtil from "../../utils/HelperUtil";
import {CABINET_KEY, LOCATION_KEY,CABINET_DOWNLOAD_FILE_NAME, FIRMWARE_KEY} from "../../utils/constantCollection";
import CabinetAPI from "../../api/restApis/CabinetAPI";
import FirmWareAPI from '../../api/restApis/FirmWareAPI';

let staticMethods = {
  // 获取所有固件
  getAllFirmWareCollection: function () {
    return FirmWareAPI.getFirmWareCollection().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,FIRMWARE_KEY);
      } else {
        return HelperUtil.returnPromiseRejectObj(result);
      }
    })
  },

  // 上传固件
  uploadLocalFirmWare: function (fileName, deviceType, fileFormData) {
    return FirmWareAPI.postUploadLocalFirmWare(fileName, deviceType, fileFormData).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 下载固件
  // downloadFirmWare: function (firmWareKey,firmWareName,selectList) {
  downloadFirmWare: function (firmWareKey,firmWareName) {
    return FirmWareAPI.putDownloadFirmWare(firmWareKey).then(result => {
      return HelperUtil.downFile(result,firmWareName);
    })
  },

  // 更新固件
  putUpdateFirmWareToDevice: function (firmwareKey, deviceKeyList) {
    return FirmWareAPI.updateFirmWare(firmwareKey, deviceKeyList).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 回退固件
  recoverFirmWareFile: function (deviceKeyList) {
    return FirmWareAPI.recoverFirmWare(deviceKeyList).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 删除固件
  batchDeleteFirmWare: function (firmWareKeyList) {
    return FirmWareAPI.deleteBatchFirmWare(firmWareKeyList).then(result => {
      if (result.data.result) {

      } else {
        return HelperUtil.returnPromiseRejectObj(result);
      }
    })
  },

  // 条件查询固件
  conditionQueryFirmWare: function (conditionSet) {
    return FirmWareAPI.putConditionQueryFirmWare(conditionSet).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, FIRMWARE_KEY);
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 添加机架
  addCabinet: function (cabinet) {
    return CabinetAPI.postAddCabinet(cabinet).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 删除机房
  deleteCabinets: function (cabinetKeyList) {
    return CabinetAPI.deleteDeleteCabinet(cabinetKeyList).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 修改机架
  modifyCabinet: function (cabinet) {
    return CabinetAPI.putModifyCabinet(cabinet).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 条件查询机架
  conditionQueryCabinet: function (conditionSet) {
    return CabinetAPI.putConditionQueryCabinet(conditionSet).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,CABINET_KEY);
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 导出机架
  downloadCabinet: function (conditionSet) {
    return CabinetAPI.putDownloadCabinet(conditionSet).then(result => {
      return HelperUtil.downFile(result,CABINET_DOWNLOAD_FILE_NAME);
    });
  },

  // 导入机房
  uploadCabinet: function (fileFormData) {
    return CabinetAPI.postUploadCabinet(fileFormData).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  }
};

function CabinetService (websocketService = null) {

  return Object.freeze(Object.assign(
    {
      // object methods comes here
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(CabinetService, staticMethods);
export default CabinetService;
