/*
 * @copyright Copyright (c) WHMCS Limited 2023
 * @license https://www.whmcs.com/eula WHMCS EULA
 */
function paypal_acdc_init()
{
    var paymentMethod = jQuery('input[name="paymentmethod"]'),
        frm = jQuery('#frmCheckout'),
        newCcForm = jQuery('.frm-credit-card-input'),
        paymentForm = jQuery('#frmPayment'),
        adminCreditCard = jQuery('#frmCreditCardDetails');

    if (paymentMethod.length && !newCcForm.length) { // checkout
        paypal_acdc_InjectElements(function() {
            jQuery('#newCardInfo').first()
                .after(paypal_acdc_ElementsHTMLCheckout(
                    paypal_acdc.elementsId,
                    paypal_acdc.translations
                ));
        })

        var newOrExisting = jQuery('input[name="ccinfo"]'),
            selectedCard = jQuery('input[name="ccinfo"]:checked'),
            selectedPaymentMethod = jQuery('input[name="paymentmethod"]:checked').val(),
            existingCvv = jQuery('#existingCardInfo');

        if (typeof selectedPaymentMethod == 'undefined') {
            selectedPaymentMethod = jQuery('input[name="paymentmethod"]').val();
        }

        if (selectedPaymentMethod === 'paypal_acdc') {
            paypal_acdc_hideGenericFields();
            if (selectedCard.val() == 'new') {
                paypal_acdc.elementsNode.slideDown();
            }
        }

        paymentMethod.on('ifChecked', function(){
            selectedPaymentMethod = jQuery(this).val();
            if (selectedPaymentMethod === 'paypal_acdc') {
                var newOrExistingValue = jQuery('input[name="ccinfo"]:checked').val();
                paypal_acdc_hideGenericFields();
                if (newOrExistingValue == 'new') {
                    paypal_acdc.elementsNode.slideDown();
                }
            } else {
                paypal_acdc.elementsNode.hide('fast');
            }
        });
        jQuery(document).on('ifChecked', 'input[name="ccinfo"]', function() {
            selectedPaymentMethod = jQuery('input[name="paymentmethod"]:checked').val();
            if (selectedPaymentMethod !== 'paypal_acdc') {
                return;
            }
            paypal_acdc_hideGenericFields();
            if (jQuery(this).val() === 'new') {
                paypal_acdc.elementsNode.slideDown();
            } else {
                paypal_acdc.elementsNode.slideUp();
            }
        });
    } else if (paymentForm.length) { // invoice-pay

        paypal_acdc_InjectElements(function() {
            paymentForm.find('#billingAddressChoice')
                .before(paypal_acdc_ElementsHTMLInvoice(
                    paypal_acdc.elementsId,
                    paypal_acdc.translations
                ));
        });

        paymentForm.find('#inputCardCvv').closest('div.form-group').remove();
        paymentForm.off('submit', validateCreditCardInput);
        if (jQuery('input[name="ccinfo"]:checked').val() === 'new') {
            paymentForm.find('#inputCardNumber').closest('div.form-group').remove();
            paymentForm.find('#inputCardExpiry').closest('div.form-group').remove();
            paypal_acdc.elementsNode.slideDown();
        } else {
            paymentForm.find('#inputCardNumber').closest('div.form-group').remove();
            paymentForm.find('#inputCardExpiry').closest('div.form-group').remove();
        }
        jQuery('input[name="ccinfo"]').on('ifChecked', function(){
            if (jQuery(this).val() === 'new') {
                paypal_acdc.elementsNode.slideDown();
            } else {
                paypal_acdc.elementsNode.slideUp();
            }
        });
    }
}

function paypal_acdc_hideGenericFields()
{
    var frm = paypal_acdc.elementsNode.closest('form'),
        cardInputs = jQuery('#newCardInfo,.cc-details,#existingCardInfo');
    if (cardInputs.is(':visible')) {
        cardInputs.slideUp('fast', function() {
            frm.find('#cctype').removeAttr('name');
            frm.find('#inputCardCvvExisting').removeAttr('name');
            frm.find('#inputCardNumber').removeAttr('name');
            frm.find('#inputCardExpiry').removeAttr('name');
            frm.find('#inputCardCVV').removeAttr('name');
            frm.find('#inputCardCvvExisting').removeAttr('name');
        });
    }
}

function paypal_acdc_InjectElements(injector)
{
    if (paypal_acdc.elementsNode != null) {
        return paypal_acdc.elementsNode;
    }
    injector();
    paypal_acdc.elementsNode = jQuery('#' + paypal_acdc.elementsId);
    return paypal_acdc.elementsNode;
}

function paypal_acdc_ElementsHTMLCheckout(containerId, translations) {
    let saveCard = '';
    if (paypal_acdc.vaultCapable) {
        saveCard = '<div class="col-md-12">' +
            '<input type="checkbox" id="paypal-acdc-vaultCard" name="save" checked>&nbsp;' +
            '<label for="save">' + translations.saveForFutureUse + '</label>' +
            '</div>';
    }
    return '<div id="' + containerId + '" class="row" style="display: none;">' +
        '<div class="col-md-6">' +
        '<label for="paypal-acdc-CreditCard">' + translations.creditCardInput + '</label>' +
        '<div id="paypal-acdc-CreditCard"></div>' +
        '</div>' +
        '<div class="col-md-3">' +
        '<label for="paypal-acdc-ExpiryDate">' + translations.creditCardExpiry + '</label>' +
        '<div id="paypal-acdc-ExpiryDate"></div>' +
        '</div>' +
        '<div class="col-md-3">' +
        '<label for="paypal-acdc-Cvc">' + translations.creditCardCvc + '</label>' +
        '<div id="paypal-acdc-Cvc"></div>' +
        '</div>' +
        saveCard +
        '</div>';
}

function paypal_acdc_ElementsHTMLInvoice(containerId, translations)
{
    let saveCard = '';
    if (paypal_acdc.vaultCapable) {
        saveCard = '<div class="form-group row cc-billing-address">' +
            '<span class="col-sm-4 control-label">' + '&nbsp;' + '</span>' +
            '<div class="col-sm-8">' +
            '<input type="checkbox" id="paypal-acdc-vaultCard" name="save" checked>&nbsp;' +
            '<label for="save" class="control-label">' + translations.saveForFutureUse + '</label>' +
            '</div>' +
            '</div>';
    }
    return '<div id="' + containerId + '" style="display: none;">' +
        '<div class="row cc-billing-address">' +
        '<label for="paypal-acdc-CreditCard" class="col-sm-4 control-label">' +
        translations.creditCardInput + '</label>' +
        '<div class="col-sm-7">' +
        '<div id="paypal-acdc-CreditCard" aria-describedby="cc-type"></div>' +
        '</div>' +
        '<div class="col-sm-4"></div>' +
        '</div>' +
        '<div class="row cc-billing-address">' +
        '<label for="paypal-acdc-ExpiryDate" class="col-sm-4 control-label">' +
        translations.creditCardExpiry + '</label>' +
        '<div class="col-sm-3">' +
        '<div id="paypal-acdc-ExpiryDate"></div>' +
        '</div>' +
        '<div class="col-sm-6"></div>' +
        '</div>' +
        '<div class="row cc-billing-address">' +
        '<label for="paypal-acdc-Cvc" class="col-sm-4 control-label">' +
        translations.creditCardCvc + '</label>' +
        '<div class="col-sm-3">' +
        '<div id="paypal-acdc-Cvc"></div>' +
        '</div>' +
        '<div class="col-sm-4">' +
        '<div id="paypal-acdc-CvcWhere"></div>' +
        '</div>' +
        '</div>' +
        saveCard +
        '</div>' +
        '</div>';
}
