const _this = {};

export const init_module = (e) => {
  _this.func = e.func;
  _this.glb = e.glb;
  _this.SESSION_OBJ = e.SESSION_OBJ;
  _this.APP_OBJ = e.APP_OBJ;
  _this.IS_DOCKER = e.IS_DOCKER;
  _this.IS_API_SERVER = e.IS_API_SERVER;
  _this.IS_PROCESS_SERVER = e.IS_PROCESS_SERVER;
};

export const init_peer = async function (SESSION_ID, peer_regional_server) {
  // return new Promise(async function (resolve, reject) {
  var _session = _this.SESSION_OBJ[SESSION_ID];
  // var app_id = _session.app_id;

  const peer = new Peer(_session.SYS_GLOBAL_OBJ_CLIENT_INFO.fingerprint, {
    host: `https://${peer_regional_server}`,
    path: "/peer",
    secure: true,
    config: {
      iceServers: [
        { urls: "stun:stun.l.google.com:19302" },
        { urls: "stun:stun.l.google.com:5349" },
        { urls: "stun:stun1.l.google.com:3478" },
        { urls: "stun:stun1.l.google.com:5349" },
        { urls: "stun:stun2.l.google.com:19302" },
        { urls: "stun:stun2.l.google.com:5349" },
        { urls: "stun:stun3.l.google.com:3478" },
        { urls: "stun:stun3.l.google.com:5349" },
        { urls: "stun:stun4.l.google.com:19302" },
        { urls: "stun:stun4.l.google.com:5349" },
      ],
    },
  });
  SUPPORT_PEER = peer;

  peer.on("open", function (peer_id) {
    //   func.common.db(SESSION_ID, "get_support", {
    //     peer_id,
    //     name,
    //     subject,
    //     description,
    //   });
    //   setTimeout(() => {
    //     $("body").addClass("get_support_request");
    //     $("body")
    //       .remove(".get_support_request_title")
    //       .append("<div class='get_support_request_title'>Support pending</div>");
    //   }, 2000);

    func.common.db(SESSION_ID, "get_support", {
      peer_id,
    });
    setTimeout(() => {
      $("body").addClass("get_support_request");
      $("body")
        .remove(".get_support_request_title")
        .append(
          "<div class='get_support_request_title'>Support processing</div>"
        );
    }, 2000);
  });

  // peer.on("open", function (peer_id) {
  //   func.common.db(Object.keys(SESSION_OBJ)[0], "get_support", {
  //     peer_id,
  //     name,
  //     subject,
  //     description,
  //   });

  //   $("body").addClass("get_support_request");
  // });

  peer.on("connection", async function (conn) {
    conn.on("open", () => {
      // SUPPORT_PEER_CONN = conn;
      SUPPORT_PEER_CONN = conn.send.bind(conn);

      $("body").addClass("get_support_online");
      // if (SESSION_OBJ[SESSION_ID].system_ready) {
      //   conn.send({
      //     service: "system_ready",
      //     // id: peer_id,
      //   });
      //   if (STUDIO_PEER_CONN_MSG_QUEUE.length) {
      //     for (let val of STUDIO_PEER_CONN_MSG_QUEUE) {
      //       val.id = conn.connectionId;
      //       conn.send(val);
      //     }
      //     STUDIO_PEER_CONN_MSG_QUEUE = [];
      //   }
      // }
    });
    const peer_actions_module = await func.common.get_module(
      SESSION_ID,
      "xuda-peer-actions-module.esm.js"
    );
    conn.on("data", (data) => {
      console.log(data);

      peer_actions_module.peer_actions(SESSION_ID, SUPPORT_PEER_CONN, data);
    });

    conn.on("close", function (conn) {
      $("body").removeClass("get_support_online");
    });
  });

  peer.on("call", function (call) {
    const displayMediaStreamConstraints = {
      video: {
        cursor: "always",
      },
      audio: true,
      preferCurrentTab: true,
    };

    const success = function (stream) {
      call.answer(stream);
    };

    const error = function (error) {
      call.answer(error);
    };

    if (navigator.mediaDevices.getDisplayMedia) {
      navigator.mediaDevices
        .getDisplayMedia(displayMediaStreamConstraints)
        .then(success)
        .catch(error);
    } else {
      navigator
        .getDisplayMedia(displayMediaStreamConstraints)
        .then(success)
        .catch(error);
    }
  });

  peer.on("error", function (err) {
    console.error(err);
  });
};
export const terminate_peer = async function (SESSION_ID) {
  // var _session = _this.SESSION_OBJ[SESSION_ID];
  // var app_id = _session.app_id;

  SUPPORT_PEER.disconnect();
  $("body").removeClass("get_support_request");
  $("body").find(".get_support_request_title").remove();
  $("body").removeClass("get_support_online");
  $("body").find(".get_support_online_title").remove();

  func.common.db(SESSION_ID, "cancel_support", {});
  // func.common.db(Object.keys(SESSION_OBJ)[0], "cancel_support", {});
  SUPPORT_PEER = null;
  SUPPORT_PEER_CONN = null;
};
