"use strict";
const common_vendor = require("../common/vendor.js");
const store_index = require("../store/index.js");
const init = () => {
  console.log("sok-登录");
  var timeout = 1e4;
  var timeoutObj = null;
  var timeAnt = 0;
  setInterval(() => {
    timeAnt++;
  }, 1e3);
  openConnection();
  function checkOpenSocket() {
    common_vendor.index.sendSocketMessage({
      data: "ping",
      success: (res) => {
        timeAnt = 0;
        return;
      },
      fail: (err) => {
        openConnection();
      }
    });
  }
  function openConnection() {
    common_vendor.index.connectSocket({
      url: `wss://appapi.prod.zhongshuai2023.com/ws/deviceUpMessage?token=${common_vendor.index.getStorageSync("token")}`,
      success(res) {
        console.log("连接成功 connectSocket=", res);
      },
      fail(err) {
        console.log("连接失败 connectSocket=", err);
      }
    });
    common_vendor.index.onSocketOpen((res) => {
      checkOpenSocket();
      console.log("连接成功");
    });
    common_vendor.index.onSocketError((err) => {
      console.log("连接失败");
    });
    onSocketMessage();
  }
  function onSocketMessage() {
    timeout = 1e4;
    timeoutObj = null;
    common_vendor.index.onSocketMessage((res) => {
      timeAnt = 0;
      getSocketMsg(res.data);
    });
  }
  function getSocketMsg(data) {
    var obj;
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    let url = currentPage.route;
    if (data !== "pong") {
      store_index.store.commit("setSokStatus", 1);
      common_vendor.index.hideLoading();
      obj = JSON.parse(data);
      store_index.store.commit("setCommand", obj.command);
      store_index.store.commit("setDataObj", obj);
      if (obj.insertStatus || obj.insertStatus === 0) {
        store_index.store.commit("setInsertStatus", obj.insertStatus);
      }
      if (obj.command == "startResult" && obj.result == "-1") {
        common_vendor.index.showModal({
          title: "提示",
          content: "启动失败，请重试",
          showCancel: false,
          confirmText: "确认",
          success: () => {
          }
        });
      }
      if (obj.command == "startResult" && obj.result == "-2") {
        common_vendor.index.showModal({
          title: "提示",
          content: "启动超时，请重试",
          showCancel: false,
          confirmText: "确认",
          success: () => {
          }
        });
      }
      if (obj.command == "startResult" && obj.result == "01") {
        if (url != "pageOrder/recharge/recharge") {
          common_vendor.index.navigateTo({
            url: `/pageOrder/recharge/recharge?transactionNo=${obj.transactionNo}&type=order`
          });
        }
      }
      if (obj.command == "transactionResult") {
        if (url == "pageOrder/recharge/recharge") {
          common_vendor.index.navigateBack();
        }
      }
    }
    timeAnt = 0;
    reset();
  }
  function reset() {
    clearInterval(timeoutObj);
    start();
  }
  function start() {
    timeoutObj = setInterval(function() {
      console.log(timeAnt);
      if (timeAnt > 10) {
        console.log("连接失败重新连接....");
        openConnection();
        return;
      }
      common_vendor.index.sendSocketMessage({
        data: "ping",
        success: (res) => {
          console.log(res);
        },
        fail: (err) => {
          console.log("连接失败重新连接....");
          openConnection();
        }
      });
    }, timeout);
  }
};
exports.init = init;
