import request from '@/utils/request'

/**
 * @param data {any}
 * @param options undefined | { url:string|undefined}
 * @param hookFun Function | undefined
 * */
export function toPrintServer(data, options = {}, hookFun) {
  const { url } = options
  return handlePrint(data, url)
}

// 调用打印机服务
export function handlePrint(data, url) {
  return request({
    method: 'post',
    url: url || 'http://127.0.0.1:6060/print',
    data
  })
}

//打印服务录视频 "startVideo" : "stopVideo"
export function printServiceVideo(data) {
  return request({
    url: 'http://localhost:6060/video',
    method: 'post',
    data
  })
}

/**
 * @param data {any}
 * @param hookFun Function | undefined
 * */
export function toprintServiceVideo(data, hookFun) {
  return printServiceVideo(data)
}

// 打印拼多多服务
export function toPrintServicePdd(dataPrint, callback) {
  //拼多多打印
  let ws = null
  try {
    ws = new WebSocket("ws://127.0.0.1:5000/")
    //建立连接
    ws.onopen = function (evt) {
      //发送打印请求
      ws.send(JSON.stringify(dataPrint))
    };
    //收到消息
    ws.onmessage = function (evt) {
      callback(evt);
    }
    //关闭连接
    ws.onclose = function (evt) {
      callback(evt);
    };
  } catch (error) {
    ws?.close();
    throw new Error(error);
  }
}

//打印菜鸟服务
export function toPrintServiceCN(dataPrint, callback) {
  //菜鸟打印
  var ws = null;
  try {
    ws = new WebSocket("ws://127.0.0.1:13528");
    //建立连接
    ws.onopen = function (evt) {
      console.log(evt);
      //发送打印请求
      ws.send(JSON.stringify(dataPrint));
    };
    //收到消息
    ws.onmessage = function (evt) {
      let res = JSON.parse(evt.data);
      callback({
        success: res.taskStatus == "printed" || res.status == "success" ? true : false,
        data: res,
        message: res.msg || res.taskStatus == "printed" || res.status == "success" ? "成功" : "失败"
      });
    };
    //关闭连接
    ws.onclose = function (evt) {
      callback(evt);
    };
  } catch (error) {
    ws.close();
    throw new Error(error);
  }
}

//打印抖音服务
export function toPrintServiceDY(dataPrint, callback) {
  var ws = null;
  try {
    ws = new WebSocket("ws://127.0.0.1:13888");
    //建立连接
    ws.onopen = function (evt) {
      console.log(evt)
      //发送打印请求
      ws.send(JSON.stringify(dataPrint));
    };
    //收到消息
    ws.onmessage = function (evt) {
      let rsp = { success: false, message: "打印失败" }
      let res = JSON.parse(evt.data);
      if (res.cmd == "print" && res.status == "success") {
        rsp.success = true;
        rsp.message = "打印请求成功";
      }
      /* if (res.cmd == "notifyPrintResult") {
        if (res.taskStatus == "printed") {
          rsp.success = true;
          rsp.message = "打印成功";
        }
        else {
          let msg = res.printStatus[0].msg
          rsp.message = msg || "打印失败";
        }
      } */
      callback(rsp);
    };
    //关闭连接
    ws.onclose = function (evt) {
      callback({ success: false, message: "打印组件已经关闭" });
    };
  } catch (error) {
    ws.close();
    callback({ success: false, message: "连接打印组件失败" });
  }
};

//打印京东
export function toPrintServiceJD(dataPrint, callback) {
  var ws = null;
  try {
    ws = new WebSocket("ws://127.0.0.1:9113");
    //建立连接
    ws.onopen = function (evt) {
      console.log(evt)
      //发送打印请求
      ws.send(JSON.stringify(dataPrint));
    };
    //收到消息
    ws.onmessage = function (evt) {
      let rsp = { success: false, message: "打印失败" }
      let res = JSON.parse(evt.data);
      if (res.success == "true") {
        rsp.success = true;
        rsp.message = "打印请求成功";
      }
      callback(rsp);
    };
    //关闭连接
    ws.onclose = function (evt) {
      callback({ success: false, message: "打印组件已经关闭" });
    };
  } catch (error) {
    ws.close();
    callback({ success: false, message: "连接打印组件失败" });
  }
};

//打印唯品会
export function toPrintServiceVIP(dataPrint, callback) {
  var ws = null;
  try {
    ws = new WebSocket("ws://127.0.0.1:12233/print");
    //建立连接
    ws.onopen = function (evt) {
      console.log(evt)
      //发送打印请求
      ws.send(JSON.stringify(dataPrint));
    };
    //收到消息
    ws.onmessage = function (evt) {
      let rsp = { success: false, message: "打印失败" }
      let res = JSON.parse(evt.data);
      rsp.success = res.code == 200 ? true : false;
      rsp.message = res.msg || res.result;
      callback(rsp);
    };
    //关闭连接
    ws.onclose = function (evt) {
      callback({ success: false, message: "打印组件已经关闭" });
    };
  } catch (error) {
    ws.close();
    callback({ success: false, message: "连接打印组件失败" });
  }
};