// Chat Bot placement on top of footer on scrolling
window.onload = () => {
  const getScrollingPos = () => {
    let screenWidth = screen.width;
    let footerHeight = $('.footer-container').height();
    let scrollPos = $(document).height() - ($(window).scrollTop() + window.innerHeight);
    let chatBotSpacing = 0;
    let chatBotPlace = 0;
    if (screenWidth <= 991) {
      chatBotSpacing = 16;
    }
    if (scrollPos <= footerHeight) {
      chatBotPlace = footerHeight - scrollPos + chatBotSpacing - 1;
    } else {
      chatBotPlace = chatBotSpacing - 1;
    }
    $('.helpButton').css({'bottom': chatBotPlace+'px'});
    $('.dockableContainer').css({'bottom': chatBotPlace+'px'});
    $('.embeddedServiceSidebarMinimizedDefaultUI.sidebarHeader').attr({'style': 'bottom: '+ chatBotPlace+'px'+'!important'});
  }

  getWindowScroll = (footerHeight, scrollPos) => {
    console.log('footerHeight, scrollPos', footerHeight, scrollPos);
    if ( scrollPos <= footerHeight ) {
      let chatBotPlace = footerHeight - scrollPos;
      window.scrollBy(0, -(chatBotPlace / 2));
    }
  }
  
  window.onscroll = () => {
    getScrollingPos();
  }

  setTimeout(() => {getScrollingPos()}, 5000);

  setFocusOnChatWindow = () => {
    let footerHeight = $('.footer-container').height();
    let scrollPos = $(document).height() - ($(window).scrollTop() + window.innerHeight);
    setTimeout(() => {
      getScrollingPos(),
      getWindowScroll(footerHeight, scrollPos);
    }, 5000);
  }

  jQuery(document).on("click", ".helpButton", function () {
    setFocusOnChatWindow();
  });
  jQuery(document).on("click", ".embeddedServiceSidebarMinimizedDefaultUI.sidebarHeader", function () {
    setFocusOnChatWindow();
  });
};
