/*global define*/

/**
 * Sends a request for validating IBAN and BIC in the checkout process.
 * IBAN and BIC are part of the new direct debit payment called SEPA.
 *
 * @class de_epages.paymenttypes.inc.validatesepadata
 *
 * @uses jQuery
 * @uses ep
 * @uses ep.tooltip
 * @uses ep.ajax
 *
 * @since 6.17.2
 *
 */
define("de_epages/paymenttypes/inc/validatesepadata", [
    "jquery",
    "ep/ajax",

    "ep/ui/tooltip"
], function ($, ep) {
    'use strict';

    /**
     * @param {Object}  options                             The arguments of the module.
     * @param {jQuery}  options.IBANBICWarnMessageNode      The DOM node of the message which will be shown if IBAN/BIC are not valid.
     * @param {jQuery}  options.SEPAWarnMessageNode         The DOM node of the message which will be removed when the "IBANBICWarnMessage" is shown.
     * @param {jQuery}  options.accountNoNode               The DOM node of the input which contains the IBAN (former bank account number).
     * @param {jQuery}  options.bankCodeNode                The DOM node of the input which contains the BIC (former bank code number).
     * @param {jQuery}  options.nextButton                  The DOM node of the button which leads to the next checkout step.
     * @param {String}  options.buttonText                  A String which will set to the "nextButton" when IBAN/BIC are invalid.
     */
    return function (IBANBICWarnMessageNode, SEPAWarnMessageNode, accountNoNode, bankCodeNode, nextButton, buttonText) {
        nextButton.on('click', function (event) {
            var $this = $(this);

            if (!IBANBICWarnMessageNode.is(':visible')) {
                event.preventDefault();

                // check the given IBAN and BIC
                ep.ajax({
                    url: '?',
                    dataType: 'json',
                    type: 'POST',
                    data: {
                        ObjectID: ep.config.objectId,
                        ChangeAction: 'JSONValidateSEPAData',
                        BankAccountNo: accountNoNode.val(),
                        BankCode: bankCodeNode.val(),
                        ViewAction: 'JSONViewResponse'
                    }
                })
                    // IBAN and BIC are valid >> continue the checkout process
                    .done(function () {
                        $this.trigger('submit');
                    })
                    // some error is thrown
                    .fail(function (jqXHR, statusText, errorThrown) {
                        $.each(errorThrown, function (i, error) {
                            // IBAN and/or BIC are not in the expected format
                            if (error.Reason === "FORMAT_NOT_IBAN" || error.Reason === "FORMAT_NOT_BIC") {
                                // remove the sepa message to prevent the display of two warnings
                                if (SEPAWarnMessageNode.length) {
                                    SEPAWarnMessageNode.remove();
                                }

                                // show message and change text of the "next" button
                                IBANBICWarnMessageNode.removeClass('HideElement');
                                nextButton.html(buttonText);

                                // highlight IBAN if it is invalid
                                if (error.Reason === "FORMAT_NOT_IBAN") {
                                    accountNoNode.addClass('ui-warn');
                                }

                                // hightlight BIC if it is invalid
                                if (error.Reason === "FORMAT_NOT_BIC") {
                                    bankCodeNode.addClass('ui-warn');
                                }
                            // we continue the checkout if there is another problem with the request
                            } else {
                                $this.trigger('submit');
                            }
                        });
                    });
            }
        });

    };
});