var Wick = Wick || {};
/*
  for activation sticky functionality in checkout-right-aside, please do next steps:
  1) please wrap right aside content by <div class=“right-aside-sticky”></div>;
  2) if page contains "recently viewed" component, please wrap it by <div class="footer_sticky-right-aside">
*/

Wick.StickyAside = (function () {
  var $rightAside = $('.right-aside-sticky'),
    $footer = $('.footer_sticky-right-aside'),
    $body = $('body'),
    stickyAsideTopMargin = 10,
    asideWaypointInstance,
    asideSimpleBarInstance;

  function resetAsideHeight () {
    $rightAside.css('height', '');
  }

  function calculateAsideHeight () {
    var windowHeight = $(window).height(),
      footerTopPosition = $footer[0].getBoundingClientRect().top,
      isFooterVisible = windowHeight > footerTopPosition,
      isRightAsideStuck = $rightAside.hasClass('stuck');

    if (isFooterVisible && isRightAsideStuck) {
      $rightAside.css('height', (footerTopPosition - stickyAsideTopMargin) + 'px');
    } else {
      resetAsideHeight();
    }
  }

  function destroySimpleBar () {
    var $simpleBarContent = $rightAside.find('.simplebar-content');
    if ($simpleBarContent.length) {
      $rightAside.removeAttr('data-simplebar');
      $rightAside.find('.simplebar-track').remove();
      $simpleBarContent.children().unwrap().unwrap();
      asideSimpleBarInstance.unMount();
    }
  }

  function destroyStickyAside () {
    if ($rightAside.parent('.sticky-wrapper').length) {
      asideWaypointInstance.destroy();
      resetAsideHeight();
      destroySimpleBar();
    }
  }

  function initStickyAside () {
      asideWaypointInstance = new Waypoint.Sticky({
        element: $rightAside[0],
        handler: function () {
          calculateAsideHeight();
        },
        offset: 10
      });
      asideSimpleBarInstance = new SimpleBar($rightAside[0], {
        autoHide: false
      });
  }

  function handleInitOnBreakpoint (bp) {
    if ($body.hasClass('scroll-disable')) {
      return;
    }
    if (bp === 'down-sm') {
      destroyStickyAside();
      unbindEvents();
      return;
    } else if ($rightAside.parent('.sticky-wrapper').length) {
      calculateAsideHeight();
      asideSimpleBarInstance.recalculate();
    } else {
      initStickyAside();
    }
    bindEvents();
  }

  function unbindEvents () {
    $(window).off('.calculate.height').off('.bs.modal.sticky');
  }

  function bindEvents () {
    $(window).off('.calculate.height').on('scroll.calculate.height', calculateAsideHeight);
    $(window).off('.bs.modal.sticky')
      .on('show.bs.modal.sticky', destroyStickyAside)
      .on('hide.bs.modal.sticky', function () {
        handleInitOnBreakpoint(Wick.Responsive.getCurrentBreakpoint());
      });
  }

  function init () {
    handleInitOnBreakpoint(Wick.Responsive.getCurrentBreakpoint());

    Wick.Responsive.onBreakpoint(function () {
      handleInitOnBreakpoint('down-sm');
    }, 'down-sm');
    Wick.Responsive.onBreakpoint(function () {
      handleInitOnBreakpoint('only-md');
    }, 'only-md');
    Wick.Responsive.onBreakpoint(function () {
      handleInitOnBreakpoint('up-lg');
    }, 'up-lg');
  }

  $rightAside.length && init();
})();
