import SubnetManager from './SubnetManager'

let deviceStatus = {
  INUSING: 0,
  STOPUSED: 1,
  UNPLANED: 2
}

// Device模型对象示例。
var deviceSample = {
  deviceKey: '',     // 主键，字符串，唯一
  uuid: '', // 网元ID，唯一
  deviceName: '',　 // 网元名称，字符串
  deviceType: -1, // 网元类型 0：传统 1：SDN
  deviceModel: '', // 设备型号, 字符串,
  locationKey: '', //所在机房，字符串
  cabinetKey: '',  //所在机架，字符串
  subnetKey: '', // 所属子网，字符串, 设备所属子网的key
  ipAddress: '', //网元IP地址，字符串
  snmpVersion: '',  //SNMP版本,字符串
  snmpPort: -1,  // snmp get端口号
  trapPort: -1, // snmp trap端口号
  deviceStatus: -1, // 网元状态 （0:在用/1:停用）
  deviceVersion: '', // 设备版本，字符串
  startTime: '', // 开站时间，字符串
  subcardModel: '', // 子卡1型号，字符串
  subcardModel2: '', // 子卡2型号，字符串
  subcardModel3: '', // 子卡3型号，字符串
  delFlag: -1,   // 删除标识：0：未删除； 1：删除
  modifyTime: '', // 修改时间，字符串
  configFileKey: '', // c_device_syslog表的主键,字符串
  configFileName: '', // c_device_syslog表中的名称,字符串
  status: '' // 前端新增字段，设备状态，字符串，从Alarm的alarmLevel来
}

class DeviceMgrImplement {
  // 根据key值查找设备
  findDeviceByKey (deviceCollection, deviceKey) {
    return deviceCollection[deviceKey]
  }

  getDeviceListsGroupBySubnet (deviceCollection, subnetCollection) {
    let subnets = {}
    let devicesNoSubnet = {}
    subnets = this.getSubnetWithDeviceListAndNoDeviceList(deviceCollection, subnetCollection)
    devicesNoSubnet = this.getDeviceListWithoutSubnet(deviceCollection)
    // TODO : 设置好返回值
    return {
      subnets:subnets,
      devicesNoSubnet:devicesNoSubnet
    }
  }

  // 获取含有设备和没有设备的子网
  getSubnetWithDeviceListAndNoDeviceList(deviceCollection, subnetCollection) {
    let subnetMgr = SubnetManager(subnetCollection)
    let subnets = {}
    let devices = this.devicesAsArray(deviceCollection);
    // 获取没有设备的子网
    for (var subnetKey in subnetCollection) {
      subnets[subnetKey] = subnetCollection[subnetKey]
    }
    for (let i = 0; i < devices.length; i++) {
      if (devices[i].subnetKey) {
        // 当前设备有子网
        let subnet = subnets[devices[i].subnetKey]
        if (subnet) {
          this.devicesAsArray(subnet.deviceList).push(devices[i])
        } else {
          let devSubnet = subnetMgr.findSubnetByKey(devices[i].subnetKey)
          subnet = Object.assign({}, devSubnet)

          this.devicesAsArray(subnet.deviceList).push(devices[i])
          subnets[devices[i].subnetKey] = subnet
        }
      }
    }

    return subnets
  }

  // 获取在用的deviceList
  getInUsingDeviceList (deviceCollection) {
    let inUsingDeviceList = {}
    let devices = this.devicesAsArray(deviceCollection);
    for (let i = 0; i < devices.length; i++) {
      if (devices[i].deviceStatus === deviceStatus.INUSING) {
        inUsingDeviceList[devices[i].deviceKey] = devices[i]
      }
    }
    return inUsingDeviceList
  }

  // 获取停用的deviceList
  getStopUsedDeviceList (deviceCollection) {
    let stopUsingDeviceList = {}
    let devices = this.devicesAsArray(deviceCollection);
    for (let i = 0; i < devices.length; i++) {
      if (devices[i].deviceStatus === deviceStatus.STOPUSED) {
        stopUsingDeviceList[devices[i].deviceKey] = devices[i]
      }
    }
    return stopUsingDeviceList
  }

  // 获取未规划的deviceList
  getUnPlanedDeviceList (deviceCollection) {
    let unPlanedDeviceList = {}
    let devices = this.devicesAsArray(deviceCollection);
    for (let i = 0; i < devices.length; i++) {
      if (devices[i].deviceStatus === deviceStatus.UNPLANED) {
        unPlanedDeviceList[devices[i].deviceKey] = devices[i]
      }
    }
    return unPlanedDeviceList
  }

  // 根据现有deviceList构建只含有deviceKey和deviceName的deviceList
  createNewDeviceListWithPortNameAndKey (deviceCollection) {
    let deviceList = {}
    let devices = this.devicesAsArray(deviceCollection)
    for (var i = 0; i < devices.length; i++) {
      let device = {
        deviceKey: devices[i].deviceKey,
        deviceName: devices[i].deviceName,
        port: ''
      }
      deviceList[device.deviceKey] = device
    }
    return Object.values(deviceList)
  }

  // 将结构体转成List
  devicesAsArray (deviceCollection) {
    if (deviceCollection) {
      return Object.values(deviceCollection)
    }else {
      return [];
    }
  }
}

let implementMethods = new DeviceMgrImplement()

var staticMethods = {
  createDeviceSample: function () {
    return Object.freeze(Object.assign({}, deviceSample))
  }
}

// 模型对象构造函数，管理Device POJO集合。使用Factory Function模式实现。
function DeviceManager (deviceCollection, contextState = {}) {
  // 根据key值获取设备
  function findDeviceByKey (deviceKey) {
    return implementMethods.findDeviceByKey(deviceCollection, deviceKey)
  }

  // 获取含有设备和没有设备的子网以及没有字网的设备
  function getDeviceListsGroupBySubnet(subnetCollection) {
    return implementMethods.getDeviceListsGroupBySubnet(deviceCollection, subnetCollection)
  }

  // 查找含有所属设备和没有所涉设备的子网
  function getSubnetWithDeviceListAndNoDeviceList (subnetCollection) {
    return implementMethods.getSubnetWithDeviceListAndNoDeviceList(deviceCollection, subnetCollection)
  }

  // 获取在用的deviceList
  function getInUsingDeviceList () {
    return implementMethods.getInUsingDeviceList(deviceCollection)
  }

  // 获取停用的deviceList
  function getStopUsedDeviceList () {
    return implementMethods.getStopUsedDeviceList(deviceCollection)
  }

  // 获取未规划设备
  function getUnPlanedDeviceList () {
    return implementMethods.getUnPlanedDeviceList(deviceCollection)
  }

  // 获取含有设备和没有设备的子网以及没有字网的设备
  function createNewDeviceListWithPortNameAndKey () {
    return implementMethods.createNewDeviceListWithPortNameAndKey(deviceCollection)
  }

  // 结构体转数组
  function asArray () {
    return implementMethods.devicesAsArray(deviceCollection)
  }

  return Object.freeze(Object.assign(
    {
      // 在此列出模型对象方法
      findDeviceByKey,
      getDeviceListsGroupBySubnet,
      getSubnetWithDeviceListAndNoDeviceList,
      getInUsingDeviceList,
      getStopUsedDeviceList,
      getUnPlanedDeviceList,
      createNewDeviceListWithPortNameAndKey,
      asArray
    },
    staticMethods // 将静态模型方法注入到模型对象方法中
  ))
}

// 导出模型对象构造函数
export default DeviceManager
