var Namecard = {};

Namecard.makeNameCard = function (shopMemberID) {
  if (currentUserInfo.shopMemberID == shopMemberID) {
    return;
  }
  const nameCardInfo = NativeBridgeDataSource.getNameCardData(shopMemberID);
  const namecardTemplate = getTemplate(TemplateURL.USER_NAME_CARD);

  nameCardInfo.profileUrl = Common.getProfileImgUrl(nameCardInfo.profileUrl);
  let namecardHTML = Mustache.render(namecardTemplate, {
    shopMemberId: nameCardInfo.shopMemberId,
    profileUrl: nameCardInfo.profileUrl,
    name: nameCardInfo.shopMemberName,
    groupPathList: nameCardInfo.groupPathList,
    chat: getLocalizedString("chat"),
    voice: getLocalizedString("voice"),
    favorite: getLocalizedString("addFavorite"),
    isFavorite: nameCardInfo.isFavorite,
  });

  let namecardObj = $(jQuery.parseHTML(namecardHTML)).on(
    "click",
    function () {}
  );

  $("#userProfileModal").html(namecardObj);
  $("#userNameCard").modal("show");
};

// template_user_name_card.html
Namecard.startChat = function (userShopMemberID, userName) {
  Common.showLoadingIndicator();
  let userIDList = [];
  userIDList.push(userShopMemberID);

  // 参加ユーザ名でルーム名を生成
  const newRoomName = currentUserInfo.loginID + "," + userName;
  NativeBridgeDelegate.createChatRoom(
    ChatRoomType.DM,
    userIDList.join(","),
    newRoomName,
    MakeRoomFlag.NAME_CARD,
    false
  );
};

// template_user_name_card.html
Namecard.startVoice = function (userShopMemberID, userName) {
  Common.showLoadingIndicator();
  let userIDList = [];
  userIDList.push(userShopMemberID);

  // 参加ユーザ名でルーム名を生成
  const newRoomName = currentUserInfo.loginID + "," + userName;
  NativeBridgeDelegate.createChatRoom(
    ChatRoomType.DM,
    userIDList.join(","),
    newRoomName,
    MakeRoomFlag.NAME_CARD,
    true
  );
};

Namecard.favoriteUserChange = function (shopMemberID, star) {
  if ($(star).hasClass("active")) {
    Namecard.removeFavoriteUser(shopMemberID);
  } else if ($(star).hasClass("disable")) {
    Namecard.insertFavoriteUser(shopMemberID);
  }
};

Namecard.removeFavoriteUser = function (shopMemberID) {
  Common.showLoadingIndicator();
  $("#userNameCard").modal("hide");
  $("#myNameCard").modal("hide");
  const result = NativeBridgeDataSource.removeFavoriteUser(shopMemberID);
  if (result) {
    $(".shopmember_" + shopMemberID).removeClass("active");
    $(".shopmember_" + shopMemberID).addClass("disable");
  } else {
    $(".shopmember_" + shopMemberID).addClass("active");
    $(".shopmember_" + shopMemberID).removeClass("disable");
  }
  Common.dismissLoadingIndicator();
};

Namecard.insertFavoriteUser = function (shopMemberID) {
  $("#userNameCard").modal("hide");
  $("#myNameCard").modal("hide");
  const result = NativeBridgeDataSource.addFavoriteUser(shopMemberID);
  if (result) {
    $(".shopmember_" + shopMemberID).removeClass("disable");
    $(".shopmember_" + shopMemberID).addClass("active");
  } else {
    $(".shopmember_" + shopMemberID).addClass("disable");
    $(".shopmember_" + shopMemberID).removeClass("active");
  }
  Common.dismissLoadingIndicator();
};

Namecard.refreshForOnline = function () {
  $("#favoriteButton").prop("disabled", false);
};

Namecard.refreshForOffline = function () {
  $("#favoriteButton").prop("disabled", true);
};

Namecard.moveContactPage = function (paramGroupID) {
  const groupID = paramGroupID;
  if (window.location.pathname.includes("chat_room") ||
        window.location.pathname.includes("archive_detail"))
  {
    if (groupID == "") return;
    NativeBridgeDelegate.setToMoveGroupId(groupID);
    window.location.href = "contact.html";
  }
}
