// 名前空間
var CHAT_ADD_USER = {};

document.addEventListener("DOMContentLoaded", function () {
  // メンバー検索
  bindMemberSearch();
  // iOSキーボード変換検知用
  bindiOSKeyBoardEvent();
});

// ユーザー検索
CHAT_ADD_USER.searchUser = function (keyword) {
  const isAllGroup = $("#tabAllGroupOnAddUser").is(":checked");
  const overlayMessage = $(".overlay_src_msg");
  let hasNoData = false;
  overlayMessage.empty();

  //全グループ検索画面
  if (isAllGroup) {
    //グループデータ検索
    const groupList = CHAT_DB.getGroupByName(keyword);
    searchGroupData(groupList);

    //ユーザデータ検索
    var userList = CHAT_DB.getAllGroupShopMemberNotInRoomByName(keyword);
    searchUserData(userList);
    hasNoData = userList.length == 0 && groupList.length == 0;

    //連絡先画面
  } else {
    var userList = CHAT_DB.getMyGroupShopMemberNotInRoomByName(keyword);
    searchUserData(userList);
    hasNoData = userList.length == 0;
  }
  // Set NoResult
  if (hasNoData) {
    const noResultMessage = getNoResultMessage();
    overlayMessage.append(noResultMessage);
  }
};

/** UTILS */

var bindMemberSearch = function () {
  const searchInput = $('#chat_add_user .search_form input[type="search"]');
  searchInput.click(function (e) {
    //画面タイトル設定
    let contactListTitle = getLocalizedString("userSearch");
    $("#addUserTitle").text(contactListTitle);
  });

  searchInput.keyup(function (e) {
    const keyword = searchInput.val();
    const enterKeyPressed = e.key == "Enter" || e.KeyCode == 13;
    const keywordNotEmpty = keyword != "" && keyword.length != 0;
    const keywordEmpty = keyword == "" || keyword.length < 2;

    if (enterKeyPressed) {
      if (keywordNotEmpty) {
        searchInput.blur();
        return false;
      }
    } else if (keywordEmpty) {
      $(".overlay_src_msg").empty();
      return false;
    }

    CHAT_ADD_USER.searchUser(keyword);

    if (enterKeyPressed) {
      searchInput.blur();
      return;
    }
  });
};

var bindiOSKeyBoardEvent = function () {
  const searchInput = $('#chat_add_user .search_form input[type="search"]');
  searchInput.on("compositionend", function () {
    if (CHAT_UTIL.isIOS()) {
      var keyword = searchInput.val();
      CHAT_ADD_USER.searchUser(keyword);
    }
  });
};

var searchGroupData = function (groupList) {
  const groupTemplate = getTemplate(TemplateURL.MAKE_ROOM_GROUP_LIST);

  groupList.forEach(function (group) {
    let html = renderGroupTemplate(
      groupTemplate,
      group.groupName,
      group.groupId
    );

    let obj = jQuery.parseHTML(html);
    overlayMessage.append(obj);
  });
};

var renderGroupTemplate = function (groupTemplate, groupName, groupID) {
  return Mustache.render(groupTemplate, {
    name: groupName,
    id: groupID,
  });
};

var searchUserData = function (userList) {
  const userTemplate = getTemplate(TemplateURL.MAKE_ROOM_GROUP_LIST);
  userList.forEach(function (user) {
    setUserProfile(user);
    checkUser(user);
  });
  let html = renderUserTemplate(userTemplate, userList);
  let obj = jQuery.parseHTML(html);
  overlayMessage.append(obj);
};

var setUserProfile = function (user) {
  user.profileUrl = CHAT.getProfileImgUrl(user.profileUrl);
};

var checkUser = function (user) {
  let findObj = CHAT.globalSelectedUserList.find(function (shopMemberId) {
    return shopMemberId == user.shopMemberId;
  });
  if (findObj) {
    user.checked = "checked";
  }
};

var renderUserTemplate = function (userTemplate, userList) {
  return Mustache.render(userTemplate, {
    userList: userList,
  });
};