var Namecard = {};

// archive_detail.html
// collaboration_overlay_user_list.html
// >> collaboration.js
// archive.js
// collaboration.js
// template_chatroom_user_list.html (CHATROOM_USER_LIST)
// >> chat-room.js
// template_group_user_list.html (GROUP_USER_LIST)
// >> contact.js
// template_user_list.html
// >> chat-room.js, contact.js
// template_user_message.html
// >> chat-websocket-message.js
// >> chat-room.js
CHAT_UI.makeNameCard = function (shopMemberId) {
  if (currentUserInfo.shopMemberId == shopMemberId) {
    return;
  }
  var nameCardInfo = NativeBridgeDataSource.getNameCardData(shopMemberId);
  var 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
CHAT_UI.startChat = function (userShopMemberId, userName) {
  Common.showLoadingIndicator();
  var userIdList = [];
  userIdList.push(userShopMemberId);

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

// template_user_name_card.html
CHAT_UI.startVoice = function (userShopMemberId, userName) {
  Common.showLoadingIndicator();
  var userIdList = [];
  userIdList.push(userShopMemberId);

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

// template_group_user_list.html (GROUP_USER_LIST)
// >> chat-room.js
// >> contact.js
// template_user_list.html
CHAT_UI.favoriteUserChange = function (shopMemberId, star) {
  if ($(star).hasClass("active")) {
    CHAT_UI.removeFavoriteUser(shopMemberId);
  } else if ($(star).hasClass("disable")) {
    CHAT_UI.insertFavoriteUser(shopMemberId);
  }
};

// modal_collabo_profile.html
// collaboration.js
// template_user_name_card.html
CHAT_UI.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();
};

// modal_collabo_profile.html
// collaboration.js
// template_user_name_card.html
CHAT_UI.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();
};


// contact.html
// contact.js
// template_group_list.html (GROUP_LIST)
// >> contact.js
// template_group_path.html (GROUP_PATH)
// >> self
// template_user_name_card.html
// >> CHAT_UI.makeNameCard
//全グループ検索画面表示。
CHAT_UI.refreshAllGroupSearch = function (paramGroupId) {
  var groupId = paramGroupId;
  if (window.location.pathname.includes("chat_room")) {
    if (groupId == "") return;
    NativeBridgeDelegate.setToMoveGroupId(groupId);
    window.location.href = "contact.html";
  }

  Common.showLoadingIndicator();
  $("#userNameCard").modal("hide");
  $(".cancel").addClass("none");
  $(".search_form input").removeClass("focus");
  $(".search_form input").val("");
  $(".search_form form").removeClass();
  $(".content").removeClass("none");
  $(".overlay_src_msg").empty();
  $("#contactSearch").attr(
    "placeholder",
    getLocalizedString("searchUserAndGroup")
  );
  $("#tabAllGroup").prop("checked", true);

  //オンライン状態であればサーバから情報更新。
  NativeBridgeDelegate.updateGroupInfo(groupId);

  //画面エリアを初期化。
  $("#rootGroupBtn").off();
  $("#parentGroupBtn").off();
  $("#childGroupListArea").html("");
  $("#userInGroupList").html("");
  $("#groupPathArea").html("");

  //DBからグループ情報を取得。
  var result = NativeBridgeDataSource.getGroupInfo(groupId);

  //上位グループ、トップグループ遷移ボタンのイベント追加。
  if (typeof result.parentGroupId !== "undefined") {
    $("#parentGroupBtn").on("click", function () {
      CHAT_UI.refreshAllGroupSearch(result.parentGroupId);
    });
  }
  if (typeof result.rootGroupId !== "undefined") {
    if (paramGroupId == 0) {
      groupId = result.rootGroupId;
    }
    $("#rootGroupBtn").on("click", function () {
      CHAT_UI.refreshAllGroupSearch(result.rootGroupId);
    });
  }
  if (groupId == result.rootGroupId || paramGroupId == "0") {
    $("#rootGroupArea").addClass("none");
    $("#parentGroupArea").addClass("none");
  } else {
    $("#rootGroupArea").removeClass("none");
    $("#parentGroupArea").removeClass("none");
  }
  //該当グループのパースを表示。
  const groupPathTemplate = getTemplate(TemplateURL.GROUP_PATH);
  result.groupPathList.forEach(function (groupPath) {
    let html = Mustache.render(groupPathTemplate, {
      name: groupPath.groupName,
      id: groupPath.groupId,
    });
    let obj = jQuery.parseHTML(html);
    $("#groupPathArea").append(obj);
  });

  //該当グループの下位グループ表示。
  const groupTemplate = getTemplate(TemplateURL.GROUP_LIST);
  result.childGroupList.forEach(function (childGroup) {
    let html = Mustache.render(groupTemplate, {
      name: childGroup.groupName,
      id: childGroup.groupId,
      isFavorite: childGroup.isFavorite,
    });

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

  //該当グループの所属ユーザを表示。
  const userTemplate = getTemplate(TemplateURL.USER_LIST);

  result.groupUserList.forEach(function (groupUser) {
    groupUser.profileUrl = Common.getProfileImgUrl(groupUser.profileUrl);
  });
  let html = Mustache.render(userTemplate, {
    userList: result.groupUserList,
  });
  let obj = jQuery.parseHTML(html);
  $("#userInGroupList").append(obj);
  Common.dismissLoadingIndicator();
};

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

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