export const API_BASE_URL = "wss://sxbv2-socket.4dkankan.com";
// export const API_BASE_URL = "ws://192.168.0.76:14000";
// ws://120.24.252.95:14000
// wss://sxb-socket.4dkankan.com
// 88888888
export const VIDEO_BASE_URL = "https://houseoss.4dkankan.com/";
export const CDN_URL =
  "https://4d-tjw.oss-cn-shenzhen.aliyuncs.com/project/4dage-sxb";

export let app = {
  id: "",
  longtime: null,
  websocket: null,
  is_connect_ok: null,
  is_send_msg: null,
  noOperationTimeout: 60,

  send(msg) {
    if (!app.websocket) {
      return;
    }
    if (msg === "request") {
      app.websocket.emit(msg + " " + this.id);
    } else if (msg === "close") {
      app.websocket.emit({
        data: msg + " 0",
      });
    } else {
      if (app.is_connect_ok == null) {
        app.websocket.emit({
          data: "request " + this.id,
        });
      }
      setTimeout(function () {
        app.websocket.emit({
          data: msg,
        });
      }, 300);
    }
  },

  emitEvent: {
    close() {
      app.websocket &&
        app.websocket.emit("Close", "close", function (data) {
          console.log(data, "close");
        });
    },
    request(cb = null, authorCode = null) {
      app.websocket.emit("Request", `${app.id}#${authorCode}`, function (data) {
        cb && cb(data);
      });
    },
    switchMachine(scene) {
      app.websocket.emit("Switch", scene, function (data) {
        console.log(data, scene);
      });
    },

    Photo(photo, cb = null) {
      app.websocket.emit("Photo", photo, function (data) {
        console.log(data, photo);
        cb && cb(data);
      });
    },

    Video(video, cb = null) {
      app.websocket.emit("Video", video, function (data) {
        console.log(data, video);
        cb && cb(data);
      });
    },
  },

  showLoading: function () {
    return (() => {
      wx.showLoading({
        mask: true,
        title: "加载中",
      });
    })();
  },
  hideLoading: function () {
    return (() => {
      try {
        wx.hideLoading();
      } catch (error) {}
    })();
  },
  showAlert(msg, comfirm, confirmText = "确定") {
    if (comfirm) {
      return (() => {
        wx.showModal({
          title: "提示",
          confirmText,
          content: msg,
          showCancel: false,
          success(res) {
            if (res.confirm) {
              comfirm();
            }
          },
        });
      })();
    }
    return (() => {
      wx.showModal({
        title: "提示",
        showCancel: false,
        content: msg,
        success() {},
      });
    })();
  },
  sendCheck() {
    console.log(this.is_connect, this.is_connect_ok, this.is_other_used);

    if (!this.is_connect) {
      this.hideLoading();
      app.showAlert(
        "服务器连接失败，请重新连接",
        () => {
          app.emitEvent.request();
        },
        "重新连接"
      );
      return false;
    } else if (this.is_connect_ok === false) {
      this.hideLoading();
      app.showAlert(
        "服务器已断开，请重新连接",
        () => {
          setTimeout(() => {
            wx.showLoading({
              title: "重新连接...",
              mask: true,
            });
          });
          setTimeout(() => {
            app.emitEvent.request();
          }, 1000);
        },
        "重新连接"
      );
      return false;
    } else if (this.is_other_used) {
      this.hideLoading();
      app.showAlert("当前画屏正在被使用，请稍后", () => {
        app.emitEvent.request();
      });
      return false;
    }

    this.is_send_msg = true;

    return true;
  },

  checkOperationTimeout(stop, cb = () => {}) {
    this.operationTimer && clearTimeout(this.operationTimer);
    this.operationTimer = null;
    if (stop) {
      // 暂停检测
      return;
    }

    this.operationTimer = setTimeout(function () {
      this.is_other_used = true;
      this.is_connect_ok = false;
      app.checkOperationTimeout(true);
      cb();
    }, (this.noOperationTimeout || 120) * 1000);
  },
};
