ChatRoom.bindSearchUI = function () {
  console.log("peacekim:: ChatRoom.bindSearchUI");
  const chatSearchForm = $(".chat_room_src_form");
  const chatSearchInput = $('.chat_room_src_form input[type="search"]');
  const searchMessage = $("#searchMessage");

  // 検索アイコン押下イベント
  $(".nav_item_wrap .search_menu").click(function () {
    console.log(
      "peacekim:: ChatRoom.bindSearchUI .nav_item_wrap .search_menu clicked"
    );
    $(".nav_item_wrap").addClass("none");
    $(".footer-wrap").addClass("none");
    chatSearchForm.removeClass("none");
    $(".room_container").addClass("none");
    $(".overlay_src_msg").removeClass("none");
    // フィルタ表示
    $("#filter").removeClass("none");
    $("#user_list").addClass("none");

    chatSearchInput.focus();
    searchMessage.addClass("onfocus");
    $(".chat_list").removeClass("none");
  });

  $(".chat_room_src_form .cancel").click(function () {
    console.log(
      "peacekim:: ChatRoom.bindSearchUI .chat_room_src_form .cancel clicked"
    );
    $(".nav_item_wrap").removeClass("none");
    $(".footer-wrap").removeClass("none");
    chatSearchForm.addClass("none");
    $(".chat_room_src_form input").val("");
    $(".room_container").removeClass("none");
    $(".overlay_src_msg").empty();
    // ユーザーリスト表示
    $("#filter").addClass("none");
    $("#user_list").removeClass("none");

    chatSearchInput.blur();
    searchMessage.removeClass("onfocus");
    $(".chat_list").addClass("none");
    ChatRoom.scrollToBottom();
  });

  chatSearchInput.click(function () {
    console.log(
      "peacekim:: ChatRoom.bindSearchUI .chat_room_src_form input[type=search] clicked"
    );
    if (searchMessage.hasClass("onfocus")) {
      searchMessage.removeClass("onfocus");
      searchMessage.blur();
    } else {
      searchMessage.addClass("onfocus");
      searchMessage.focus();
    }
  });

  searchMessage.blur(function () {
    console.log(
      "peacekim:: ChatRoom.bindSearchUI .chat_room_src_form input[type=search] blured"
    );
    searchMessage.removeClass("onfocus");
  });

  // チャットルーム
  // メッセージ検索イベント
  chatSearchInput.keyup(function (e) {
    console.log(
      "peacekim:: ChatRoom.bindSearchUI .chat_room_src_form input[type=search] key up"
    );
    let workVal = "";
    const keyword = chatSearchInput.val();
    const enterKeyPressed = e.key == "Enter" || e.KeyCode == 13;
    if (enterKeyPressed) {
      if (keyword != "" && keyword.length != 0) {
        chatSearchInput.blur();
        return;
      }
    } else if (keyword == "" || keyword.length < 2) {
      $(".overlay_src_msg").empty();
      return;
    }
    ChatRoom.searchMessage(keyword, workVal);

    if (enterKeyPressed) {
      searchMessage.removeClass("onfocus");
      chatSearchInput.blur();
      return;
    }
  });

  // iOSキーボード変換検知用
  chatSearchInput.on("compositionend", function () {
    console.log(
      "peacekim:: ChatRoom.bindSearchUI .chat_room_src_form input[type=search] compositionend"
    );
    if (deviceInfo.isiOS()) {
      let workVal = "";
      var keyword = chatSearchInput.val();
      ChatRoom.searchMessage(keyword, workVal);
    }
  });

  ChatRoom.bindUserFilteredMessage();

  // フィルタ選択イベント
  $(document).on("click", "#filter .img_wrap", function (event) {
    console.log("peacekim:: ChatRoom.bindSearchUI .filter .img_wrap clicked");
    // チェックアイコン追加
    $(this).toggleClass("filter");
  });
};

ChatRoom.bindUserFilteredMessage = function () {
  console.log("peacekim:: ChatRoom.bindUserFilteredMessage");
  $(document).on("click", ".filter_img", function (event) {
    console.log(
      "peacekim:: ChatRoom.bindUserFilteredMessage on document click"
    );
    const keyword = $('.chat_room_src_form input[type="search"]').val();
    $(".overlay_src_msg").empty();
    let checkedUserList = ChatRoom.getCheckedUserList();
    if (keyword.length == 1) {
      return;
    }
    const messages = NativeBridgeDataSource.searchMessages(
      keyword,
      checkedUserList.join(",")
    );
    ChatRoom.appendSearchMessages(messages);
  });
};

//　メッセージ検索
ChatRoom.searchMessage = function (keyword, workVal) {
  console.log("peacekim:: ChatRoom.searchMessage");
  $(".overlay_src_msg").empty();
  let checkedUserList = ChatRoom.getCheckedUserList();
  const messages = NativeBridgeDataSource.searchMessages(
    keyword,
    checkedUserList.join(",")
  );

  ChatRoom.appendSearchMessages(messages, workVal);

  if (messages.length == 0) {
    const noResultMsg = getNoResultMessage();
    $(".overlay_src_msg").append(noResultMsg);
  }
};

ChatRoom.getCheckedUserList = function () {
  console.log("peacekim:: ChatRoom.getCheckedUserList");
  let checkedUserList = [];
  $(".img_wrap.filter").each(function (user) {
    const selectedUser = $(".img_wrap.filter")[user];
    checkedUserList.push($(selectedUser).data("user-id"));
  });
  return checkedUserList;
};
