import CabinetManager from './CabinetManager'

var LocationSample = {
  locationKey: '',
  locationId: '',
  locationName: '',
  line: '',
  site: '',
  km: '',
  longitude: '',
  latitude: '',
  delFlag: '',
  modifyTime: ''
}

class LocationMgrImplement {
  // 根据key值获取机房
  findLocationByKey (locationCollection, locationKey) {
    return locationCollection[locationKey]
  }

  // 查找机架所在机房
  findCabinetLocation (cabinetKey, CabinetCollection, locationCollection) {
    let cabiinetMgr = CabinetManager(CabinetCollection)
    let cabinet = cabiinetMgr.findCabinetByKey(cabinetKey)
    return this.findLocationByKey(locationCollection, cabinet.locationKey)
  }

  // 结构体转数组
  locationAsArray (locationCollection) {
    return Object.values(locationCollection)
  }
}

let implementMethods = new LocationMgrImplement()

var staticMethods = {
  createSubnetSample: function () {
    return Object.freeze(Object.assign({}, LocationSample))
  }
}

function LocationManager (LocationCollection) {
  // 根据key值获取机房
  function findLocationByKey (LocationKey) {
    return implementMethods.findLocationByKey()
  }

  // 查找机架所在机房
  function findCabinetLocation (cabinetKey, CabinetCollection) {
    return implementMethods.findCabinetLocation(cabinetKey, CabinetCollection, LocationCollection)
  }

  // 结构体转数组
  function asArray() {
    implementMethods.locationAsArray(LocationCollection)
  }

  return Object.freeze(Object.assign(
    {
      // 在此列出模型对象方法
      findLocationByKey,
      findCabinetLocation,
      asArray
    },
    staticMethods // 将静态模型方法注入到模型对象方法中
  ))
}

export default LocationManager
