import TopologyManager from "./TopologyManager";
import DeviceManager from "./DeviceManager";

var linkSample = {
  linkKey: '',    //主键，字符串，唯一
  linkName: '',   //链路名称，字符串
  startDeviceKey: '', //开始设备，字符串
  startPortName: '',   //开始端口,字符串
  endDeviceKey: '',   //结束设备，字符串
  endPortName: '',   //结束端口
  linkType: -1,  //链路类型
  delFlag: -1,  //删除标识
  modifyTime: '',   //修改时间，字符串
  status: '',    //链路状态，字符串
  linkFlag: -1,  //链路标志
  startDeviceName: '', //开始设备名称，字符串
  endDeviceName: ''   //结束设备名称
}

class LinkManagerImplement {
  findLinkByKey (LinkCollection, linkKey) {
    return LinkCollection[linkKey]
  }

  findLinkByDeviceKey (linkCollection, deviceCollection, deviceKey) {
    let linkList = []
    let newLinkList = []
    let deviceMgr = DeviceManager(deviceCollection)
    for (var linkKey in linkCollection) {
      let link = this.findLinkByKey(linkCollection,linkKey)
      if (link.startDeviceKey === deviceKey || link.endDeviceKey === deviceKey) {
        if (link.linkStatus === 1) {
          linkList.push(link)
        }
      }
    }
    for (var i = 0; i < linkList.length; i++) {
      let startDevice = deviceMgr.findDeviceByKey(linkList[i].startDeviceKey)
      let endDevice = deviceMgr.findDeviceByKey(linkList[i].endDeviceKey)
      if (typeof (startDevice) === "undefined" || typeof (endDevice) === "undefined") {
        continue
      }
      if (startDevice.deviceStatus !== 2 && endDevice.deviceStatus !== 2) {
        newLinkList.push(linkList[i])
      }
    }
    return newLinkList
  }

  getLinkListWithBandWidth (linkCollection, returnValue) {
    for (var i = 0; i < returnValue.length; i++) {
      for (var linkKey in linkCollection) {
        let link = this.findLinkByKey(linkCollection, linkKey)
        if (link.startDeviceKey === returnValue[i].deviceAndPort.deviceKey && link.startPortName === returnValue[i].deviceAndPort.port) {
          link.bandwidthUsed = returnValue[i].bandwidthUsed.toString()  //将已用带宽转换为字符串
          link.bandwidthMax = returnValue[i].bandwidthMax.toString()  //将最大带宽转换为字符串
        }
      }
    }
    return linkCollection
  }

  linkAsArray (linkCollection) {
    return Object.values(linkCollection)
  }
}

let implementMethods = new LinkManagerImplement()

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

function LinkManager (linkCollection, contextState = {}) {
  // 根据key值获取链路
  function findLinkByKey (LinkKey) {
    return implementMethods.findLinkByKey(linkCollection, LinkKey)
  }

  // 根据deviceKey获取链路
  function findLinkByDeviceKey (deviceCollection, deviceKey) {
    return implementMethods.findLinkByDeviceKey(linkCollection, deviceCollection, deviceKey)
  }

  // 链路集合添加带宽
  function getLinkListWithBandWidth (returnValue) {
    return implementMethods.getLinkListWithBandWidth(linkCollection, returnValue)
  }

  // 将结构体转成数组
  function asArray () {
    return implementMethods.linkAsArray(linkCollection)
  }

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

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