var Wick = window.Wick || {};
Wick.Wisdom = {
    el: {
        body: 'body',
        pdpPrice: '.pdp-price',
        wisdomModal: '.wisdom-modal',
        orderItem: '.order-item',
        wisdomModalFull: '#wisdom-modal',
        wisdom: '.wisdom',
        loggedIn: 'wisdom__logged-in',
        loggedOut: 'wisdom__logged-out',
        wisdomOOS: 'wisdom__oos',
        wisdomPartial: 'wisdom__partial',
        wisdomPartialAdded: 'wisdom__partial-added',
        wisdomAdded: 'wisdom__added',
        wisdomBtnAdd: '.wisdom__btn-add',
        wisdomBtn: '.wisdom-btn',
        wisdomBtnAddString: 'wisdom__btn-add',
        wisdomBtnCheck: '.wisdom__btn-check',
        textPartial: '.btn__text-partial',
        formAdd: '.wisdom__add-form',
        change: '.wisdom__change',
        changeLink: '.wisdom__link',
        changeHide: 'wisdom__change-hide',
        description: '.wisdom__description',
        descriptionPartialAdded: '.wisdom__description-partial-added',
        formAddError: 'wisdom__add-form-error',
        notificationBlock: '.wisdom__notification',
        postcodeInput: '.wisdom-postcode',
        quantityField: '.tbx_quantity',
        mcfcModal: '.wisdom-mcfc__modal',
        pdp: '.wisdom-pdp',
        BtnMcfc: '.btn-mcfc',
    },
    addForDelivery: function () {
        const context = Wick.Wisdom.getContext(this);
        const MCFC = Wick.Wisdom.isMCFC(this);
        const postcode = context.find(Wick.Wisdom.el.postcodeInput).val();

        MCFC || context.find(Wick.Wisdom.el.notificationBlock).empty();

        if (context.find(Wick.Wisdom.el.wisdom).hasClass(Wick.Wisdom.el.wisdomPartial)) {
            Wick.Wisdom.checkAvailability(context, postcode, true);
        } else {
            if (postcode.length) {
                context.find(Wick.Wisdom.el.formAdd).removeClass(Wick.Wisdom.el.formAddError);
                Wick.Wisdom.checkAvailability(context, postcode);
            } else {
                context.find(Wick.Wisdom.el.formAdd).addClass(Wick.Wisdom.el.formAddError);
            }
        }

        $(this).blur();
        Wick.Wisdom.closeModalPopup(this);
    },
    getContext: function (context) {
        if ($(context).closest(Wick.Wisdom.el.wisdom).hasClass('wisdom-pdp')) {
            return $(context).closest(Wick.Wisdom.el.pdpPrice);
        }
        if ($(context).closest(Wick.Wisdom.el.wisdom).hasClass('wisdom-plp')) {
            return $(context).closest(Wick.Wisdom.el.wisdomModal);
        }
        if ($(context).closest(Wick.Wisdom.el.wisdom).hasClass('wisdom-basket')) {
            return $(context).closest(Wick.Wisdom.el.orderItem);
        }
        if (Wick.Wisdom.isMCFC(context)) {
            return $(context).closest(Wick.Wisdom.el.mcfcModal);
        }
    },
    isMCFC: function (context) {
        return $(context).closest(Wick.Wisdom.el.mcfcModal).hasClass('show');
    },
    checkAvailability: function (context, postcode, quantityUpdated) {
        let quantity,
            notification,
            available,
            isPDP,
            isPLP,
            isMcfc;

        if (context.find(Wick.Wisdom.el.wisdom).hasClass('wisdom-pdp')) {
            quantity = +context.find(Wick.Wisdom.el.quantityField).val();
            isPDP = true;
            available = +context.find(Wick.Wisdom.el.wisdom).attr('data-available');
            notification = quantityUpdated ? `${available} added for Delivery` : null;
        }
        if (context.find(Wick.Wisdom.el.wisdom).hasClass('wisdom-plp')) {
            quantity = +context.find(Wick.Wisdom.el.quantityField).val();
            isPLP = true;
            available = +context.find(Wick.Wisdom.el.wisdom).attr('data-available');
            notification = quantityUpdated ? `${available} added for Delivery` : null;
        }
        if ($(Wick.Wisdom.el.wisdom).hasClass('wisdom-mcfc')) {
            context = $(Wick.Wisdom.el.pdpPrice);
            quantity = +context.find(Wick.Wisdom.el.quantityField).val();
            available = +context.find(Wick.Wisdom.el.wisdom).attr('data-available');
            isMcfc = true;
            notification = 'Product added for Delivery'
            Wick.Wisdom.doMcfcTransition(context, postcode);
        }

        Wick.Wisdom.showDefaultStage(context);

        if (available > 0) {
            if (available >= quantity) {
                if (isPDP || isMcfc) {
                    quantityUpdated
                        ? Wick.Wisdom.executeSuccessScenarioAfterPartialPDP(context, notification, postcode)
                        : Wick.Wisdom.executeSuccessScenarioPDP(context, notification);
                } else if (isPLP) {
                    Wick.Wisdom.executeSuccessScenarioPLP_Basket(context, postcode);
                }
            } else {
                if (isMcfc) {
                    notification = `${available} added for Delivery`
                    Wick.Wisdom.executeSuccessScenarioPDP(context, notification);
                } else {
                    Wick.Wisdom.executePartialScenario(context, available, postcode);
                }
            }
        } else {
            Wick.Wisdom.executeErrorScenario(context, postcode)
        }
    },
    executeSuccessScenarioPDP: function (context, notification) {
        Wick.Wisdom.showSuccessCase(context, notification);
        context.find(Wick.Wisdom.el.wisdom).addClass(Wick.Wisdom.el.wisdomAdded);
    },
    executeSuccessScenarioAfterPartialPDP: function (context, notification, postcode) {
        Wick.Wisdom.showSuccessCase(context, notification);
        context.find(Wick.Wisdom.el.wisdomBtnAdd).attr('disabled', 'disabled');
        context.find(Wick.Wisdom.el.change).removeClass(Wick.Wisdom.el.changeHide);
        context.find(Wick.Wisdom.el.wisdom).addClass(Wick.Wisdom.el.wisdomPartialAdded);
        context.find(Wick.Wisdom.el.descriptionPartialAdded).html(`Added to basket for delivery to: <b>${postcode}</b>`)
    },
    executeSuccessScenarioPLP_Basket: function (context, postcode) {
        let notification = `Available for delivery to <b>${postcode}</b>`
        Wick.Wisdom.showAvailableCase(context, notification);
        context.find(Wick.Wisdom.el.change).removeClass(Wick.Wisdom.el.changeHide);
        context.find(Wick.Wisdom.el.wisdom).addClass(Wick.Wisdom.el.wisdomAdded);
    },
    executePartialScenario: function (context, available, postcode) {
        context.find(Wick.Wisdom.el.quantityField).val(available);
        Wick.Wisdom.showPartialCase(context, available, postcode);
        context.find(Wick.Wisdom.el.wisdomBtnAdd).find(Wick.Wisdom.el.textPartial).text(`Add ${available} for delivery`)
        context.find(Wick.Wisdom.el.wisdom).addClass(Wick.Wisdom.el.wisdomPartial);
        context.find(Wick.Wisdom.el.change).removeClass(Wick.Wisdom.el.changeHide);
    },
    executeErrorScenario: function (context, postcode) {
        Wick.Wisdom.showErrorCase(context, postcode)
        context.find(Wick.Wisdom.el.wisdom).addClass(Wick.Wisdom.el.wisdomOOS);
        context.find(Wick.Wisdom.el.change).removeClass(Wick.Wisdom.el.changeHide);
        context.find(Wick.Wisdom.el.wisdomBtnAdd).attr('disabled', 'disabled');
    },
    showErrorCase: function (context, postcode) {
        let text = `Out of stock for delivery to <b>${postcode}</b>`;
        Wick.Wisdom.createNotification(context, 'oos', text);
    },
    showPartialCase: function (context, available, postcode) {
        let text = `Only ${available} available for Delivery to <b>${postcode}</b>`;
        Wick.Wisdom.createNotification(context, 'partial', text);
    },
    showSuccessCase: function (context, notification) {
        let text = notification || 'Product added for Delivery';
        Wick.Wisdom.createNotification(context, 'added', text);
    },
    showAvailableCase: function (context, notification) {
        let text = notification || 'Product added for Delivery';
        Wick.Wisdom.createNotification(context, 'available', text);
    },
    closeModalPopup: function (context) {
        let isPLP = $(context).closest(Wick.Wisdom.el.wisdom).hasClass('wisdom-plp');
        let isDeliveryBtn = $(context).hasClass(Wick.Wisdom.el.wisdomBtnAddString);

        if (isPLP && isDeliveryBtn) {
            $(context).closest(Wick.Wisdom.el.wisdomModalFull).modal('toggle');
            Wick.Wisdom.showDefaultStage($(context).closest(Wick.Wisdom.el.wisdomModal));
        }

    },
    doMcfcTransition: function (context, postcode) {
        $(Wick.Wisdom.el.mcfcModal).modal('toggle');
        context.find(Wick.Wisdom.el.postcodeInput).val(postcode);
        context.find(Wick.Wisdom.el.pdp).removeClass('wisdom-mcfc');
        context.find(Wick.Wisdom.el.BtnMcfc).hide();
    },
    createNotification: function (context, type, text) {
        let icon,
            className;

        switch (type) {
            case 'added':
                icon = '<span class="icon notification__icon"><span class="fas fa-check-circle"></span></span>';
                className = 'notification_success';
                break;
            case 'oos':
                icon = '<span class="icon notification__icon"><span class="fas fa-times-circle"></span></span>';
                className = 'notification_oos';
                break;
            case 'partial':
                icon = '<span class="icon notification__icon"><span class="fas fa-info-circle"></span></span>';
                className = 'notification_partial';
                break;
            case 'available':
                icon = '<span class="icon notification__icon"><span class="fas fa-check-circle"></span></span>';
                className = 'notification_available';
                break;
        }

        let template = `
            <div class="notification ${className}">
                ${icon}
                ${text}
            </div>
        `
        context.find(Wick.Wisdom.el.notificationBlock).empty();

        context.find(Wick.Wisdom.el.notificationBlock).append(template);
    },
    showDefaultStage: function (context, link) {
        let currentElement;

        if (context) {
            currentElement = context.find(Wick.Wisdom.el.wisdom);
        } else if (link) {
            currentElement = $(link).closest(Wick.Wisdom.el.wisdom);
        } else {
            currentElement = $(this).closest(Wick.Wisdom.el.wisdom);
        }

        currentElement.find(Wick.Wisdom.el.notificationBlock).empty();
        currentElement.removeClass(Wick.Wisdom.el.wisdomOOS);
        currentElement.removeClass(Wick.Wisdom.el.wisdomPartial);
        currentElement.removeClass(Wick.Wisdom.el.wisdomAdded);
        currentElement.removeClass(Wick.Wisdom.el.wisdomPartialAdded);
        currentElement.find(Wick.Wisdom.el.change).addClass(Wick.Wisdom.el.changeHide);
        currentElement.find(Wick.Wisdom.el.wisdomBtnAdd).removeAttr('disabled');

        if (currentElement.hasClass('wisdom-plp') || !context) {
            currentElement.find(Wick.Wisdom.el.postcodeInput).val('');
            currentElement.find(Wick.Wisdom.el.formAdd).removeClass(Wick.Wisdom.el.formAddError);
        }
    },
    onLinkChange: function (e) {
        e.preventDefault();
        Wick.Wisdom.showDefaultStage(null, this);
    }
};

$(document).ready(function () {
    function init() {
        if (!$('.wisdom').length || $('.basket').length) {
            return;
        }
        if (Wick.checkLocalEnv) {
            //local usage
            if (window.location.pathname.includes('registered')) {
                $('body').attr('data-is-logged-in', 'true')
                $(Wick.Wisdom.el.postcodeInput).val('RM6 4HX');
            } else {
                $('body').attr('data-is-logged-in', 'false')
            }
        }

        $(Wick.Wisdom.el.wisdomBtnAdd).on('click', Wick.Wisdom.addForDelivery);
        $(Wick.Wisdom.el.formAdd).on('submit', Wick.Wisdom.addForDelivery);
        $(Wick.Wisdom.el.wisdomBtnCheck).on('click', Wick.Wisdom.addForDelivery);
        $(Wick.Wisdom.el.changeLink).on('click', Wick.Wisdom.onLinkChange);

        if ($(Wick.Wisdom.el.body).attr('data-is-logged-in') === 'false') {
            $(Wick.Wisdom.el.wisdom).addClass(Wick.Wisdom.el.loggedOut);
        } else {
            $(Wick.Wisdom.el.wisdom).addClass(Wick.Wisdom.el.loggedIn);
            $(Wick.Wisdom.el.wisdomBtn).on('click', function () {
                let targetSelector = $(this).attr('data-target');
                Wick.Wisdom.checkAvailability($(targetSelector), $(targetSelector).find(Wick.Wisdom.el.postcodeInput).val())
            });
        }
    }

    init();
});
