/* --------------------------------------------------- */
/* Functions                                           */
/* --------------------------------------------------- */
let coview_api = new CoviewApi();
var attendingUsers = new Array();
var isBoard = false;
var backgroundFileName;

document.addEventListener("DOMContentLoaded", function () {
  coview_api.Init({
    coview_api_srv_addr: BIZ_TASK_YELL_URL,
    coview_wrap_id: "coviewShare",
    coview_api_key: "8dda7092c5820d663",
  });

  CoviewBridge.bindReadyEvent();

  CoviewBridge.bindStartEvent();

  CoviewBridge.bindAllByeEvent();

  CoviewBridge.bindGuestByeEvent();

  CoviewBridge.bindDestroyEvent();

  CoviewBridge.bindMessageEvent();
});

// call from collaboration_overlay_menu.html and collaboration.html
function changeCollaboration(collaborationType) {
  recordStop(function () {
    var newMeetingID = 0;
    if (globalUserInfo.collaborationType == COLLABORATION_TYPE.DOCUMENT) {
      NativeBridgeDelegate.exitMeetingRoom();
    }
    NativeBridgeDelegate.setJoinCollaborationType(collaborationType);

    globalUserInfo.collaborationType = collaborationType;
    CollaborationUI.updateScreen(collaborationType);

    switch (collaborationType) {
      case COLLABORATION_TYPE.AUDIO:
        coview_api.ChangeCollaboration("audio");
        break;
      case COLLABORATION_TYPE.CAMERA:
        removeOldLocalVideo(g_localStream);
        coview_api.ChangeCollaboration("image");
        break;
      case COLLABORATION_TYPE.VIDEO:
        coview_api.ChangeCollaboration("video");
        break;
      case COLLABORATION_TYPE.DOCUMENT:
        coview_api.ChangeCollaboration("audio");
        newMeetingID = NativeBridgeDataSource.createContentView();
        if (newMeetingID == "-1") {
          //会議室作成失敗
          CoviewBridge.finishCollaboration();
          return;
        }
        break;
      case COLLABORATION_TYPE.BOARD:
        coview_api.ChangeCollaboration("audio");
        break;
    }

    if ($("#micBtn .voice").hasClass("disable")) {
      micOff();
    } else {
      micOn();
    }

    globalUserInfo.meetingID = newMeetingID;

    NativeBridgeDelegate.changeCollaboration(collaborationType, newMeetingID);
    FermiWebSocketBridge.changeCollaboration(collaborationType, newMeetingID);
  });
}

CollaborationUI.updateScreen = async function (collaborationType) {
  $("#coviewEraserCtrBtn").click();
  if ($(".user_btn").hasClass("hide")) {
    $(".user_btn").click();
  }
  if ($(".menu_btn").hasClass("hide")) {
    $(".menu_btn").click();
  }

  CollaborationUI.displayAddUserButtonIfNeeded();

  $("link[href='./css/collaboration_board.css']").remove();

  $(".collaboration_contents").addClass("none");

  switch (collaborationType) {
    case COLLABORATION_TYPE.AUDIO:
      $(".voice_contents").removeClass("none");
      break;
    case COLLABORATION_TYPE.CAMERA:
      $(".picture_contents").removeClass("none");
      break;
    case COLLABORATION_TYPE.VIDEO:
      $(".video_contents").removeClass("none");
      break;
    case COLLABORATION_TYPE.DOCUMENT:
      $(".document_contents").removeClass("none");
      break;
    case COLLABORATION_TYPE.BOARD:
      $(".board_contents").removeClass("none");
      $("head").append(
        '<link rel="stylesheet" href="./css/collaboration_board.css">'
      );
      break;
  }
  await waitMillisecond(1000);
  $(".before_loading_indicator").addClass("none");
};

// call from modal_collabo_host_request.html
function applyForHostChange() {
  if (
    globalUserInfo.collaborationType == COLLABORATION_TYPE.DOCUMENT &&
    deviceInfo.androidVersion < ANDROID_SDK_VERSION.O &&
    deviceInfo.isAndroid()
  ) {
    alert(getLocalizedString("not_support_version"));
    return;
  }
  const hostRequestFlg = NativeBridgeDataSource.getHostRequestFlg();
  if (hostRequestFlg == HostRequestFlag.DOING) {
    alert(getLocalizedString("already_processing_host_request"));
    return;
  }

  NativeBridgeDelegate.setHostRequestFlg(HostRequestFlag.DOING);
  FermiWebSocketBridge.changeHostApply();
}

function getFermiLoginId(loginId) {
  return globalUserInfo.shopName + "_" + loginId;
}

function screenLock() {
  // ロック用のdivを生成
  const element = document.createElement("div");
  element.id = "screenLock";

  // ロック用のスタイル
  element.style.height = "100%";
  element.style.left = "0px";
  element.style.position = "fixed";
  element.style.top = "0px";
  element.style.width = "100%";
  element.style.zIndex = "9999";
  element.style.opacity = "0";

  document.getElementsByTagName("body").item(0).appendChild(element);
}

async function waitMillisecond(millesecond) {
  await new Promise((done) => setTimeout(() => done(), millesecond));
}
