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) {
  const div = $(star);
  if (div.hasClass("sp-circle")) {
    return;
  }
  div.removeClass("star");
  div.addClass("sp-circle");

  setTimeout(() => {
    if (div.hasClass("active")) {
      Namecard.removeFavoriteUser(shopMemberID, div);
    } else if (div.hasClass("disable")) {
      Namecard.insertFavoriteUser(shopMemberID, div);
    }
    div.removeClass("sp-circle");
    div.addClass("star");

    if (typeof Contact != "undefined") {
      Contact.reloadFavoriteList();
    }

    if (div.hasClass("active")) {
      window.scroll(0, window.scrollY + $(".my_info_sell").height());
    } else if (div.hasClass("disable")) {
      window.scroll(0, window.scrollY - $(".my_info_sell").height());
    }
  }, 0);
};

Namecard.removeFavoriteUser = function (shopMemberID, div) {
  const result = NativeBridgeDataSource.removeFavoriteUser(shopMemberID);
  div.removeClass(result ? "active" : "disable");
  div.addClass(result ? "disable" : "active");
};

Namecard.insertFavoriteUser = function (shopMemberID, div) {
  const result = NativeBridgeDataSource.addFavoriteUser(shopMemberID);
  div.removeClass(result ? "disable" : "active");
  div.addClass(result ? "active" : "disable");
};

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

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

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