import Stomp from 'stompjs'

export default class MessageBridge {
  observers;// 观察者

  calcSocket;// 后台websocket
  adapterSocket;// 适配器websocket

  constructor() {
    this.register = this.register.bind(this);
    this.unregister = this.unregister.bind(this);
    this.onmessage = this.onmessage.bind(this);
    this.getActions = this.getActions.bind(this);

    this.observers = new Array();

    this.calcSocket = new WebSocket("ws://192.168.10.18:8099/wisdom_service", this.onmessage, ["/topic/suggestion", "/topic/sync-command-result", "/topic/fault-count",
      "/topic/alarm-count", "/topic/fault-popup", "/topic/popup-remove", "/topic/heartbeat-data"]);
    this.adapterSocket = new WebSocket("ws://192.168.10.18:8011/wisdom", this.onmessage, ["/topic/windturbine", "/topic/pv"]);
  }

  /* 单例 */
  static getInstance() {
    if (!MessageBridge.instance) {
      MessageBridge.instance = new MessageBridge();
    }
    return MessageBridge.instance;
  }

  /* 获得消息 */
  onmessage(msg) {
    if(msg.headers["data-type"] && msg.headers["data-type"]=="heartbeat"){
      console.log("获得心跳包!");
      return;
    }
    if (msg.command != "MESSAGE" || !msg.headers.destination) return;
    var os = this.getActions(msg.headers.destination);
    for (var id in os) {
      try{
        os[id].action(msg.body);
      }catch(e){console.log(e);}
    }
  }

  getActions(destination){
    var list = new Array();
    for(var i in this.observers){
      if(this.observers[i].key==destination){
        list.push(this.observers[i]);
      }
    }
    return list;
  }

  /* 注册消息 */
  register(msgs) {
    for(var i in msgs){
      this.observers.push(msgs[i]);
    }
  }

  /* 取消注册消息 */
  unregister(msgs) {
    this.observers.remove(msgs);
  }
}

class WebSocket {
  onmessage;
  url;
  settings;
  client;
  constructor(url, onmessage, settings) {
    this.onerror = this.onerror.bind(this);
    this.connectCallBackSubscribe = this.connectCallBackSubscribe.bind(this);
    this.send = this.send.bind(this);
    this.connect=this.connect.bind(this);

    this.onmessage = onmessage;
    this.url = url;
    this.settings = settings;
    try {
      this.connect();
    } catch (e) {
      console.log("websocket连接错误:\n" + e);
    }

    console.log("websocket");
  }

  /* 连接 */
  connect() {
    console.log(`正在连接websocket [${this.url}]`)
    this.client = Stomp.client(this.url);
    this.client.connect("", "", this.connectCallBackSubscribe, this.onerror);
  }

  /* 检测连接是否正常 */
  protector() {

  }

  /* 注册 */
  connectCallBackSubscribe() {
    console.log(`注册消息${this.settings}`)
    for (var index in this.settings) {
      this.client.subscribe(this.settings[index], frame => this.onmessage(frame));
    }
  }

  /* 发送 */
  send(destination, headers, body) {
    if (this.client.connected) {
      this.client.send(destination, headers, body);
    }
  }

  /* 发生错误 */
  onerror(error) {
    console.log(`websocket [${this.url}] 连接出现错误:\n${error.message}`);
    // TODO 断线重连还有问题
    setTimeout(this.connect(), 5000);
  }
}