import HelperUtil from '../../utils/HelperUtil'
import TopologyAPI from '../../api/restApis/TopologyAPI'

// 静态方法
let staticMethods = {
  // 获取所有拓扑
  getAllTopologyCollection: function () {
    return TopologyAPI.getTopologyCollection().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, 'elementKey')
        // return HelperUtil.arrayToObject(result.data.returnValue,TOPOLOGY_KEY);
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 拓扑节点的保存
  saveTopologyCollection: function (topologyCollection) {
    return TopologyAPI.postSaveTopologyCollection(topologyCollection).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 获取交换机配置的数据
  getInterchangeData: function (ipAddress, userName, userPwd) {
    // let interchangeConfig = HelperUtil.getInterchangeRequestConfig()
    // let url = interchangeConfig.protocol + ipAddress + interchangeConfig.url
    let interchangeConfig = configSelf.interchangeData
    let url = interchangeConfig.protocol + ipAddress + interchangeConfig.url
    // 定义请求结果执行事件
    var form = $('<form></form>')
    form.attr('method', 'post')
    form.attr('Action', url)
    form.attr('target', '_blank')
    var input1 = $('<input class="text" type="text" id="name" name="name" value=""/>')
    input1.val(userName)
    var input2 = $('<input class="text" type="password" id="pwd" name="pwd"/>')
    input2.val(userPwd)
    var select = $('<select name="lang" id="lang"/>')
    select.val('cn')
    form.append(input1)
    form.append(input2)
    form.append(select)
    $('body').append(form)
    form.submit()
    form.remove()
  }
}

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