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

document.addEventListener("DOMContentLoaded", function () {
  // メンバー検索
  searchMember();

  // iOSキーボード変換検知用
  bindiOSKeyBoardEvent();
});

// メンバー検索
CHAT_MAKE_ROOM.searchUser = function (keyword) {
  const overlayMessage = $(".overlay_src_msg");
  const isAllGroup = $("#tabAllGroupOnMakeRoom").is(":checked");
  let hasNoData = false;
  overlayMessage.empty();

  //全グループ検索画面
  if (isAllGroup) {
    //グループデータ検索
    var groupList = CHAT_DB.getGroupByName(keyword);
    var groupTemplate = getTemplate(TemplateURL.MAKE_ROOM_GROUP_LIST);

    groupList.forEach(function (group) {
      renderRoomList(groupTemplate, group.groupName, group.groupId);

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

    //ユーザデータ検索
    var userList = CHAT_DB.getAllGroupShopMemberByName(keyword);
    searchUserData(userList);
    var userTemplate = getTemplate(TemplateURL.MAKE_ROOM_USER_LIST);
    let html = renderUser(userTemplate, userList);
    let obj = jQuery.parseHTML(html);
    overlayMessage.append(obj);
    hasNoData = groupList.length == 0 && userList.length == 0;
    //連絡先画面
  } else {
    const userList = CHAT_DB.getMyGroupShopMemberByName(keyword);
    searchUserData();
    var userTemplate = getTemplate(TemplateURL.MAKE_ROOM_USER_LIST);
    let html = renderUser(userTemplate, userList);
    let obj = jQuery.parseHTML(html);
    $(".overlay_src_msg").html(obj);

    hasNoData = userList.length == 0;
  }

  if (hasNoData) {
    const noResultMsg = getNoResultMessage();
    overlayMessage.append(noResultMsg);
  }
};

/** UTIL */

var searchMember = function () {
  const searchInput = $('#chatMakeRoom .search_form input[type="search"]');
  searchInput.click(function (e) {
    let contactListTitle = getLocalizedString("userSearch");
    $("#makeRoomTitle").text(contactListTitle);
  });
  searchInput.keyup(function (e) {
    const enterKeyPressed = e.KeyCode == 13 || e.key == "Enter";
    const keywordEmpty = keyword == "" || keyword.length < 2;
    const keywordNotEmpty = keyword != "" && keyword.length != 0;
    //画面タイトル設定
    var keyword = searchInput.val();
    if (enterKeyPressed) {
      if (keywordNotEmpty) {
        searchInput.blur();
        return false;
      }
    } else if (keywordEmpty) {
      $(".overlay_src_msg").empty();
      return false;
    }
    CHAT_MAKE_ROOM.searchUser(keyword);
    if (enterKeyPressed) {
      searchInput.blur();
      return;
    }
  });
};

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

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

var searchUserData = function (userList) {
  userList.forEach(function (user) {
    user.profileUrl = CHAT.getProfileImgUrl(user.profileUrl);
    let findObj = CHAT.globalSelectedUserList.find(function (shopMemberId) {
      return shopMemberId == user.shopMemberId;
    });
    if (findObj) {
      user.checked = "checked";
    }
  });
};

var renderUser = function (url, userList) {
  return Mustache.render(url, {
    userList: userList,
  });
};
