// import { Message } from "element-ui";
// import i18n from '@/lang'
// 获取绑定在Vue的$message
import Vue from "vue";

const { $message } = Vue.prototype;
// eslint-disable-next-line
export class ELabel {
  ACTIONS = {
    GETPRINTERLIST: "GetPrinterList", // 获取打印机列表
    SETTING: "SETTING", // TODO 系统参数设置
    PRINT: "PRINT", // 标签打印
    PREVIEW: "PREVIEW", // 标签预览
    ERROR: "ERROR", // 出错
  };

  COMMAND = {
    Command: "", // 命令,ACTIONS的值
    LabelFile: "", // 要加载的模板文件名称如: test.yncx .....服务器上模板文件为根目录ElabelTemplate文件夹下,需将.yncx改为.html
    PrinterName: "", // 打印机名称, 由GETPRINTERLIST获取的值
    ColumnName: null, // 打印数据所对应的列(Columns)名
    ValueList: null, // 打印数据所对就在的行(Rows)值
  };

  printerList = null;
  webskt = null;
  socketEnable = false;
  socketOpened = false;
  isInernalAction = false;
  iTrySendTimes = 0;
  result = null;
  ipAddress = "";
  // eslint-disable-next-line class-methods-use-this
  onData = (strdata) => {};
  // eslint-disable-next-line class-methods-use-this
  onError = (errMsg) => {
    const errObj = {
      CMD: "ERROR",
      MSG: errMsg,
    };
    this.onData(JSON.stringify(errObj));
  };

  onInternalData = (strdata) => {
    const retData = JSON.parse(strdata);

    if (retData != null) {
      // eslint-disable-next-line
      switch (retData.CMD) {
        case "GetPrinterList":
          this.printerList = retData.DATA;
          break;
      }
    }
  };

  // eslint-disable-next-line class-methods-use-this
  onConnectSuccess = () => {};
  connect = (errMsg) => {
    // eslint-disable-next-line
    const objThis = this;
    objThis.socketEnable = true;
    try {
      const WsImpl = window.WebSocket || window.MozWebSocket;
      // 192.168.31.145:10606
      objThis.webskt = new WsImpl(`ws://${objThis.ipAddress}`);

      objThis.webskt.onopen = function (e) {
        objThis.socketOpened = true;
        objThis.onConnectSuccess();
      };
      objThis.webskt.onmessage = function (e) {
        if (JSON.parse(e.data).HAS_ERR === true) {
          if (JSON.parse(e.data).MSG) {
            $message.error({
              content: JSON.parse(e.data).MSG,
              key: "Error",
            });
          }
          return;
        }
        const strResult = e.data;
        objThis.result = strResult;
        try {
          if (objThis.isInernalAction) {
            objThis.isInernalAction = false;
            objThis.onInternalData(strResult);
          }

          objThis.onData(strResult);
        } catch (err) {
          window.console.log(err);
        }
      };
      objThis.webskt.onclose = function (e) {
        if (objThis.socketOpened) {
          objThis.socketEnable = false;
          objThis.socketOpened = false;
        }
        // setTimeout(objThis.connect(), 2000)
      };
      objThis.webskt.onerror = function (e) {
        objThis.socketOpened = false;
        // objThis.onError(`${i18n.t('TIPS:CORE:PRINT:UNABLE_TO_CONNECT_TO')} + ws://${objThis.ipAddress}`)
        // objThis.onError(`${i18n.t('TIPS:CORE:PRINT:PRINTER_CONNECTION_FAILED')}`)
        objThis.onError(errMsg);
        if (objThis.webskt.readyState === 0) {
          $message.error({
            content: errMsg === undefined ? "打印机连接失败" : errMsg,
            key: "Error",
          });
        }
      };
    } catch (err) {
      objThis.webskt = null;
      $message.error({
        content: errMsg === undefined ? "打印机连接失败" : errMsg,
        key: "Error",
      });
    }
  };

  disconnect = () => {
    if (this.webskt) {
      this.webskt.close();
    }
  };
  // eslint-disable-next-line
  sendData = (strData) => {
    if (this.socketOpened && this.webskt && this.webskt.readyState === 1) {
      this.result = null;
      this.iTrySendTimes = 0;
      this.webskt.send(strData);
      return true;
    }
    // eslint-disable-next-line
    this.iTrySendTimes++;
    if (this.iTrySendTimes <= 1) {
      setTimeout(this.sendData(strData), 1000);
    } else {
      // this.connect()
    }
    if (this.webskt.readyState === 0) {
      $message.error({
        content: "打印机连接失败",
        key: "Error",
      });
    }
  };

  getPrinters = () => {
    const cmd = {
      Command: this.ACTIONS.GETPRINTERLIST,
    };
    this.isInernalAction = true;
    this.sendData(JSON.stringify(cmd));
  };
}
