import ElementDiscoverAPI from '../../api/restApis/ElementDiscoverAPI'
import HelperUtil from '../../utils/HelperUtil'
import DeviceStateSocketApi from '../../api/websocketApis/DeviceStateSocketApi'

// 静态方法
let staticMethods = {
  //
  getSpeedAndProgressMessage: function (key) {
    return ElementDiscoverAPI.getSpeedAndProgressMessage(key).then(result => {
      if (result.data.result) {
        console.log(result.data)
        return result.data.result
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  }
}

function ElementDiscoverService (websocketService = null) {
  function subscribeAddDiscoverDevice (discoveryCollection, mutator = (device) => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.TOPOLOGY_DISCOVER, discoveryCollection, (returnObject) => {
      console.log(returnObject)
      discoveryCollection = returnObject
      mutator(discoveryCollection)
    })
  }
  function unsubscribeAddDiscoverDevice (discoveryCollection) {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.TOPOLOGY_DISCOVER, discoveryCollection)
  }

  return Object.freeze(Object.assign(
    {
      // object methods comes here
      subscribeAddDiscoverDevice,
      unsubscribeAddDiscoverDevice
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(ElementDiscoverService, staticMethods)
export default ElementDiscoverService
