import DeviceStateSocketApi from "../../../api/websocketApis/DeviceStateSocketApi";
import Vue from "vue";
import ElineAPI from "../../../api/restApis/BusinessAPI/ElineAPI";
import WebSocketService from "../WebSocketService";
import HelperUtil from '../../../utils/HelperUtil'
import LinkManager from '../../models/LinkManager'
import {ELINE_SERVICE_KEY} from "../../../utils/constantCollection";
// 静态方法
let staticMethods = {
  // 获取所有Eline
  getAllElineCollection: function(){
    return ElineAPI.getElineCollection().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,ELINE_SERVICE_KEY);
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 添加Eline
  addEline: function (eline) {
    return ElineAPI.postElineAdd(eline).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 删除Eline
  deleteElines: function (elineKeyList) {
    return ElineAPI.deleteElineDelete(elineKeyList).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 修改Eline
  modifyEline: function (eline) {
    return ElineAPI.putElineUpdate(eline).then(result => {
      if (result.data.result) {
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 条件查询Eline
  conditionQueryEline: function (conditionSet) {
    return ElineAPI.getElineByCondition(conditionSet).then(result => {
      if (result.data.result) {
        return result.data.returnValue;
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  //获取设备列表
  getElineDeviceList:function (serviceTypeID) {
    return ElineAPI.getElineDeviceList(serviceTypeID).then(result => {
      if (result.data.result) {
        return result.data.returnValue;
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 获取所有路由路径
  getAllTunnelCollection: function(){
    return ElineAPI.getTunnelCollection().then(result => {
      if (result.data.result) {
        return result.data.returnValue;
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 获取业务路由路径
  getAllServiceTunnelCollection: function(vpnid){
    return ElineAPI.getServiceTunnelCollection(vpnid).then(result => {
      if (result.data.result) {
        return result.data.returnValue;
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 清除已完成业务
  clearFinishedService: function (services) {
    for(let vpnId in services){
      if(services[vpnId].elineStatus !== "SUBMITTED"){
        Vue.delete(services,services[vpnId].vpnId);
      }
    }
  },

  // 获取所有路由路径new
  getAllTunnelWidthCollection: function(){
    return ElineAPI.getTunnelBandwidthCollection().then(result => {
      if (result.data.result) {
        return result.data.returnValue;
      }else{
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

};

function ElineService (websocketService = null) {
  // 新建Eline业务
  function subscribeAddEline (elines,mutator = (elines) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.ADD_ELINE, elines, (returnObject) => {
      Vue.set(elines,returnObject.vpnId,returnObject);
      mutator(elines);
    });
  }
  function unsubscribeAddEline (elines) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.ADD_ELINE, elines);
  }

  // 删除Eline业务
  function subscribeDeleteEline (elines,mutator = (elines) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.DELETE_ELINE, elines, (returnObject) => {
      Vue.delete(elines,returnObject.vpnId);
      mutator(elines);
    });
  }
  function unsubscribeDeleteEline (elines) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.DELETE_ELINE, elines);
  }

  // 修改Eline业务
  function subscribeModifyEline (elines,mutator = (elines) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.MODIFY_ELINE, elines, (returnObject) => {
      //更新业务集合状态
      elines[returnObject.vpnId].elineStatus = returnObject.elineStatus;
      mutator(elines);
    });
  }
  function unsubscribeModifyEline (elines) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.MODIFY_ELINE, elines);
  }

  // 流量监控
  function subscribeBandWidth (links,mutator = () => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.BANK_WIDTH, links, (returnObject) => {
      links = LinkManager(links).getLinkListWithBandWidth(returnObject);
      mutator();
    });
  }
  function unsubscribeBandWidth (links) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.BANK_WIDTH, links);
  }

  return Object.freeze(Object.assign(
    {
      // object methods comes here
      subscribeAddEline,
      unsubscribeAddEline,
      subscribeDeleteEline,
      unsubscribeDeleteEline,
      subscribeModifyEline,
      unsubscribeModifyEline,
      subscribeBandWidth,
      unsubscribeBandWidth
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(ElineService, staticMethods);
export default ElineService
