var Wick = Wick || {};
Wick.ProductBanner = {
  el: {
    banners: '.card-product-banner',
    bannerInactiveClass: 'card-product-banner--inactive',
    productsWrap: '.products-list-v2',
    $products: $('.product-card'),
    attrPositionDesktop: 'data-desktop-position',
    attrPositionMobile: 'data-mobile-position',
    attrRequiredAmountDesktop: 'data-required-amount-desktop',
    attrRequiredAmountMobile: 'data-required-amount-mobile',
    hide: 'd-none'
  },
  currentBreakpoint: null,
  checkProductsInColumn() {
    Wick.ProductBanner.addInactiveClasses();
    Wick.ProductBanner.arrangeBannersInRightOrder();
    $(Wick.ProductBanner.el.banners).each((index, banner) => {
      Wick.ProductBanner.hideBannerDueLimit(banner, index);
      Wick.ProductBanner.moveBannerToSpecifiedPosition(banner);
    });
  },
  moveBannerToSpecifiedPosition(banner) {
    const $listSlots = Wick.ProductBanner.getListSlots();
    const bannerPosition = Wick.ProductBanner.getBannerPosition(banner) - 2;
    const listSlotsLength = $listSlots.length;

    if(Wick.ProductBanner.isBannerVisible(banner)) {
      if (bannerPosition <= listSlotsLength) {
        if ($listSlots[bannerPosition]) {
          Wick.ProductBanner.showBanner(banner);
          Wick.ProductBanner.removeInactiveClass(banner);
          Wick.ProductBanner.moveBanner(banner, $listSlots[bannerPosition]);
        } else {
          Wick.ProductBanner.hideBanner(banner);
          Wick.ProductBanner.coverEdgeCases(banner);
        }
      } else {
        Wick.ProductBanner.hideBanner(banner);
      }
    }
  },
  moveBanner(banner, targetElement) {
    $(banner).remove();
    $(targetElement).after(banner);
  },
  hideBanner(banner) {
    $(banner).addClass(Wick.ProductBanner.el.hide);
  },
  showBanner(banner) {
    $(banner).removeClass(Wick.ProductBanner.el.hide);
  },
  isBannerVisible(banner) {
    return !$(banner).hasClass(Wick.ProductBanner.el.hide);
  },
  hideBannerDueLimit(banner) {
    const bannerMinLimit = Wick.ProductBanner.getBannerLimit(banner) - 1;
    const $listSlots = Wick.ProductBanner.getListSlots();
    const listProductsLength = $listSlots.length;
    Wick.ProductBanner.showBanner(banner);

    if (bannerMinLimit > listProductsLength) {
      Wick.ProductBanner.hideBanner(banner);
    }
  },
  getBannerPosition(banner) {
    const isMobile = Wick.ProductBanner.isMobileResolution();
    const positionAttribute = isMobile
      ?  Wick.ProductBanner.el.attrPositionMobile
      : Wick.ProductBanner.el.attrPositionDesktop

    return banner.hasAttribute(positionAttribute)
      ? Number(banner.getAttribute(positionAttribute))
      : Number(Wick.ProductBanner.getListLength() + Wick.ProductBanner.getBannersAmount());
  },
  getBannerLimit(banner) {
    const isMobile = Wick.ProductBanner.isMobileResolution();

    const limitAttribute = isMobile
      ? Wick.ProductBanner.el.attrRequiredAmountMobile
      : Wick.ProductBanner.el.attrRequiredAmountDesktop;

    return banner.hasAttribute(limitAttribute)
      ? Number(banner.getAttribute(limitAttribute))
      : 0;
  },
  isMobileResolution() {
    return Wick.Responsive.getCurrentBreakpoint() !== 'up-lg';
  },
  getListLength() {
    const $listItems = $(Wick.ProductBanner.el.productsWrap).find('.product-card');
    return $listItems.length;
  },
  getBannersAmount() {
    return $(Wick.ProductBanner.el.banners).length;
  },
  getProductsAmount() {
    return Wick.ProductBanner.el.$products.length;
  },
  arrangeBannersInRightOrder() {
    $(Wick.ProductBanner.el.banners).sort((a, b) => {
      let bannerPositionA = Wick.ProductBanner.getBannerPosition(a);
      let bannerPositionB = Wick.ProductBanner.getBannerPosition(b);

      return bannerPositionA - bannerPositionB
    }).appendTo(Wick.ProductBanner.el.productsWrap);
  },
  addInactiveClasses() {
    $(Wick.ProductBanner.el.banners).addClass(Wick.ProductBanner.el.bannerInactiveClass);
  },
  removeInactiveClass(banner) {
    $(banner).removeClass(Wick.ProductBanner.el.bannerInactiveClass)
  },
  insertFirstBanner(banner) {
    const bannerPosition = Wick.ProductBanner.getBannerPosition(banner);

    if (bannerPosition === 1) {
      Wick.ProductBanner.showBanner(banner);
      Wick.ProductBanner.removeInactiveClass(banner);
      $(banner).remove();
      $(Wick.ProductBanner.el.productsWrap).prepend(banner);
    }
  },
  coverEdgeCases(banner) {
    Wick.ProductBanner.insertFirstBanner(banner);
  },
  getListSlots() {
    return $(Wick.ProductBanner.el.productsWrap)
      .find('.product-card, .card-product-banner:not(.card-product-banner--inactive)');
  },
  init() {
    Wick.Responsive.onResize(function () {
      if(Wick.ProductBanner.currentBreakpoint !== Wick.Responsive.getCurrentBreakpoint()) {
        Wick.ProductBanner.currentBreakpoint = Wick.Responsive.getCurrentBreakpoint()
        Wick.ProductBanner.checkProductsInColumn();
        $(window).trigger('productBannerPositionChanged');
      }
    });
  }
};

$(document).ready(function () {
  if (!Wick.ProductBanner.getBannersAmount() || !Wick.ProductBanner.getProductsAmount()) {
    return;
  }

  Wick.ProductBanner.init();
})
