CollaborationUI.initialBindAddUserButton = function () {
  console.log("peacekim:: CollaborationUI.initialBindAddUserButton");
  // ユーザー招待メンバー検索
  $(".add_user_btn").click(function () {
    console.log(
      "peacekim:: CollaborationUI.initialBindAddUserButton add_user_btn clicked"
    );
    CollaborationUI.enableScroll();
    Common.showLoadingIndicator();
    ChatManagementCommon.selectedUserList = [];
    CollaborationUI.refreshMyGroupForAddUserInCollaboration();
    $("#addUserConfirmBtnInCollaboration")
      .off()
      .on("click", function () {
        $("#addUserInCollaboration").modal("hide");
        console.log(
          "peacekim:: CollaborationUI.initialBindAddUserButton addUserConfirmBtnInCollaboration clicked ChatManagementCommon.selectedUserList: " +
            ChatManagementCommon.selectedUserList
        );

        if (ChatManagementCommon.selectedUserList.length == 0) {
          return;
        }

        const selectedUserList =
          ChatManagementCommon.selectedUserList.join(",");
        NativeBridgeDelegate.saveSelectedUserList(selectedUserList);
        CollaborationUI.confirmInviteUserListInCollaboration();
      });
  });
};

$("#tabMyGroupOnAddUserInCollaboration").on("click", function (e) {
  console.log(
    "peacekim:: CollaborationUI.initialBindAddUserButton tabMyGroupOnAddUserInCollaboration clicked"
  );
  CollaborationUI.refreshMyGroupForAddUserInCollaboration();
});

$("#tabAllGroupOnAddUserInCollaboration").on("click", function (e) {
  console.log(
    "peacekim:: CollaborationUI.initialBindAddUserButton tabAllGroupOnAddUserInCollaboration clicked"
  );
  CollaborationUI.refreshAllGroupForAddUserInCollaboration("0");
});

CollaborationUI.refreshMyGroupForAddUserInCollaboration = function () {
  console.log(
    "peacekim:: CollaborationUI.initialBindAddUserButton refreshMyGroupForAddUserInCollaboration"
  );
  if (ChatManagementCommon.selectedUserList.length > 0) {
    $(".select_member_num").text(ChatManagementCommon.selectedUserList.length);
  } else {
    $(".select_member_num").text("0");
  }
  $("#favoriteListForAddUserInCollaboration").html("");
  $("#myGroupListForAddUserInCollaboration").html("");

  $("#tabMyGroupOnAddUserInCollaboration").prop("checked", true);

  CollaborationUI.updateContactInfo();

  CollaborationUI.appendFavoriteGroupList();

  CollaborationUI.appendFavoriteUserList();

  CollaborationUI.appendMyGroupList();

  $("#addUserInCollaboration").modal("show");
  Common.dismissLoadingIndicator();
};

CollaborationUI.updateContactInfo = async function () {
  setTimeout(function () {
    NativeBridgeDelegate.updateContactInfo();
  }, 0);
};

CollaborationUI.appendFavoriteGroupList = function () {
  console.log("peacekim:: CollaborationUI.appendFavoriteGroupList");
  // グループの様式を読み込む
  const groupTemplate = getTemplate(
    TemplateURL.ADD_USER_GROUP_LIST_IN_COLLABORATION
  );
  //お気に入りグループ取得。
  const favoriteGroupList = NativeBridgeDataSource.getFavoriteGroups();
  favoriteGroupList.forEach(function (favoriteGroup) {
    let html = Mustache.render(groupTemplate, {
      name: favoriteGroup.groupName,
      id: favoriteGroup.groupId,
    });

    let obj = $(jQuery.parseHTML(html)).on("click", function () {});
    $("#favoriteListForAddUserInCollaboration").append(obj);
  });
};

CollaborationUI.appendFavoriteUserList = function () {
  console.log("peacekim:: CollaborationUI.appendFavoriteUserList");
  // ユーザの様式を読み込む
  const userTemplate = getTemplate(
    TemplateURL.ADD_USER_USER_LIST_IN_COLLABORATION
  );
  //お気に入りユーザ取得。
  const favoriteUserList = NativeBridgeDataSource.getFavoriteUsersNotInRoom();
  favoriteUserList.forEach(function (favoriteUser) {
    favoriteUser.profileUrl = Common.getProfileImgUrl(favoriteUser.profileUrl);
    let findObj = ChatManagementCommon.selectedUserList.find(function (
      shopMemberId
    ) {
      return shopMemberId == favoriteUser.shopMemberId;
    });
    if (findObj) {
      favoriteUser.checked = "checked";
    }
  });
  let html = Mustache.render(userTemplate, {
    userList: favoriteUserList,
  });
  let obj = jQuery.parseHTML(html);
  $("#favoriteListForAddUserInCollaboration").append(obj);
};

CollaborationUI.appendMyGroupList = function () {
  console.log("peacekim:: CollaborationUI.appendMyGroupList");
  const groupUserTemplate = getTemplate(
    TemplateURL.ADD_USER_GROUP_USER_LIST_IN_COLLABORATION
  );
  const myGroupList = NativeBridgeDataSource.getMyGroupUsersNotInRoom();
  myGroupList.forEach(function (myGroup) {
    myGroup.groupUserList.forEach(function (groupUser) {
      groupUser.profileUrl = Common.getProfileImgUrl(groupUser.profileUrl);
      let findObj = ChatManagementCommon.selectedUserList.find(function (
        shopMemberID
      ) {
        return shopMemberID == groupUser.shopMemberId;
      });
      if (findObj) {
        groupUser.checked = "checked";
      }
    });
    let html = Mustache.render(groupUserTemplate, {
      groupName: myGroup.groupName,
      groupUserList: myGroup.groupUserList,
    });
    let obj = $(jQuery.parseHTML(html)).on("click", function () {});

    $("#myGroupListForAddUserInCollaboration").append(obj);
  });
};

CollaborationUI.refreshAllGroupForAddUserInCollaboration = function (groupID) {
  console.log(
    "peacekim:: CollaborationUI.refreshAllGroupForAddUserInCollaboration"
  );
  $(".content").removeClass("none");

  $("#tabAllGroupOnAddUserInCollaboration").prop("checked", true);

  setTimeout(function () {
    NativeBridgeDelegate.updateGroupInfo(groupID);
  }, 0);

  //画面エリアを初期化。
  $("#parentGroupBtnForAddUserInCollaboration").off();
  $("#rootGroupBtnForAddUserInCollaboration").off();
  $("#childGroupListAreaForAddUserInCollaboration").html("");
  $("#userInGroupListForAddUserInCollaboration").html("");
  $("#groupPathAreaForAddUserInCollaboration").html("");

  //DBからグループ情報を取得。
  const result = NativeBridgeDataSource.getGroupInfoForAddUser(groupID);

  //上位グループ、トップグループ遷移ボタンのイベント追加。
  CollaborationUI.appendRootGroupAndParentGroupIfNeeded(
    result.rootGroupId,
    result.parentGroupId,
    groupID
  );

  //該当グループのパースを表示。
  CollaborationUI.appendGroupPath(result.groupPathList);

  //該当グループの下位グループ表示。
  CollaborationUI.appendChildGroups(result.childGroupList);

  //該当グループの所属ユーザを表示。
  CollaborationUI.appendUsers(result.groupUserList);
};

CollaborationUI.bindOnClickParentGroup = function (parentGroupID) {
  console.log("peacekim:: CollaborationUI.bindOnClickParentGroup");
  $("#parentGroupBtnForAddUserInCollaboration").on("click", function () {
    console.log(
      "peacekim:: CollaborationUI.bindOnClickParentGroup parentGroupBtnForAddUserInCollaboration clicked"
    );
    CollaborationUI.refreshAllGroupForAddUserInCollaboration(parentGroupID);
  });
};

CollaborationUI.bindOnClickRootGroup = function (rootGroupID) {
  console.log("peacekim:: CollaborationUI.bindOnClickRootGroup");
  $("#rootGroupBtnForAddUserInCollaboration").on("click", function () {
    console.log(
      "peacekim:: CollaborationUI.bindOnClickRootGroup rootGroupBtnForAddUserInCollaboration clicked"
    );
    CollaborationUI.refreshAllGroupForAddUserInCollaboration(rootGroupID);
  });
};

CollaborationUI.displayRootGroupAndParentGroupIfNeeded = function (
  rootGroupID,
  groupID
) {
  console.log(
    "peacekim:: CollaborationUI.displayRootGroupAndParentGroupIfNeeded"
  );
  if (typeof rootGroupID !== "undefined" && rootGroupID == 0) {
    groupID = rootGroupID;
  }

  if (groupID == rootGroupID || groupID == "0") {
    $("#rootGroupAreaInCollaboration").addClass("none");
    $("#parentGroupAreaInCollaboration").addClass("none");
  } else {
    $("#rootGroupAreaInCollaboration").removeClass("none");
    $("#parentGroupAreaInCollaboration").removeClass("none");
  }
};

CollaborationUI.appendGroupPath = function (groupPathList) {
  console.log(
    "peacekim:: CollaborationUI.appendGroupPath groupPathList: " + groupPathList
  );
  const groupPathTemplate = getTemplate(
    TemplateURL.ADD_USER_GROUP_PATH_IN_COLLABORATION
  );

  let groupPathCount = 0;
  groupPathList.forEach(function (groupPath) {
    if (!(groupPathCount < groupPathList.length - 3)) {
      let html = Mustache.render(groupPathTemplate, {
        name: groupPath.groupName,
        id: groupPath.groupId,
      });
      let obj = jQuery.parseHTML(html);
      $("#groupPathAreaForAddUserInCollaboration").append(obj);
    }
    groupPathCount++;
  });
};

CollaborationUI.appendChildGroups = function (chidGroups) {
  console.log(
    "peacekim:: CollaborationUI.appendChildGroups chidGroups: " + chidGroups
  );
  const groupTemplate = getTemplate(
    TemplateURL.ADD_USER_GROUP_LIST_IN_COLLABORATION
  );

  chidGroups.forEach(function (childGroup) {
    let html = Mustache.render(groupTemplate, {
      name: childGroup.groupName,
      id: childGroup.groupId,
    });

    let obj = $(jQuery.parseHTML(html)).on("click", function () {});
    $("#childGroupListAreaForAddUserInCollaboration").append(obj);
  });
};

CollaborationUI.appendUsers = function (userList) {
  console.log("peacekim:: CollaborationUI.appendUsers userList: " + userList);
  const userTemplate = getTemplate(
    TemplateURL.ADD_USER_USER_LIST_IN_COLLABORATION
  );

  userList.forEach(function (groupUser) {
    groupUser.profileUrl = Common.getProfileImgUrl(groupUser.profileUrl);
    let findObj = ChatManagementCommon.selectedUserList.find(function (
      shopMemberID
    ) {
      return shopMemberID == groupUser.shopMemberId;
    });
    if (findObj) {
      groupUser.checked = "checked";
    }
  });
  let html = Mustache.render(userTemplate, {
    userList: userList,
  });
  let obj = jQuery.parseHTML(html);
  $("#userInGroupListForAddUserInCollaboration").append(obj);
};

CollaborationUI.appendRootGroupAndParentGroupIfNeeded = function (
  rootGroupID,
  parentGroupID,
  groupID
) {
  console.log(
    "peacekim:: CollaborationUI.appendRootGroupAndParentGroupIfNeeded"
  );
  if (typeof parentGroupID !== "undefined") {
    CollaborationUI.bindOnClickParentGroup(parentGroupID);
  }

  if (typeof rootGroupID !== "undefined") {
    CollaborationUI.bindOnClickRootGroup(rootGroupID);
  }

  CollaborationUI.displayRootGroupAndParentGroupIfNeeded(rootGroupID, groupID);
};

CollaborationUI.confirmInviteUserListInCollaboration = function () {
  console.log(
    "peacekim:: CollaborationUI.confirmInviteUserListInCollaboration"
  );
  const selectedUsers = NativeBridgeDataSource.loadSelectedUsers();
  if (selectedUsers != "") {
    $("#selectedUserListinCollaboration").html("");

    selectedUsers.forEach(function (user) {
      user.profileUrl = Common.getProfileImgUrl(user.profileUrl);
    });

    const modalTemplate = getTemplate(TemplateURL.MODAL_ADD_USER_CONFIRM);

    const html = Mustache.render(modalTemplate, {
      userList: selectedUsers,
    });
    let obj = jQuery.parseHTML(html);
    $("#modal_add_user_confirm").html(obj);
    $("#modalAddUserConfirm").modal("show");
  }

  CollaborationUI.bindCancelAddUserButton();

  CollaborationUI.bindAddUserButton(selectedUsers);
};

CollaborationUI.bindCancelAddUserButton = function () {
  console.log("peacekim:: CollaborationUI.bindCancelAddUserButton");
  $("#cancelAddUserBtn")
    .off()
    .on("click", function () {
      console.log(
        "peacekim:: CollaborationUI.bindCancelAddUserButton cancelAddUserBtn clicked"
      );
      $("#modalAddUserConfirm").modal("hide");
      $("#addUserInCollaboration").modal("show");
    });
};

CollaborationUI.bindAddUserButton = function (selectedUsers) {
  console.log("peacekim:: CollaborationUI.bindAddUserButton");
  $("#addUserBtn")
    .off()
    .on("click", function () {
      console.log(
        "peacekim:: CollaborationUI.bindAddUserButton addUserBtn clicked"
      );
      Common.showLoadingIndicator();
      let userIDList = selectedUsers.map((user) => user.shopMemberId);
      const commaJoinedUserIDList = userIDList.join(",");
      const collaborationType = globalUserInfo.collaborationType;
      scrollTo(0, 0);
      NativeBridgeDelegate.inviteCollaboration(
        commaJoinedUserIDList,
        collaborationType
      );

      Common.dismissLoadingIndicator();
      $("#modalAddUserConfirm").modal("hide");
    });
};

CollaborationUI.toggleCategory = function (category) {
  console.log("peacekim:: CollaborationUI.toggleCategory");
  $(category).toggleClass("open");
  $(category).next().slideToggle();
};
