var ChatManagementCommon = {};

//招待するメンバーを保存する変数
ChatManagementCommon.selectedUserList = new Array();

ChatManagementCommon.showMakeRoomConfirmView = function () {
  $("#selectedUserList").html("");

  const userTemplate = getTemplate(TemplateURL.MAKE_ROOM_CONFIRM_USER_LIST);

  const selectedUserList = NativeBridgeDataSource.loadSelectedUsers();

  selectedUserList.forEach(function (user) {
    const html = Mustache.render(userTemplate, {
      id: user.shopMemberId,
      profileImage: Common.getProfileImgUrl(user.profileUrl),
      name: user.shopMemberName,
    });
    const obj = jQuery.parseHTML(html);
    $("#selectedUserList").append(obj);
  });

  $("#makeRoomBtn")
    .off()
    .on("click", function () {
      // #36130に対応
      const trimmedRoomName = $("#newRoomName").val().trim();
      if (trimmedRoomName.length == 0) {
        // loadingIndicatorを表示
        Common.showLoadingIndicator();
        let userIDList = new Array();
        let userNameList = new Array();

        selectedUserList.forEach(function (user) {
          userIDList.push(user.shopMemberId);
          userNameList.push(user.shopMemberName);
        });

        // 参加ユーザ名でルーム名を生成
        let newRoomName =
          currentUserInfo.loginID + "," + userNameList.join(",");
        NativeBridgeDelegate.createChatRoom(
          ChatRoomType.DM,
          userIDList.join(","),
          newRoomName,
          MakeRoomFlag.MAKE_ROOM,
          false
        );
      } else if (
        trimmedRoomName.includes(";") ||
        trimmedRoomName.includes("/") ||
        trimmedRoomName.includes("?") ||
        trimmedRoomName.includes(":") ||
        trimmedRoomName.includes("@") ||
        trimmedRoomName.includes("&") ||
        trimmedRoomName.includes("=") ||
        trimmedRoomName.includes("+") ||
        trimmedRoomName.includes("$") ||
        trimmedRoomName.includes(",") ||
        trimmedRoomName.includes("-") ||
        trimmedRoomName.includes("_") ||
        trimmedRoomName.includes(".") ||
        trimmedRoomName.includes("!") ||
        trimmedRoomName.includes("~") ||
        trimmedRoomName.includes("*") ||
        trimmedRoomName.includes("'") ||
        trimmedRoomName.includes("(") ||
        trimmedRoomName.includes(")") ||
        trimmedRoomName.includes("#") ||
        trimmedRoomName.includes("\\") ||
        trimmedRoomName.includes('"') ||
        trimmedRoomName.includes("`")
      ) {
        // #36147
        // #36174
        $("#customAlertTitle").text(getLocalizedString("invalidCharacter"));
        $("#customAlertOk").text(getLocalizedString("yesTitle"));

        $("#customAlert")
          .appendTo("body")
          .modal({
            backdrop: "static",
            keyboard: false,
          })
          .on("click", "#customAlertOk", function (e) {});
      } else if (trimmedRoomName.length > 20) {
        // #36142
        var inputText = $("#newRoomName").val().trim(); // #36142 文字列の前又は後の空白文字列を削除

        // #36174
        $("#customAlertTitle").text(getLocalizedString("nameTooLong"));
        $("#customAlertOk").text(getLocalizedString("yesTitle"));

        $("#customAlert")
          .appendTo("body")
          .modal({
            backdrop: "static",
            keyboard: false,
          })
          .on("click", "#customAlertOk", function (e) {
            $("#newRoomName").val(
              inputText.substr(0, $("#newRoomName").prop("maxlength"))
            );
          });
      } else {
        //loadingIndicatorを表示
        Common.showLoadingIndicator();
        let userIdList = new Array();

        selectedUserList.forEach(function (user) {
          userIdList.push(user.shopMemberId);
        });

        // TODO: check why here using trimmedroomname instead of encodedRoomName
        // ルーム名のtrimmingした後、URIencodingを行う
        const encodedRoomName = encodeURIComponent(trimmedRoomName);
        NativeBridgeDelegate.createChatRoom(
          ChatRoomType.DM,
          userIdList.join(","),
          trimmedRoomName,
          MakeRoomFlag.MAKE_ROOM,
          false
        );
      }
    });

  $("#newRoomName").attr("placeholder", getLocalizedString("newRoomName"));
};

ChatManagementCommon.showAddUserConfirmView = function () {
  $("#selectedUserList").html("");

  const userTemplate = getTemplate(TemplateURL.ADD_USER_CONFIRM_USER_LIST);
  const selectedUserList = NativeBridgeDataSource.loadSelectedUsers();

  selectedUserList.forEach(function (user) {
    let html = Mustache.render(userTemplate, {
      id: user.shopMemberId,
      profileImage: Common.getProfileImgUrl(user.profileUrl),
      name: user.shopMemberName,
    });
    let obj = jQuery.parseHTML(html);
    $("#selectedUserList").append(obj);
  });

  $("#addUserBtn")
    .off()
    .on("click", function () {
      Common.showLoadingIndicator();
      let userIdList = new Array();
      selectedUserList.forEach(function (user) {
        userIdList.push(user.shopMemberId);
      });
      NativeBridgeDelegate.inviteUsers(userIdList.join(","));
    });
};

ChatManagementCommon.checkForMakeChat = function (checkMemberID) {
  let findObj = ChatManagementCommon.selectedUserList.find(function (
    shopMemberID
  ) {
    return shopMemberID == checkMemberID;
  });

  if (findObj) {
    // remove
    ChatManagementCommon.selectedUserList =
      ChatManagementCommon.selectedUserList.filter(function (shopMemberID) {
        return checkMemberID != shopMemberID;
      });
    ChatManagementCommon.updateCheckBox(checkMemberID, false);
  } else {
    // add
    ChatManagementCommon.selectedUserList.push(checkMemberID);
    ChatManagementCommon.updateCheckBox(checkMemberID, true);
  }

  const cnt = ChatManagementCommon.selectedUserList.length;
  if (ChatManagementCommon.selectedUserList.length > 0) {
    $(".select_member_num").text(cnt);
  } else {
    $(".select_member_num").text("0");
  }
};

ChatManagementCommon.updateCheckBox = function (checkMemberID, checked) {
  $(".checkbox" + checkMemberID)
    .prop("checked", checked)
    .trigger("change");
};