import LocationAPI from '../../api/restApis/LocationAPI'
import HelperUtil from '../../utils/HelperUtil'
import {LOCATION_KEY, LOCATION_DOWNLOAD_FILE_NAME} from '../../utils/constantCollection'

// 静态方法
let staticMethods = {
  // 获取所有机房
  getAllLocationCollection: function () {
    return LocationAPI.getLocationCollection().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, LOCATION_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 添加机房
  addLocation: function (location) {
    return LocationAPI.postAddLocation(location).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 删除机房
  deleteLocations: function (locationKeyList) {
    return LocationAPI.deleteDeleteLocation(locationKeyList).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 修改机房
  modifyLocation: function (location) {
    return LocationAPI.putModifyLocation(location).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 条件查询机房
  conditionQueryLocation: function (conditionSet) {
    return LocationAPI.putConditionQueryLocation(conditionSet).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, LOCATION_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 导出机房
  downloadLocation: function (conditionSet) {
    return LocationAPI.putDownloadLocation(conditionSet).then(result => {
      return HelperUtil.downFile(result, LOCATION_DOWNLOAD_FILE_NAME)
    })
  },

  // 导入机房
  uploadLocation: function (fileFormData) {
    return LocationAPI.postUploadLocation(fileFormData).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  }
}

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