var Wick = (window.Wick = window.Wick || {});

Wick.MiniBasket = (function () {
    var $el = $('.header-minicart-wrap .header-minicart__switcher');

    if (Wick.Responsive.getCurrentBreakpoint() === 'up-lg') {
        $el = $('.header-right__item .header-minicart__switcher');
    }

    var $rollover = $('#mini-basket-rollover'),
        $popup = $('#mini-basket-popup'),
        $body = $('body'),
        fixContent = 'fixed-content',
        $switcherBtn = $('.header-minicart__btn_switcher'),
        $linkBtn = $('.header-minicart__btn_link');

    if (!$el.length) {
        return;
    }
    //fix for iOS 13
    if (Wick.Responsive.isTouchDevice()) {
        $linkBtn.on('touchstart', function () {
            document.location.href = $linkBtn[0].href;
        });
    }

    var showMiniBasketLink = function () {
        $switcherBtn.hide();
        $linkBtn.show();
        $body.addClass(fixContent);
    };

    var hideMiniBasketLink = function () {
        $switcherBtn.show();
        $linkBtn.hide();
        $body.removeClass(fixContent);
    };

    var removeSwitcherButton = function () {
        $switcherBtn.remove();
        $linkBtn.show();
        $body.removeClass(fixContent);
    };

    var rollover = {
        isExist: false,
        isDisabled: false,
        self: '.popover-mini-basket_permanent',
        productTitle: '.popover-mini-basket_permanent .product-card__title',
        create: function () {
            popup.destory();

            var $popover = $el.popover({
                trigger: 'manual',
                html: true,
                placement: 'bottom',
                fallbackPlacement: 'clockwise',
                offset: '-65%',
                content: $rollover.html(),
            });

            $popover.on('inserted.bs.popover', function () {
                $(rollover.productTitle).each(function (index, value) {
                    $clamp(value, { clamp: 3 });
                });
            });

            if (Wick.Responsive.isTouchDevice()) {
                $popover
                    .on('hidden.bs.popover', hideMiniBasketLink)
                    .on('shown.bs.popover', showMiniBasketLink);
            }

            rollover.isExist = true;
        },
        _bindTouchEvents: function () {
            if (!Wick.Responsive.isTouchDevice()) {
                return;
            }

            $el.on('touchstart', function () {
                if (rollover.isDisabled) {
                    empty.show();
                    return;
                }

                if (!rollover.isExist) {
                    rollover.create();
                }
                $el.popover('show');
            });

            $(document)
                .off('touchstart.leaveBasket')
                .on('touchstart.leaveBasket', function (e) {
                    if (
                        !$('.popover').hasClass('show') ||
                        $(e.target).parents(rollover.self).length ||
                        $(e.target).closest('.header-minicart__btn').length
                    ) {
                        return;
                    }

                    $el.popover('hide');
                    $el.tooltip('hide');
                });
        },
        _bindEvents: function () {
            if (Wick.Responsive.isTouchDevice()) {
                return;
            }

            $el.on('mouseenter', function () {
                if (rollover.isDisabled) {
                    return;
                }

                if (!rollover.isExist) {
                    rollover.create();
                }

                $el.popover('show');
            }).on('mouseleave', function () {
                setTimeout(function () {
                    if (!$('.popover:hover').length) {
                        $el.popover('hide');
                    }
                }, 100);
            });

            $(document).on('mouseleave', rollover.self, function () {
                $el.popover('hide');
            });
        },
        bindEvents: function () {
            // this._bindEvents();
            // this._bindTouchEvents();
        },
        enable: function () {
            rollover.isDisabled = false;
        },
        disable: function () {
            rollover.isDisabled = true;
        },
        destory: function () {
            if (!rollover.isExist) {
                return;
            }

            $el.popover('dispose');
            rollover.isExist = false;
        },
    };

    var popup = {
        isExist: false,
        timeout: undefined,
        productTitle: '.popover-mini-basket_just-added .product-card__title',
        btnClose: '.popover-mini-basket_just-added .notification__close',
        create: function () {
            var $justAddedPopover = $el
                .popover({
                    trigger: 'manual',
                    html: true,
                    placement: 'bottom',
                    fallbackPlacement: 'clockwise',
                    offset: '-65%',
                    content: $popup.html(),
                })
                .popover('show');

            if (Wick.Responsive.isTouchDevice()) {
                $justAddedPopover.on('shown.bs.popover', hideMiniBasketLink);
            }

            popup.timeout = setTimeout(function () {
                popup.destory();
            }, 6000);

            $(popup.productTitle).each(function (index, value) {
                $clamp(value, { clamp: 3 });
            });

            $('html, body').animate({ scrollTop: 0 }, 300);

            popup.isExist = true;
        },
        bindEvents: function () {
            $(document).on('click', popup.btnClose, function () {
                popup.destory();
            });
        },
        destory: function () {
            if (!popup.isExist) {
                return;
            }

            $el.popover('dispose');
            popup.isExist = false;
            clearTimeout(popup.timeout);
        },
    };

    var empty = {
        create: function () {
            var $tooltip = $el.tooltip({
                title: 'Your basket is currently empty',
                placement: 'bottom',
                offset: '-15%',
                template:
                    '<div class="tooltip popover-mini-basket__tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>',
            });

            if (Wick.Responsive.isTouchDevice()) {
                $tooltip
                    .on('hidden.bs.tooltip', hideMiniBasketLink)
                    .on('shown.bs.tooltip', showMiniBasketLink);
            }
        },
        show: function () {
            $el.tooltip('show');
        },
        destroy: function () {
            $el.tooltip('dispose');
        },
    };

    if (!Wick.Responsive.isTouchDevice()) {
        removeSwitcherButton();
    }

    if ($rollover.length) {
        rollover.bindEvents();
        rollover.create();
    } else {
        empty.create();
    }

    if ($popup.length) {
        popup.bindEvents();
    }

    return {
        showJustAdded: function () {
            rollover.destory();
            rollover.enable();
            popup.create();
            empty.destroy();
        },
        disableRollover: function () {
            rollover.disable();
            empty.create();
        },
    };
})();
