var Namecard = {};

Namecard.makeNameCard = function (shopMemberID) {
  console.log("peacekim:: Namecard.makeNameCard shopMemberID: "+ 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) {
  console.log("peacekim:: Namecard.startChat userShopMemberID: "+ userShopMemberID + ", userName: " + 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) {
  console.log("peacekim:: Namecard.startVoice userShopMemberID: "+ userShopMemberID + ", userName: "+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) {
  console.log(
    "peacekim:: Namecard.startVoice shopMemberID: " +
      shopMemberID +
      ", star: " +
      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) {
  console.log("peacekim:: Namecard.insertFavoriteUser shopMemberID: "+ 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 () {
  console.log("peacekim:: Namecard.refreshForOnline");
  $("#favoriteButton").prop("disabled", false);
};

Namecard.refreshForOffline = function () {
  console.log("peacekim:: Namecard.refreshForOffline");
  $("#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";
  }
}
