import vue from 'vue'

var subnetSample = {
  subnetKey: '',   //主键，子网ID，唯一
  subnetName: '',  //子网名称，字符串
  domain: '',       //管辖区域，字符串
  delFlag: -1,      // 删除标识
  modifyTime: '',   //修改时间，字符串
  deviceList: ''   //子网所管辖设备
}

class SubnetManagerImplement {
  findSubnetByKey (SubnetCollection, SubnetKey) {
    return SubnetCollection[SubnetKey]
  }

  // 获取子网下的设备
  findSubnetDeviceByKey (SubnetCollection, SubnetKey) {
    return SubnetCollection[SubnetKey].deviceList
  }

  // 获取没有子网的设备
  functionDeviceWithoutSubnet (SubnetCollection) {
    for (var subnetKey in SubnetCollection) {
      if (subnetKey === '0') {
        return SubnetCollection[subnetKey].deviceList
      }
    }
  }

  subnetCollectionTotopology (SubnetCollection, TopologyCollection) {
    let subnetList = this.subnetAsArray(SubnetCollection)
    let topologyList = this.subnetAsArray(TopologyCollection)
    if (subnetList.length > topologyList.length) {
      for (let subnetKey in SubnetCollection) {
          if(!TopologyCollection.hasOwnProperty(subnetKey)) {
            vue.set(TopologyCollection, subnetKey, SubnetCollection[subnetKey])
        }
      }
    } else if (subnetList.length < topologyList.length) {
      for (let topologyKey in TopologyCollection) {
        if (!SubnetCollection.hasOwnProperty(topologyKey)) {
          vue.delete(TopologyCollection, topologyKey)
        }
      }
    } else if (subnetList.length === topologyList.length) {
      
    }
  }

  // 结构体转数组
  subnetAsArray (SubnetCollection) {
    return Object.values(SubnetCollection)
  }
}

let implementMethods = new SubnetManagerImplement();

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

// 模型对象构造函数，管理Device POJO集合。使用Factory Function模式实现。
function SubnetManager (SubnetCollection, contextState = {}) {
  // 根据key值获取子网
  function findSubnetByKey (SubnetKey) {
    return implementMethods.findSubnetByKey(SubnetCollection, SubnetKey)
  }

  // 查找子网下的所属设备
  function findSubnetDeviceByKey (SubnetKey) {
    return implementMethods.findSubnetDeviceByKey(SubnetCollection, SubnetKey)
  }

  function functionDeviceWithoutSubnet () {
    return implementMethods.functionDeviceWithoutSubnet(SubnetCollection)
  }

  function subnetCollectionTotopology (TopologyCollection) {
    return implementMethods.subnetCollectionTotopology(SubnetCollection, TopologyCollection)
  }

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

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

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