import {tiObj} from "./generic.js"
import {} from "./pdp-generic.js"
import {} from "./calculator.js"
import {
    goToStep3,
    resetInvalidMsg
} from "./installation.js"

const step2Next = $('.ti__step2 .ti__next .btn'),
    step3Next = $('.ti__step3 .ti__next .btn'),
    optionsItems = $('.ti__cost_options-list input'),
    removalRadioLevelling = $('.ti__cost_levelling-radio input'),
    checkBox2 = $('.choose__callback input.form-toggler'),
    formDetails = $('.form-details'),
    innerRow = $('.form-details .form-row'),
    finalRadio = $('.ti__choose .form-toggler'),
    removalArea = $('.ti__cost_removal'),
    removalRadio = $('.ti__cost_removal-radio input'),
    concreteCbx = $('#concrete'),
    levellingBlock = $('.ti__cost_levelling'),
    disposalInput = $('.ti__cost_removal-input input');

checkBox2.change(function () {
    if ($(this).hasClass('form-toggler-on')) {
        formDetails.addClass('form-details_open');
    } else {
        formDetails.removeClass('form-details_open');
    }
});

finalRadio.change(function () {
    $(this).attr('checked', 'checked');
    finalRadio.not($(this)).removeAttr('checked');
    tiObj.userInfo.finalChoose = $(this).siblings('span').text();
});

removalRadio.change(function () {
    resetInvalidMsg(this);
    $(this).attr('checked', 'checked');
    if ($(this).siblings('span').text() === 'Yes') {
        $(removalArea).addClass('ti__cost_removal-yes');
    } else {
        $(removalArea).removeClass('ti__cost_removal-yes');
    }
    removalRadio.not($(this)).removeAttr('checked');
});

removalRadioLevelling.change(function () {
    resetInvalidMsg(this);
    $(this).attr('checked', 'checked');
    removalRadioLevelling.not($(this)).removeAttr('checked');
});

optionsItems.change(function () {
    resetInvalidMsg(this);

    if ($(optionsItems[optionsItems.length - 1]).attr('checked') === 'checked') {
        $(optionsItems[optionsItems.length - 1]).click();
    }
    if ($(optionsItems[optionsItems.length - 1])[0] === $(this)[0]) {
        let y = optionsItems.filter(function (item) {
            return item < optionsItems.length - 1;
        }).filter(function () {
            return $(this).attr('checked') === 'checked';
        });
        y.click();
    }

    if ($(this).attr('checked') === 'checked') {
        $(this).removeAttr('checked');
    } else {
        $(this).attr('checked', 'checked');
    }
});

concreteCbx.on('click', function () {
    levellingBlock.toggleClass('ti__cost_levelling_open');
});

step2Next.on('click', function () {
    let optionsSelected = optionsItems.filter(function () {
        return $(this).attr('checked') === 'checked';
    });
    let optionsBlock = $('.ti__cost_options');
    let radioChose = removalRadio.filter(function () {
        return $(this).attr('checked') === 'checked';
    });
    let radioLevelling = removalRadioLevelling.filter(function () {
        return $(this).attr('checked') === 'checked';
    });
    let radioBlock = $('.ti__cost_removal');
    let radioBlockLevelling = $('.ti__cost_levelling');
    let postCodeValue = $('.ti__cost_postcode input').val();
    let postCodeBlock = $('.ti__cost_postcode');

    if (postCodeValue.length > 4 && radioChose.length && optionsSelected.length && isLevellingBlockOpen(radioLevelling.length)) {
        if (tiObj.system.notification2) {
            tiObj.system.notification2.hide();
        }
        optionsBlock.removeClass('ti__cost-invalid');
        radioBlock.removeClass('ti__cost-invalid');
        radioBlockLevelling.removeClass('ti__cost-invalid');
        postCodeBlock.removeClass('ti__cost-invalid');
        tiObj.userInfo.tilingOptions = [];
        optionsSelected.map(function () {
            tiObj.userInfo.tilingOptions.push($(this).siblings('span').text());
        });
        tiObj.userInfo.disposal = radioChose.siblings('span').text() === 'Yes';
        tiObj.userInfo.disposalArea = +disposalInput.val();
        tiObj.userInfo.postCode = postCodeValue;
        goToStep3();
    } else {
        tiObj.system.notification2 = Wick.Notification.show({
            text: 'Please answer the questions above before continuing.',
            container: $(this).parents('.ti__step').find('.ti__error'),
            type: 'error',
            noScrolling: true,
            //temporary: true,
            //closeBtn: true,
            //delay: 1000
        });
        optionsBlock.removeClass('ti__cost-invalid');
        radioBlock.removeClass('ti__cost-invalid');
        radioBlockLevelling.removeClass('ti__cost-invalid');
        postCodeBlock.removeClass('ti__cost-invalid');

        if (!optionsSelected.length) {
            optionsBlock.addClass('ti__cost-invalid');
        }
        if (!radioChose.length) {
            radioBlock.addClass('ti__cost-invalid');
        }
        if (!(postCodeValue.length > 4)) {
            postCodeBlock.addClass('ti__cost-invalid');
        }
        if (!radioLevelling.length) {
            radioBlockLevelling.addClass('ti__cost-invalid');
        }
    }
});

step3Next.on('click', function () {
    let chooseCallback = $(".choose__callback");
    let contactInfo = [];


    if (formDetails.hasClass('form-details_open')) {
        innerRow.each(function () {
            if ($(this).find('input').val() === "") {
                $(this).find(".form-row__error").css("display", "block");
                $(this).find('input').css("border", "1px solid #cf000f");
                contactInfo.push(0)
            } else {
                $(this).find(".form-row__error").css("display", "none");
                $(this).find('input').css("border", "1px solid #ccc");
                contactInfo.push(1)
            }
        })

    }

    function checkContactInfo() {
        if (formDetails.hasClass('form-details_open')) {
            return contactInfo.every((item) => item !== 0);
        } else {
            return true;
        }
    }

    let hasContactInfo = checkContactInfo();

    if (tiObj.userInfo.finalChoose && hasContactInfo) {
        if (tiObj.system.notification3) {
            tiObj.system.notification3.hide();
        }
        chooseCallback.removeClass('red-border');

        if (tiObj.userInfo.finalChoose == 'Call me to discuss installation') {
            $('#modal-thank-you').modal();
        }
    } else {
        tiObj.system.notification3 = Wick.Notification.show({
            text: 'Please answer the questions above before continuing.',
            container: $(this).parents('.ti__step').find('.ti__error'),
            type: 'error',
            noScrolling: true,
        });
        chooseCallback.addClass('red-border');
    }
});

disposalInput.change(function () {
    $('.ti__cost_removal-value').text($(this).val());
});

function isLevellingBlockOpen(y) {
    let x = $('.ti__cost_levelling_open').length;

    if (x) {
        if (y) {
            return true;
        } else {
            return false;
        }
    } else {
        return true;
    }
}
