String.prototype.replaceAll = function (org, dest) {
  return this.split(org).join(dest);
};

function includeJs(jsFilePath) {
  var js = document.createElement("script");

  js.type = "text/javascript";
  js.src = jsFilePath;

  document.body.appendChild(js);
}

function getTemplate(url) {
  var template;
  $.get({ url: url, async: false }, function (text) {
    template = text;
  });
  return template;
}

function getNoResultMessage() {
  const noResultMsg = $("<div/>", {
    width: "auto",
    style: "text-align: center",
  });
  noResultMsg.append(getLocalizedString("noResult"));
  return noResultMsg;
}

// アコーディオン
$(".category").on("click", function () {
  $(this).toggleClass("open");
  $(this).next().slideToggle();
});

$(".home_btn").on("click", function () {
  NativeBridgeDelegate.goHome();
});

document.addEventListener("DOMContentLoaded", function () {
  // ローディング表示
  $("footer a").click(function (event) {
    const a = $(this);
    event.preventDefault();
    Common.showLoadingIndicator();

    setTimeout(function () {
      window.location.href = a[0].href;
    }, 1000);
  });

  // 共通検索フォーム　キャンセルボタン表示
  $('.search_form input[type="search"]').click(function () {
    $(this).addClass("focus");
    // キャンセルボタン表示
    $(".cancel").removeClass("none");
    $(".search_form form").addClass("d-flex flex-row h-100 align-items-center");
    $(".content").addClass("none");
    $(".craeteRoomButton").addClass("none");
  });

  // 共通検索フォーム　キャンセルボタン押下イベント
  $(".search_form .cancel").click(function () {
    $(this).addClass("none");
    $(".search_form input").removeClass("focus");
    $(".search_form input").val("");
    $(".search_form form").removeClass();
    $(".content").removeClass("none");
    $(".overlay_src_msg").empty();
    $(".craeteRoomButton").removeClass("none");
  });
});

document.addEventListener("readystatechange", () => {
  
  switch (document.readyState) {
    case "interactive":
      break;
    case "complete":
      if (typeof initialLoading != "undefined" && initialLoading) {
        break;
      }
      Common.dismissLoadingIndicator();
      break;
  }
});

var Common = {};

//loadingIndicatorを表示
Common.showLoadingIndicator = function () {
  NativeBridgeDelegate.showLoadingIndicator();
};

//loadingIndicatorを表示しない
Common.dismissLoadingIndicator = function () {
  NativeBridgeDelegate.hideLoadingIndicator();
};

Common.refreshForOnline = function () {
  Common.showLoadingIndicator();
  serverInfo.isOnline = true;
  $(".footer_item a").removeClass("ui-state-disabled");
  if (typeof ChatList != "undefined") {
    ChatList.refreshForOnline();
  }
  if (typeof ChatRoom != "undefined") {
    ChatRoom.refreshForOnline();
  }
  if (typeof Namecard != "undefined") {
    Namecard.refreshForOnline();
  }
};

Common.refreshForOffline = function () {
  serverInfo.isOnline = false;
  $(".footer_item a").addClass("ui-state-disabled");
  if (typeof ChatList != "undefined") {
    ChatList.refreshForOffline();
  }
  if (typeof ChatRoom != "undefined") {
    ChatRoom.refreshForOffline();
  }
  if (typeof CollaborationUI != "undefined") {
    CollaborationUI.refreshForOffline();
  }
  if (typeof Namecard != "undefined") {
    Namecard.refreshForOffline();
  }
};

// #36170 画像パスが存在しない場合はデフォルトの画像を返す
// 存在する場合はプロフィール画像取得用APIのURLを生成して返す
Common.getProfileImgUrl = function (path) {
  if (path == undefined || path == "") {
    return "./img/noImage.png";
  } else if (path.includes("/mnt")) {
    const userInfo = path.split("/").reverse();
    return (
      serverInfo.cmsURL +
      "/chatapi/user?profileFileName=" +
      userInfo[0] +
      "&profileGetLoginId=" +
      userInfo[1] +
      "&sid=" +
      currentUserInfo.sid +
      "&cmd=12"
    );
  } else {
    return path;
  }
};

Common.startCollaboration = function (collaborationType) {
  if (deviceInfo.isAndroid()) {
    if (
      collaborationType == COLLABORATION_TYPE.DOCUMENT &&
      deviceInfo.androidVersion < ANDROID_SDK_VERSION.O
    ) {
      alert(getLocalizedString("not_support_version"));
      return;
    }
  }

  if ($(".collabo_area.start").length != 0) {
    if (!confirm(getLocalizedString("already_exist_collaboration"))) {
      Common.dismissLoadingIndicator();
      return;
    }
    const sendData = DATA_MESSAGE_SCHEME + FINISH_ALL_COLLABORATION_SIGNAL;
    CHAT_SOCKET.emitCreateMessage(sendData, 0);
    NativeBridgeDelegate.finishAllCollaboration();
  }
  NativeBridgeDelegate.startCollaboration(collaborationType);
};
