window.Wick = window.Wick || {};
window.Wick.BillieData = window.Wick.BillieData || {};
window.Wick.BillieData.billiePaymentTerms = ['30', '60', '90'];
window.INIT_KLARNA_PAYMENT = window.INIT_KLARNA_PAYMENT || 'INIT_KLARNA_PAYMENT';
window.INIT_BILLIE_PAYMENT = window.INIT_BILLIE_PAYMENT || 'INIT_BILLIE_PAYMENT';

Wick.PaymentDetails = (function () {
    var $btnEnterDetails = $('.btn-enter-details'),
        isGuestPage = $('.page_checkout-payment-details_guest').length;

    const $billieInfo = $('.checkout-payment-details__billie .billie-info');
    const $klarnaInfo = $('.checkout-payment-details__klarna .klarna-info');

    function bindEvents() {
        $btnEnterDetails.on('click', function (e) {
            e.preventDefault();
            var $scope = $(this).closest('form');
            if (!$scope.length) $scope = $(this).closest('.billing-address');
            if (!$scope.length) $scope = $('.billing-address');
            var nameVal = ($scope.find('#card-name').val() || '').trim();
            var finderVal = ($scope.find('#first-line-address').val() || '').trim();
            var manualLine1 = ($scope.find('#address-line-1').val() || '').trim();
            var manualTown = ($scope.find('#town-city').val() || '').trim();
            var manualPost = ($scope.find('#postcode_additional').val() || '').trim();
            var addressFinderOk = finderVal.length > 0;
            var addressManualOk = manualLine1 && manualTown && manualPost;

            if (nameVal && (addressFinderOk || addressManualOk)) {
                var $details = $('.checkout-payment-details__card-details');
                $details.removeClass('checkout-payment-details__card-details_hidden');
                $('html, body').animate({ scrollTop: $details.offset().top - 15 }, 700);
                return;
            }

            var formEl = $scope.closest('form').get(0) || $scope.get(0);
            if (formEl && Wick.Forms && Wick.Forms.showValidationErrors) {
                Wick.Forms.showValidationErrors(formEl);
            }
        });

        $('.checkout-payment-details__card-details').on('submit', function () {
            var $form = $(this);
            if (
                $form.find('#card-type').val() &&
                $form.find('#card-name1').val().length &&
                $form.find('#card-number').val().length &&
                $form.find('#month').val() &&
                $form.find('#year').val() &&
                $form.find('#security-code').val()
            ) {
                window.location.href = isGuestPage
                    ? './page_checkout_confirmation_guest.html'
                    : './page_checkout_confirmation.html';
                return false;
            }
            Wick.Forms.showValidationErrors(this);
            return false;
        });

        $(window).on(window.INIT_BILLIE_PAYMENT, function (e) {
            const detail = e.detail || (e.originalEvent && e.originalEvent.detail) || {};

            if (detail && detail.resolve) {
                $billieInfo.show();
                detail.resolve();
            }
        });

        $(window).on(window.INIT_KLARNA_PAYMENT, function (e) {
            const detail = e.detail || (e.originalEvent && e.originalEvent.detail) || {};

            if (detail && detail.resolve) {
                $klarnaInfo.show();
                detail.resolve();
            }
        });

        $(document).on('change', 'input[name="repaymentTerms"]', function () {
            const hasSelection = $('input[name="repaymentTerms"]:checked').length > 0;

            if (hasSelection) {
                $billieInfo.show();
            }
        });
    }

    bindEvents();
})();
