var originalOpenPopup = openPopup;

openPopup = function () {
    var method_radio = jQuery('#payment_method_trustpay_gateway');
    var wire_radio = jQuery('#payment_method_trustpay_gateway_wire');
    var card_radio = jQuery('#payment_method_trustpay_gateway_card');
    
    var eps_radio = jQuery('#payment_method_trustpay_gateway_eps');
    var giropay_radio = jQuery('#payment_method_trustpay_gateway_giropay');
    var ideal_radio = jQuery('#payment_method_trustpay_gateway_ideal');
    var blik_radio = jQuery('#payment_method_trustpay_gateway_blik');
    var openbanking_radio = jQuery('#payment_method_trustpay_gateway_openbanking');
    var tpay_radio = jQuery('#payment_method_trustpay_gateway_tpay');
    var wechatpay_radio = jQuery('#payment_method_trustpay_gateway_wechatpay');
    var mybank_radio = jQuery('#payment_method_trustpay_gateway_mybank');
    var bancontact_radio = jQuery('#payment_method_trustpay_gateway_bancontact');
    var satispay_radio = jQuery('#payment_method_trustpay_gateway_satispay');
    var sofort_radio = jQuery('#payment_method_trustpay_gateway_sofort');
    var aircashpay_radio = jQuery('#payment_method_trustpay_gateway_aircashpay');
    var paypal_radio = jQuery('#payment_method_trustpay_gateway_paypal');
    var paytrail_radio = jQuery('#payment_method_trustpay_gateway_paytrail');
    var trustly_radio = jQuery('#payment_method_trustpay_gateway_trustly');
    var paysafecard_radio = jQuery('#payment_method_trustpay_gateway_paysafecard');
    var cashtocode_radio = jQuery('#payment_method_trustpay_gateway_cashtocode');
    var payconiq_radio = jQuery('#payment_method_trustpay_gateway_payconiq');
    var multibanco_radio = jQuery('#payment_method_trustpay_gateway_multibanco');
    var nasplatkytb_radio = jQuery('#payment_method_trustpay_gateway_nasplatkytb');
    var skippay_radio = jQuery('#payment_method_trustpay_gateway_skippay');
    var sepact_radio = jQuery('#payment_method_trustpay_gateway_sepact');
    
    var type;
    var form = jQuery('form.checkout');
    var iframe = jQuery('#TrustPayFrame');

    if (method_radio.is(':checked') && (wire_radio.is(':checked') || card_radio.is(':checked') || eps_radio.is(':checked')
            || giropay_radio.is(':checked') || ideal_radio.is(':checked') || blik_radio.is(':checked') || tpay_radio.is(':checked')
            || openbanking_radio.is(':checked')
            || wechatpay_radio.is(':checked') || mybank_radio.is(':checked') || bancontact_radio.is(':checked')
            || satispay_radio.is(':checked') || sofort_radio.is(':checked')
            || aircashpay_radio.is(':checked') || paypal_radio.is(':checked')
            || paytrail_radio.is(':checked') || trustly_radio.is(':checked')  || paysafecard_radio.is(':checked')
            || cashtocode_radio.is(':checked')
            || payconiq_radio.is(':checked')
            || multibanco_radio.is(':checked')
            || nasplatkytb_radio.is(':checked')
            || skippay_radio.is(':checked')
            || sepact_radio.is(':checked')
    )) {
        if (wire_radio.is(':checked')) {
            type = wire_radio.val();
        } else if(eps_radio.is(':checked')) { 
            type = eps_radio.val();
        } else if(giropay_radio.is(':checked')) { 
            type = giropay_radio.val();
        } else if(ideal_radio.is(':checked')) { 
            type = ideal_radio.val();
        } else if(blik_radio.is(':checked')) { 
            type = blik_radio.val();
        } else if(openbanking_radio.is(':checked')) { 
            type = openbanking_radio.val();
        } else if(tpay_radio.is(':checked')) { 
            type = tpay_radio.val();
        } else if(wechatpay_radio.is(':checked')) { 
            type = wechatpay_radio.val();
        } else if(mybank_radio.is(':checked')) { 
            type = mybank_radio.val();
        } else if(bancontact_radio.is(':checked')) { 
            type = bancontact_radio.val();
        } else if(satispay_radio.is(':checked')) { 
            type = satispay_radio.val();
        } else if(sofort_radio.is(':checked')) { 
            type = sofort_radio.val();
        } else if(aircashpay_radio.is(':checked')) { 
            type = aircashpay_radio.val();
        } else if(paypal_radio.is(':checked')) { 
            type = paypal_radio.val();
        } else if(paytrail_radio.is(':checked')) { 
            type = paytrail_radio.val();
        } else if(trustly_radio.is(':checked')) { 
            type = trustly_radio.val();
        } else if(paysafecard_radio.is(':checked')) { 
            type = paysafecard_radio.val();
        } else if(cashtocode_radio.is(':checked')) { 
            type = cashtocode_radio.val();
        } else if(payconiq_radio.is(':checked')) { 
            type = payconiq_radio.val();
        } else if(multibanco_radio.is(':checked')) { 
            type = multibanco_radio.val();
        } else if(nasplatkytb_radio.is(':checked')) { 
            type = nasplatkytb_radio.val();
        } else if(skippay_radio.is(':checked')) { 
            type = skippay_radio.val();
        } else if(sepact_radio.is(':checked')) { 
            type = sepact_radio.val();
        } else {
            type = card_radio.val();
        }
    }

    iframe.on('load', function () {
        try {
            originalOpenPopup();
        }
        catch (err) {
            console.error(err);
        }
    });

    form.addClass('processing');

    jQuery.ajax({
        type: 'POST',
        url: wc_checkout_params.checkout_url + '&get_trustpay_type_url=' + type,
        data: form.serialize(),
        dataType: 'json',
        async: false,
        success: function (result) {
            form.removeClass('processing');

            if ('success' === result.result && result.redirect) {
                if(typeof(isRedirect) == "undefined") {
                    
                    // redirect aj ked je nastaveny iframe
                    if(type == 'eps' || type == 'giropay' || type == 'bancontact' || type == 'sofort' || type == 'paysafecard'
                            || type == 'cashtocode' || type == 'nasplatkytb' || type == 'skippay'
			    || type == 'paypal' || type == 'trustly'
                    ) {
                        redirectPopup(result.redirect);
                    }
                    else {
                        iframe.attr('src', result.redirect);
                    }
                    
                }
                else {
                    redirectPopup(result.redirect);
                }
            } else {
                jQuery('.woocommerce-NoticeGroup-checkout, .woocommerce-error, .woocommerce-message').remove();

                var error_message;

                if (result.messages) {
                    error_message = result.messages;
                } else {
                    error_message = '<div class="woocommerce-error">' + wc_checkout_params.i18n_checkout_error + '</div>';
                }

                form.prepend('<div class="woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout">' +
                    error_message +
                    '</div>');
                jQuery.scroll_to_notices(form);
            }
        }
    });
}


function onMessage(event) {
    var data = event.data;

    if (typeof (window[data.func]) === "function") {
        window[data.func].call(null, data.message);
    }
    else if (data === "popupLoaded") {
        if (onTrustPayPopupLoaded) {
            onTrustPayPopupLoaded();
        }
    }
}



// CashToCode handler
function cashToCode() {
    
    var country = jQuery('#billing_country').val();
    var currStr = jQuery('.order-total bdi').html();
    var amount = currStr.substring(currStr.indexOf('</span>')+7);

    console.log('amount: '+amount);

    var table = {
        'AT':[9.99, 10, 10.00, 12.99, 14.99, 17.99, 19.99, 22.99, 24.99, 25.00, 27.99, 29.99,
            34.99, 39.99, 44.99, 49.99, 50.00, 54.99, 59.99, 64.99, 69.99, 74.99, 79.99,
            84.99, 89.99, 94.99, 99.99, 100.00, 200.00, 300.00, 400.00, 500.00
        ],
        'DE':[9.99, 10, 10.00, 12.99, 14.99, 17.99, 19.99, 22.99, 24.99, 25.00, 27.99, 29.99,
            34.99, 39.99, 44.99, 49.99, 50.00, 54.99, 59.99, 64.99, 69.99, 74.99, 79.99,
            84.99, 89.99, 94.99, 99.99, 100.00, 200.00, 300.00, 400.00
        ],
        'IT':[9.99, 10, 10.00, 12.99, 14.99, 17.99, 19.99, 22.99, 24.99, 25.00, 27.99, 29.99,
            34.99, 39.99, 44.99, 49.99, 50.00, 54.99, 59.99, 64.99, 69.99, 74.99, 79.99,
            84.99, 89.99, 94.99, 99.99, 100.00, 150.00, 200.00, 300.00, 400.00, 500.00
        ],
        'IE':[9.99, 10, 10.00, 12.99, 14.99, 17.99, 19.99, 22.99, 24.99, 25.00, 27.99, 29.99,
            34.99, 39.99, 44.99, 49.99, 50.00, 54.99, 59.99, 64.99, 69.99, 74.99, 79.99,
            84.99, 89.99, 94.99, 99.99, 100.00, 150.00, 200.00, 300.00, 400.00, 500.00
        ]
    };

    var show = false;

    if(typeof(table[country]) != 'undefined' && table[country].includes(amount)) {
        show = true;
    } else if(country == 'DE' && amount >= 9.99 && amount <= 999.99) {
        show = true;
    } else if(country == 'IE' && amount <= 10.00 && amount <= 800.00) {
        show = true;
    }

    if(show) {
        jQuery('#CashToCode').show();
    }
    else {
        jQuery('#CashToCode').hide();
    } 
}

jQuery('#billing_country').change(function(){
    cashToCode();
});

jQuery( document ).ready(function() {
  cashToCode();
});