import _ from "lodash";
import { defineStore } from "pinia";
import { useUser } from "@/store/user.js";
import { usePrint } from "@/store/print.js";
import useStorage from "@/utils/useStorage";
import ReconnectingWebSocket from "reconnecting-websocket";
import { useGoods } from "@/store/goods.js";
import { ElMessage } from "element-plus";
import { getOrderByIdAjax, commOrderPrintData } from "@/utils/index.js";

export const useSocket = defineStore("socket", {
  state: () => ({
    online: false, // 在线状态
    ws: null, // websocket实例
    heartbeatTimer: null, // 心跳计时器
    orderList: [],
    orderListTimer: null,
    log: false,
    isPrinting: false,
  }),
  actions: {
    // 关闭ws
    close() {
      if (this.log) console.log("关闭ws");
      this.online = false;
      this.ws.close(1000);
      this.ws = null;
      this.clearHeartBeat();
    },
    wsReconnect: _.throttle(
      function () {
        if (this.ws.readyState == ReconnectingWebSocket.OPEN) return;
        this.ws.reconnect();
        // console.log("11111");
      },
      2000,
      { leading: true, trailing: false }
    ),
    cartInit() {
      const store = useUser();
      const goodsStore = useGoods();

      this.ws.send(
        JSON.stringify({
          type: "cashier",
          account: `cashier_${store.shopInfo.id}`,
          operate_type: "init",
          shop_id: store.shopInfo.id,
          table_code:
            goodsStore.tableInfo.tableCode || useStorage.get("tableCode"),
        })
      );
    },
    // 初始化
    init(wsUrl = import.meta.env.VITE_API_WSS) {
      return new Promise((resolve, reject) => {
        const store = useUser();
        const printStore = usePrint();
        const goodsStore = useGoods();

        if (!store.shopInfo.id) return;

        printStore.init();

        if (this.ws == null) {
          if (this.log) console.log("创建新的ws连接");

          const protocols = []; // 可选的子协议数组
          const options = {
            // 自动重新连接的选项（可选）
            connectionTimeout: 1000,
            maxRetries: 100,
          };
          this.ws = new ReconnectingWebSocket(wsUrl, protocols, options);
        } else {
          if (this.log) console.log("重新连接ws");
          this.wsReconnect();
        }

        this.ws.addEventListener("open", (event) => {
          if (this.log) console.log("wss连接成功");
          this.online = true;
          // 清除心跳
          this.clearHeartBeat();
          if (this.log) console.log(this);
          this.cartInit();
          this.startheartbeat();

          resolve()
        });

        this.ws.addEventListener("message", async (e) => {
          let data = JSON.parse(e.data);
          if (data.operate_type == "init") {
            // console.log("接收消息", data);
            if (!goodsStore.tableInfo.tableCode) {
              useStorage.set("tableCode", data.table_code);
            }
            goodsStore.getCartList(data.data);
          }

          this.ws.send(
            JSON.stringify({
              type: "receipt",
              msg_id: data.msg_id,
            })
          );

          if (data.data_type == "cart") {
            if (data.status == 1) {
              // 返回成功状态
              switch (data.operate_type) {
                case "add":
                  // 添加购物车商品
                  goodsStore.successAddCart(data.data);
                  break;
                case "edit":
                  // 编辑购物车商品
                  goodsStore.successEditCart(data.data);
                  break;
                case "del":
                  // 删除购物车商品
                  if (data.type && data.type == "bc") {
                    goodsStore.successDeleteCartItem(data.data);
                  } else {
                    goodsStore.successDeleteCartItem();
                  }
                  break;
                case "cleanup":
                  // 清空购物车
                  if (
                    data.data.table_code == goodsStore.orderListInfo.tableCode &&
                    !data.type
                  )
                    return;
                  goodsStore.successClearCart();
                  break;
                case "batch":
                  // 整单打包
                  this.cartInit();
                  break;
                case "rottable":
                  // 转桌
                  useStorage.set("tableCode", data.data.new_table_code);
                  goodsStore.successClearCart();
                  goodsStore.historyOrderAjax(data.data.new_table_code);
                  this.cartInit();
                  break;
                case "clearOrder":
                  // 清空订单或删除订单
                  goodsStore.historyOrderAjax(data.data.table_code);
                  break;
                case "bulk_edit":
                  // 限时折扣更新
                  // goodsStore.updateGoodsList();
                  break;
                default:
                  break;
              }
            } else {
              if (data.type == "no_suit_num") {
                let product = goodsStore.cartList[goodsStore.cartActiveIndex];
                ElMessage.error(
                  `${product.product_name}库存不足，已删除，请选择其他商品`
                );
                goodsStore.operateCart(product, "del");
              } else {
                ElMessage.error(data.msg || "操作失败");
              }
            }
          } else if (data.data_type == "order") {
            goodsStore.successClearCart();
            goodsStore.historyOrderAjax(data.data.table_code);
            this.cartInit();


            // 收到订单消息，打印订单小票
            let orderInfo = data.data.split("_");
            let orderId = orderInfo[0]; // 订单ID
            let orderModel = orderInfo[1]; // 订单类型
            let orderStatus = orderInfo[2]; // 订单ID_先付后付(1先付0后付)_订单状态 0未完成 1完成_第几次下单 
            let placeNum = orderInfo[3]; // 下单次数

            let printList = useStorage.get("printList") || [];

            if (goodsStore.orderListInfo.tableCode) {
              goodsStore.historyOrderAjax(goodsStore.orderListInfo.tableCode);
            }

            // 防止重复打印
            if (!printList.some((el) => el == orderId) && orderStatus == 1) {
              printList.push(orderId);
              useStorage.set("printList", _.uniq(printList));

              this.orderList.push(orderId);
              this.startPrintInterval();
            }

            // 防止重复打印客看单
            if (orderStatus == 0) {
              getOrderByIdAjax(orderId).then(res => {
                let originOrderInfo = res
                originOrderInfo.detailMap = _.at(originOrderInfo.detailMap, placeNum);

                console.log('originOrderInfo', originOrderInfo);

                let amout = 0
                originOrderInfo.detailMap.flat().forEach(item => {
                  amout += item.num * item.unitPrice
                });

                originOrderInfo.originAmount = amout

                if (originOrderInfo.placeNum == 1 && originOrderInfo.dineMode == 'dine-in') {
                  originOrderInfo.originAmount += originOrderInfo.seatAmount
                }

                if (originOrderInfo.packFee > 0) {
                  originOrderInfo.originAmount += originOrderInfo.packFee
                }

                printStore.pushReceiptData(commOrderPrintData({ ...originOrderInfo, isGuest: true, isBefore: false }));
              }).catch(err => {
                console.log(err);
              })
            }
          } else if (data.data_type == "product_update") {
            // 商品更新
            this.updateGoods();
          }
        });

        this.ws.addEventListener("error", () => {
          if (this.log) console.log("WebSocket连接发生错误");
          this.online = false;
          this.clearHeartBeat();
          reject('WebSocket连接发生错误')
        });

        this.ws.addEventListener("error", (e) => {
          if (this.log) console.log("ws关闭了", e);
          this.online = false;
          this.clearHeartBeat();
        });
      })
    },
    updateGoods: _.throttle(function () {
      const goodsStore = useGoods();
      goodsStore.updateGoodsList();
    }, 1000),
    // 启动心跳连接
    startheartbeat() {
      this.heartbeatTimer = setInterval(() => {
        if (this.log) console.log("发送心跳");
        this.ws.send(JSON.stringify({ type: "ping_interval", set: "cashier" }));
      }, 10000);
    },
    // 清除心跳
    clearHeartBeat() {
      // 清除心跳
      clearInterval(this.heartbeatTimer);
      this.heartbeatTimer = null;
    },
    // 打印队列开始执行
    startPrintInterval() {
      if (this.isPrinting) return; // 如果正在打印，直接返回
      this.isPrinting = true; // 标记为正在打印
      let printTime = 2000; // 定时器
      const printStore = usePrint();
      const printNextOrder = async () => {
        try {
          if (!this.orderList.length) {
            this.isPrinting = false; // 订单处理完，标记为不在打印
            return;
          }
          const orderInfo = await getOrderByIdAjax(this.orderList[0]);
          if (orderInfo.status == "done") {
            // 打印订单小票
            printStore.pushReceiptData(commOrderPrintData(orderInfo));
            // 打印标签小票
            printStore.labelPrint(commOrderPrintData(orderInfo));
          }
          this.orderList.splice(0, 1);
          // 递归调用打印下一个订单
          setTimeout(printNextOrder, printTime);
        } catch (error) {
          console.log(error);
          // 发生错误时继续尝试下一个订单
          // this.orderList.splice(0, 1);
          setTimeout(printNextOrder, printTime);
        }
      };
      // 开始打印第一个订单
      printNextOrder();
    },
  },
});
