import {bannerHeight, scrollDelay, resetCalculator} from "./calculator.js";
import {checkTotal, tiObj} from "./generic.js";

const $step1Next = $('.ti__step1 .ti__next .btn'),
    $step1Return = $('.ti__step1-return'),
    $step2Return = $('.ti__step2-return'),
    costRemovalStr = '.ti__cost_removal',
    tiCostInvalidStr = 'ti__cost-invalid',
    tiCostInvalidStr2 = 'ti__cost-invalid-2',
    tiCostOptions = '.ti__cost_options',
    tiCostLeveling = '.ti__cost_levelling',
    tiCostPostCodeStr = '.ti__cost_postcode';

$(tiCostPostCodeStr).find('input').on('input', function () {
    if ($(this).val().length > 4) {
        $(tiCostPostCodeStr).removeClass(tiCostInvalidStr);
    }
});

$step1Next.on('click', function () {
    if ($(this).closest('.fi').length) {
        let context = $(this).closest('.fi');
        onStep1(context);
    } else if ($(this).closest('.ti').length) {
        let context = $(this).closest('.ti');
        onStep1(context);
    }
});

$step1Return.click(function (e) {
    e.preventDefault();
    returnToStep1();
});

$step2Return.click(function (e) {
    e.preventDefault();
    returnToStep2();
});

function goToStep2(context) {
    context.find('.ti__step1').addClass('ti__step1-inactive');
    context.find('.ti__step2').addClass('ti__step2-active');
    context.find('.ti__step1 .ti__header').addClass('ti__header-inactive');
    context.find('.ti__step2 .ti__header').removeClass('ti__header-inactive');

    if (window.innerWidth > 767) {
        $('html, body').animate({scrollTop: $('#info-tile-estimation .ti__step1')[0].offsetTop - bannerHeight}, scrollDelay);
    } else {
        $('html, body').animate({scrollTop: $('#info-tile-estimation .ti__step2')[0].offsetTop - bannerHeight}, scrollDelay);
    }
}

function onStep1(context) {

    if (checkTotal(context) === true) {
        context.find('.ti__floors').removeClass('ti__floors-invalid');
        goToStep2(context);
    } else {
        console.log(context);
        if(context.hasClass('fi')) {
            tiObj.system.notification1 = Wick.Notification.show({
                text: `You need to add a floor area or enter a total area of at least <span class="ti__warning-value">${tiObj.measure.minMeasure}</span> square <span class="ti__header-unit">${tiObj.measure.measure}</span> to continue to the next step of your estimate`,
                container: context.find('.ti__step').find('.ti__error'),
                type: 'error',
                noScrolling: true,
            });
        } else if(context.hasClass('ti')) {
            tiObj.system.notification1 = Wick.Notification.show({
                text: `You need to add a wall/floor area or enter a total area of at least <span class="ti__warning-value">${tiObj.measure.minMeasure}</span> square <span class="ti__header-unit">${tiObj.measure.measure}</span> to continue to the next step of your estimate`,
                container: context.find('.ti__step').find('.ti__error'),
                type: 'error',
                noScrolling: true,
            });
        }


        context.find('.ti__floors').addClass('ti__floors-invalid');
    }
}

function returnToStep1() {
    $('.ti__step1').removeClass('ti__step1-inactive');
    $('.ti__step2').removeClass('ti__step2-active ti__step2-inactive');
    $('.ti__step3').removeClass('ti__step3-active');
    $('.ti__step .ti__header').addClass('ti__header-inactive');
    $('.ti__step1 .ti__header').removeClass('ti__header-inactive');

    $('html, body').animate({scrollTop: $('#info-tile-estimation .ti__step1')[0].offsetTop - bannerHeight}, scrollDelay);
}

function returnToStep2() {
    $('.ti__step2').removeClass('ti__step2-inactive').addClass('ti__step2-active');
    $('.ti__step3').removeClass('ti__step3-active');
    $('.ti__step .ti__header').addClass('ti__header-inactive');
    $('.ti__step2 .ti__header').removeClass('ti__header-inactive');

    $('html, body').animate({scrollTop: $('.ti__step2').offset().top - bannerHeight}, scrollDelay);
}

function goToStep3() {
    $('.ti__step2').addClass('ti__step2-inactive').removeClass('ti__step2-active');
    $('.ti__step3').addClass('ti__step3-active');
    $('.ti__step2 .ti__header').addClass('ti__header-inactive');
    $('.ti__step3 .ti__header').removeClass('ti__header-inactive');

    $('html, body').animate({scrollTop: $('.ti__step3')[0].offsetTop - bannerHeight}, scrollDelay);
}

function resetInvalidMsg(context) {
    if( $(context).closest(costRemovalStr).length) {
        $(context).closest(costRemovalStr).removeClass(tiCostInvalidStr);
    }

    if( $(context).closest(tiCostOptions).length) {
        $(context).closest(tiCostOptions).removeClass(tiCostInvalidStr);
    }

    if( $(context).closest(tiCostLeveling).length) {
        $(context).closest(tiCostLeveling).removeClass(tiCostInvalidStr);
    }

    if( $(context).closest(costRemovalStr).length) {
        $(context).closest(costRemovalStr).removeClass(tiCostInvalidStr2);
    }

    if (tiObj.system.notification2) {
        tiObj.system.notification2.hide();
    }
}

$('.ti__new-link').click(function (e) {
    e.preventDefault();
    let context;

    if ($(this).closest('.fi').length) {
        context = $(this).closest('.fi');
    } else if ($(this).closest('.ti').length) {
        context = $(this).closest('.ti');
    }

    returnToStep1();
    resetCalculator(context);

});

export {goToStep2, onStep1, returnToStep1, returnToStep2, goToStep3, resetInvalidMsg}
