var lastscrollvalue;
window.onscroll = function () {
  if (beforeScroll == window.scrollY) {
    return;
  }
  beforeScroll = window.scrollY;
  const beforeHeight = $(".room_container").height();
  messageCount = $(".chat_message").length;
  if ($(this).scrollTop() === 0 && messageCount >= PagingSize.MESSAGE) {
    if (!$("#chatLoader").is(":visible")) {
      let loader = $(
        '<div id="chatLoader" class="text-center"><div class="spinner-grow spinner-grow-sm" role="status" /></div>'
      );
      $("#messages").prepend(loader);
      const messageId = $($(".chat_message").get(0)).data("messageid");
      NativeBridgeDelegate.updatePreMessage(messageId);
      let messages = NativeBridgeDataSource.getMessagesWithMessageId(messageId);
      CHAT_UI.prependMessage(messages);
      loader.remove();
      var afterHeight = $(".room_container").height();
      window.scroll(0, afterHeight - beforeHeight);
    }
  }
};

$("#roomNameChangeBtn").click(function () {
  var changeRoomName = $("#roomNameInput").val();
  var trimmedRoomName = $("#roomNameInput").val().trim();
  if (trimmedRoomName.length == 0) {
    alert(getLocalizedString("error_empty_room_name"));
    return;
  }
  NativeBridgeDelegate.changeRoomName(changeRoomName);
});
