/**
 * NOTICE OF LICENSE
 *
 * This file is licenced under the Software License Agreement.
 * With the purchase or the installation of the software in your application
 * you accept the licence agreement.
 *
 * You must not modify, adapt or create derivative works of this source code
 *
 * @author    GlobalPayments
 * @copyright Since 2021 GlobalPayments
 * @license   LICENSE
 */

( function (
    $,
    globalpayments_admin_params
) {
    function GlobalPaymentsAdmin(options)
    {
        this.id = '';
        this.adminParams = options;
        this.messages = options.messages;
        this.attachEventHandlers();
    }
    GlobalPaymentsAdmin.prototype = {
        /**
         * Sanitize a string to prevent XSS attacks
         * 
         * @param {string} str
         * @returns {string}
         */
        sanitizeString: function(str) {
            if (typeof str !== 'string') {
                return '';
            }
            // Remove any potential script tags and event handlers
            return str
                .replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
                .replace(/on\w+\s*=/gi, '')
                .replace(/javascript:/gi, '')
                .replace(/data:/gi, '');
        },

        /**
         * Add important event handlers
         *
         * @returns
         */
        attachEventHandlers: function () {
            var self = this;
            $(document).ready(function () {
                self.attachEvents();
                $(document).on('click', self.getGatewayTabListSelector(), function () {
                    /**
                     * Once a new gateway has been selected, we remove the click event from the previous one
                     * and add it to the current one
                     */
                    $(document).off('click', self.getLiveModeSelector());
                    self.attachEvents();
                });
                self.setPayForOrderButton();
                self.renderInstallmentHistoryInPaymentDetails();
                
                // Auto-check credentials on page load
                self.autoCheckCredentialsOnLoad();

                 //Show approprate APM options on pageload, based on paymentInterface option
                self.togglePaymentInterfaceSettings();

                $(document).on('change', '#' + self.id + '_transactionRegion', function() {
                    $('#' + self.id + '_sandboxAccountNameDropdown').empty();
                });
            });
        },

        /**
         * Render GlobalPayments installment history details inside payment details (Admin Orders view)
         */
        renderInstallmentHistoryInPaymentDetails: function () {
            if (typeof globalpayments_order_data === 'undefined' || !globalpayments_order_data) {
                return;
            }

            var historyData = globalpayments_order_data.installmentHistory;
            if (!historyData || !historyData.message) {
                return;
            }

            var installments = historyData.installments || '';
            if (!installments || installments === 'N/A') {
                return;
            }

            var tableBody = $('table[data-role="payments-grid-table"] tbody');
            if (
                !tableBody.length
                || tableBody.find('[data-role="globalpayments-installment-note"]').length
            ) {
                return;
            }

            var message = historyData.message || '';
            var createdAt = historyData.createdAt || '';
            var createdAtText = '';

            if (createdAt) {
                var parsedDate = new Date(createdAt.replace(' ', 'T'));
                createdAtText = isNaN(parsedDate.getTime()) ? createdAt : parsedDate.toLocaleString();
            }

            var $detailsRow = tableBody.find('tr[data-role="payment-details"]').first();
            if (!$detailsRow.length) {
                return;
            }

            var $detailsCell = $detailsRow.find('td').first();
            if (!$detailsCell.length) {
                return;
            }

            var $noteLabelLine = $('<p/>', {
                class: 'mb-0 globalpayments-installment-note',
                'data-role': 'globalpayments-installment-note'
            });
            $noteLabelLine.append($('<strong/>').text('Order History Notes'));

            var parts = message.split(' | ');
            var $noteContainer = $('<div/>', { class: 'mt-2' });
            $noteContainer.append($noteLabelLine);

            var createLabelLine = function (label, value) {
                var $line = $('<p/>', { class: 'mb-0' });
                $line.append($('<strong/>').text(label + ':'));
                if (value) {
                    // Use text node to safely append value (prevents XSS)
                    $line.append(document.createTextNode(' ' + value));
                }
                return $line;
            };

            for (var i = 0; i < parts.length; i++) {
                var part = parts[i].trim();
                if (!part) {
                    continue;
                }
                var $line = $('<p/>', { class: 'mb-0' });
                var separatorIndex = part.indexOf(':');
                if (separatorIndex > -1) {
                    var label = part.slice(0, separatorIndex).trim();
                    var value = part.slice(separatorIndex + 1).trim();
                    var normalizedLabel = label.toLowerCase();
                    if (
                        normalizedLabel === 'order id'
                        && value.indexOf('TRN_') === 0
                        && globalpayments_order_data.orderId
                    ) {
                        $noteContainer.append(
                            createLabelLine(
                                'Order id',
                                String(globalpayments_order_data.orderId)
                            )
                        );
                        continue;
                    }
                    if (normalizedLabel === 'transaction id') {
                        continue;
                    }
                    $noteContainer.append(createLabelLine(label, value));
                } else {
                    $line.text(part);
                    $noteContainer.append($line);
                }
            }

            if (createdAtText) {
                var $createdLine = $('<p/>', { class: 'mb-0' });
                $createdLine.append($('<strong/>').text('Created at:'));
                // Use text node to safely append value (prevents XSS)
                $createdLine.append(document.createTextNode(' ' + createdAtText));
                $noteContainer.append($createdLine);
            }

            $detailsCell.append($noteContainer);
        },

        /**
         * Set the id for the gateway, show/hide the credentials for it and set the event for the
         * 'Live Mode' switch
         */
        attachEvents: function () {
            // Handle hash-based tab switching FIRST before setting ID
            var hash = window.location.hash;
            if (hash && $('ul.nav a[href="' + hash + '"]').length) {
                $('ul.nav a[href="' + hash + '"]').tab('show');
                window.scrollTo({top: 0});
            }

            this.setId();
            if (!this.id) {
                return;
            }
            this.toggleCredentialsSettings();
            this.toggleRequiredSettings();
            this.togglePaymentInterfaceSettings();
            this.toggleAvsCvvSettings();
            this.loadPaymentMethodTabsHash();
            this.syncAccountNameFields();
            this.attachCredentialChangeHandlers();
            $(document).on('click', this.getLiveModeSelector(), this.toggleCredentialsSettings.bind(this));
            $(document).on('click', this.getEnableSelector(), this.toggleRequiredSettings.bind(this));
            $(document).on('click', this.getCredentialsCheckButtonSelector(), this.checkApiCredentials.bind(this));
            $(document).on('change', this.getPaymentInterfaceSelector(), this.togglePaymentInterfaceSettings.bind(this));
            // AVS/CVV toggle - bind to specific select element change event
            var avsCvvSelector = '#' + this.id + '_checkAvsResult';
            $(document).off('change.globalpaymentsAdmin', avsCvvSelector).on('change.globalpaymentsAdmin', avsCvvSelector, this.toggleAvsCvvSettings.bind(this));
        },

        /**
         * Toggle AVS/CVV decline code fields visibility based on Check AVS/CVV Result value
         */
        toggleAvsCvvSettings: function () {
            var self = this;
            var activeForm = $(this.getActiveFormSelector());
            
            // Find the trigger select by ID
            var triggerId = '#' + this.id + '_checkAvsResult';
            var trigger = activeForm.find(triggerId);
            
            // Also try to find by class as fallback
            if (trigger.length === 0) {
                trigger = activeForm.find('select.avs-cvv-toggle-trigger');
            }
            
            if (trigger.length === 0) {
                return;
            }
            
            // Get the select element value (Yes = 1, No = 0)
            var isEnabled = trigger.val() === '1';
            
            // Find AVS and CVV decline code fields by class or ID and show/hide their form-groups
            // Method 1: Find by class on the multi-select wrapper div
            activeForm.find('.avs-cvv-toggle').each(function() {
                $(this).closest('.form-group').toggle(isEnabled);
            });
            
            // Method 2: Find by data attribute on multi-select wrapper
            activeForm.find('[data-field-id$="_avsDeclineCodes"], [data-field-id$="_cvvDeclineCodes"]').each(function() {
                $(this).closest('.form-group').toggle(isEnabled);
            });
            
            // Method 3: Find by field name directly (covers all cases)
            activeForm.find('[name="' + this.id + '_avsDeclineCodes"], [name="' + this.id + '_avsDeclineCodes[]"]').each(function() {
                $(this).closest('.form-group').toggle(isEnabled);
            });
            activeForm.find('[name="' + this.id + '_cvvDeclineCodes"], [name="' + this.id + '_cvvDeclineCodes[]"]').each(function() {
                $(this).closest('.form-group').toggle(isEnabled);
            });
        },

        /**
         * Attach event handlers for App Id and App Key changes to clear Account Name
         */
        attachCredentialChangeHandlers: function () {
            var self = this;

            // Store original values to detect actual changes
            var originalValues = {};

            // Live mode credential change handlers
            $(document).on('focus', '#' + this.id + '_appId', function() {
                originalValues.appId = $(this).val();
            });

            $(document).on('blur', '#' + this.id + '_appId', function() {
                var currentValue = $(this).val();
                if (originalValues.appId !== currentValue) {
                    self.clearAccountName(true);
                }
            });

            $(document).on('focus', '#' + this.id + '_appKey', function() {
                originalValues.appKey = $(this).val();
            });

            $(document).on('blur', '#' + this.id + '_appKey', function() {
                var currentValue = $(this).val();
                if (originalValues.appKey !== currentValue) {
                    self.clearAccountName(true);
                }
            });

            // Sandbox mode credential change handlers
            $(document).on('focus', '#' + this.id + '_sandboxAppId', function() {
                originalValues.sandboxAppId = $(this).val();
            });

            $(document).on('blur', '#' + this.id + '_sandboxAppId', function() {
                var currentValue = $(this).val();
                if (originalValues.sandboxAppId !== currentValue) {
                    self.clearAccountName(false);
                }
            });

            $(document).on('focus', '#' + this.id + '_sandboxAppKey', function() {
                originalValues.sandboxAppKey = $(this).val();
            });

            $(document).on('blur', '#' + this.id + '_sandboxAppKey', function() {
                var currentValue = $(this).val();
                if (originalValues.sandboxAppKey !== currentValue) {
                    self.clearAccountName(false);
                }
            });
        },

        /**
         * Clear account name field and dropdown for the specified mode
         */
        clearAccountName: function (isLiveMode) {
            if (isLiveMode) {
                // Clear live mode account name
                $('#' + this.id + '_accountName').val('').trigger('change');
                var liveDropdown = $('#' + this.id + '_accountNameDropdown');
                if (liveDropdown.length > 0) {
                    // Clear all options
                    liveDropdown.empty();
                }
            } else {
                // Clear sandbox mode account name
                $('#' + this.id + '_sandboxAccountName').val('').trigger('change');
                var sandboxDropdown = $('#' + this.id + '_sandboxAccountNameDropdown');
                if (sandboxDropdown.length > 0) {
                    // Clear all options
                    sandboxDropdown.empty();
                }
            }
        },

        loadPaymentMethodTabsHash: function() {
            // Hash-based initial tab loading is now handled in attachEvents()
            // This function now only handles click events for tab navigation

            // When a tab is clicked, add the hash to the URL.
            $('.nav-tabs a').click(function (e) {
                $(this).tab('show');
                window.location.hash = this.hash;
                window.scrollTo({top: 0});
            });
        },

        autoCheckCredentialsOnLoad: function () {
            var self = this;
            
            // Wait a bit for the page to fully load and elements to be available
            setTimeout(function () {
                self.setId();
                if (!self.id) {
                    return;
                }
                
                // Check if we have the required credentials before auto-checking
                var appId = self.getCredentialSetting('appId');
                var appKey = self.getCredentialSetting('appKey');
                
                if (appId && appKey) {
                    // Simulate the credentials check without user interaction
                    self.performCredentialsCheck();
                }
            }, 500); // 500ms delay to ensure page is fully loaded
        },

        performCredentialsCheck: function () {
            var self = this;
            var appId = this.getCredentialSetting('appId');
            var appKey = this.getCredentialSetting('appKey');

            // Remove any existing success messages
            var credentialsSuccess = $('.globalpayments-credentials-success');
            if (credentialsSuccess) {
                credentialsSuccess.remove();
            }

            if (!appId || !appKey) {
                return; // Don't proceed if credentials are missing
            }

            var transactionRegion = self.getCredentialSetting('transactionRegion');
            var dataResidency = (transactionRegion && transactionRegion.toLowerCase() === 'europe') ? 'EU' : undefined;
            var ajaxData = {
                isLiveMode: self.getLiveModeInputValue(),
                appId: appId,
                appKey: appKey
            };
            if (dataResidency) {
                ajaxData.dataResidency = dataResidency;
            }
            $.ajax({
                type: 'POST',
                url: self.adminParams.credentialsCheckUrl,
                data: ajaxData,
                showLoader: false, // Don't show loader for auto-check
            }).done(function (result) {
                if (!result.error && result.accountName && result.accountName.length > 0) {
                    self.populateAccountNameDropdown(result.accountName);
                }
            }).fail(function (error) {
                // Silently fail for auto-check - don't show error messages
            });
        },

        checkApiCredentials: function (e) {
            e.preventDefault();

            var self = this;
            var button = $(this.getCredentialsCheckButtonSelector());
            var appId = this.getCredentialSetting('appId');
            var appKey = this.getCredentialSetting('appKey');

            var credentialsSuccess = $('.globalpayments-credentials-success');
            if (credentialsSuccess) {
                credentialsSuccess.remove();
            }

            var errors = [];

            if (!appId) {
                errors.push(self.messages.appId);
            }
            if (!appKey) {
                errors.push(self.messages.appKey);
            }

            var transactionRegion = this.getCredentialSetting('transactionRegion');
            var dataResidency = (transactionRegion && transactionRegion.toLowerCase() === 'europe') ? 'EU' : undefined;
            var ajaxData = {
                isLiveMode: self.getLiveModeInputValue(),
                appId: appId,
                appKey: appKey
            };
            if (dataResidency) {
                ajaxData.dataResidency = dataResidency;
            }
            
            if (errors.length > 0) {
                alert(errors.join('\n'));

                return;
            }

            button.text(self.messages.checkingCredentials).attr('disabled', true);


            $.ajax({
                type: 'POST',
                url: self.adminParams.credentialsCheckUrl,
                data: ajaxData,
                showLoader: true,
            }).done(function (result) {
                if (result.error) {
                    alert(self.sanitizeString(result.message));
                } else {
                    // Create element safely to prevent XSS - use text() instead of HTML concatenation
                    var successDiv = $('<div>').addClass('globalpayments-credentials-success');
                    // Sanitize and use text() to safely set content (escapes HTML entities)
                    successDiv.text(self.sanitizeString(result.message));
                    successDiv.insertAfter(button);
                    // Populate the appropriate account name dropdown based on production mode
                    if (result.accountName && result.accountName.length > 0) {
                        self.populateAccountNameDropdown(result.accountName);
                    }
                }
            }).fail(function (error) {
                var errorMsg = error.responseJSON && error.responseJSON.message 
                    ? self.sanitizeString(error.responseJSON.message) 
                    : 'An error occurred';
                alert(errorMsg);
            }).always(function () {
                button.text(self.messages.credentialsCheck).attr('disabled', false);
            });
        },

        /**
         * Get the value of a credential setting.
         *
         * @param setting
         */
        getCredentialSetting: function (setting) {
            var element;
            if (this.isLiveMode()) {
                element = $('#' + this.id + '_' + setting);
            } else {
                // Handle special case for accountName in sandbox mode
                if (setting === 'accountName') {
                    element = $('#' + this.id + '_sandboxAccountName');
                } 
                else if (setting === 'transactionRegion') {
                    element = $('#' + this.id + '_transactionRegion');
                } else {
                    element = $('#' + this.id + '_sandbox' + this.capitalizeFirstLetter(setting));
                }
            }

            // Check if element exists and has a value
            if (element.length > 0 && element.val() !== null) {
                return element.val().trim();
            }

            return ''; // Return empty string if element not found or value is null
        },

        /**
         * Get account name field selectors based on current mode
         */
        getAccountNameSelectors: function () {
            var prefix = this.isLiveMode() ? 'accountName' : 'sandboxAccountName';
            return {
                dropdown: '#' + this.id + '_' + prefix + 'Dropdown',
                hidden: '#' + this.id + '_' + prefix
            };
        },

        /**
         * Populate account name dropdown with API results
         */
        populateAccountNameDropdown: function (accountNames) {
            var self = this;
            var selectors = this.getAccountNameSelectors();
            var dropdown = $(selectors.dropdown);
            var hiddenField = $(selectors.hidden);
            var savedValue = hiddenField.val();

            // Clear existing options except the first default option
            dropdown.find('option:not(:first)').remove();

            // Validate accountNames is an array
            if (!Array.isArray(accountNames)) {
                return;
            }

            // Add each returned account name as an option
            for (var i = 0; i < accountNames.length; i++) {
                // Validate account object exists and has name property
                if (!accountNames[i] || typeof accountNames[i].name !== 'string') {
                    continue;
                }
                // Sanitize the account name to prevent XSS
                var accountName = self.sanitizeString(accountNames[i].name);
                // Create option element safely using DOM methods
                var option = document.createElement('option');
                option.value = accountName;
                option.textContent = accountName;
                dropdown.append(option);
            }

            // Sanitize savedValue for safe comparison
            var sanitizedSavedValue = self.sanitizeString(savedValue || '');

            // Set the value: prioritize saved database value, then first available option
            if (sanitizedSavedValue && sanitizedSavedValue !== '' && sanitizedSavedValue !== 'Select Account') {
                // Check if option exists using safe method (not selector injection)
                var optionExists = dropdown.find('option').filter(function() {
                    return $(this).val() === sanitizedSavedValue;
                }).length > 0;
                
                if (optionExists) {
                    dropdown.val(sanitizedSavedValue);
                }
            } else if (accountNames.length > 0 && accountNames[0] && typeof accountNames[0].name === 'string') {
                var firstAccountName = self.sanitizeString(accountNames[0].name);
                dropdown.val(firstAccountName);
                hiddenField.val(firstAccountName);
            }
        },

        /**
         * Sync dropdown value with hidden field
         */
        syncAccountNameFields: function () {
            var self = this;

            // Initialize dropdown values from hidden fields on page load
            setTimeout(function() {
                self.initializeAccountNameDropdowns();
            }, 100);

            // Setup event handlers for dropdown changes
            this.setupAccountNameEventHandlers();
        },

        /**
         * Initialize dropdown values from hidden fields
         */
        initializeAccountNameDropdowns: function () {
            var modes = [
                { prefix: 'sandboxAccountName', isLive: false },
                { prefix: 'accountName', isLive: true }
            ];

            for (var i = 0; i < modes.length; i++) {
                var mode = modes[i];
                var hiddenValue = $('#' + this.id + '_' + mode.prefix).val();

                if (hiddenValue && hiddenValue !== '') {
                    var dropdown = $('#' + this.id + '_' + mode.prefix + 'Dropdown');
                    if (dropdown.find('option[value="' + hiddenValue + '"]').length > 0) {
                        dropdown.val(hiddenValue);
                    }
                }
            }
        },

        /**
         * Setup event handlers for account name dropdowns
         */
        setupAccountNameEventHandlers: function () {
            var self = this;

            // Sandbox account name dropdown
            $(document).on('change', '#' + this.id + '_sandboxAccountNameDropdown', function() {
                self.updateHiddenField($(this), '#' + self.id + '_sandboxAccountName');
            });

            // Live account name dropdown
            $(document).on('change', '#' + this.id + '_accountNameDropdown', function() {
                self.updateHiddenField($(this), '#' + self.id + '_accountName');
            });
        },

        /**
         * Update hidden field when dropdown changes
         */
        updateHiddenField: function (dropdown, hiddenFieldSelector) {
            var selectedValue = dropdown.val();
            if (selectedValue !== 'Select Account') {
                $(hiddenFieldSelector).val(selectedValue);
            }
        },

        /**
         * Capitalize the first letter of a string.
         *
         * @param string
         */
        capitalizeFirstLetter: function (string) {
            return string.charAt(0).toUpperCase() + string.slice(1);
        },

        /**
         * Checks if "Live Mode" setting is enabled
         *
         * @returns {boolean}
         */
        isLiveMode: function () {
            return this.getLiveModeInputValue() === 1;
        },

        /**
         * Checks if the payment interface is set to "hosted payment pages"
         * 
         * @returns {boolean}
         */
        isHppEnabled: function () {
            return document.querySelector(this.getPaymentInterfaceSelector())?.value === "hosted payment page";
        },

        /**
         * Toggle gateway credentials settings
         */
        toggleCredentialsSettings: function () {
            var display = this.isLiveMode();
            var activeForm = $(this.getActiveFormSelector());

            activeForm.find('.live-toggle').parents('.form-group').toggle(display);
            activeForm.find('.sandbox-toggle').parents('.form-group').toggle(!display);

            // Remove required attribute from hidden dropdown fields to prevent validation errors
            if (display) {
                // Live mode is ON - remove required from sandbox dropdown
                $('#' + this.id + '_sandboxAccountNameDropdown').prop('required', false);
            } else {
                // Live mode is OFF - remove required from live dropdown
                $('#' + this.id + '_accountNameDropdown').prop('required', false);
            }

            this.toggleRequiredSettings();
        },

        /**
         * Toggles all the Hpp radio CSS display properties
         * 
         */
        togglePaymentInterfaceSettings: function() {
            let hppSelected = this.isHppEnabled();

            let toggleHppSetting = (selecter, show) => document.querySelectorAll(selecter).forEach(function(input) {
                let parentGroup = input.closest('.form-group');
                
                if (parentGroup) {
                    parentGroup.style.display = (show ? "block": "none");
                }
            });
            toggleHppSetting('input[type="radio"][id*="hpp"]', hppSelected);
            toggleHppSetting(`input[name="${this.id}_dcc"]`, hppSelected);
            toggleHppSetting(`input[type="radio"][id*="Blik"]:not([id*="hpp"]),
                 input[type="radio"][id*="OpenBanking"]:not([id*="hpp"])`, !hppSelected);
            
            // Toggle Installments field visibility (only for Drop-in UI and Mexico currency)
            let installmentsField = document.querySelector('input[type="radio"][id*="enableInstallments"]');
            if (installmentsField) {
                let installmentsRow = installmentsField.closest('.form-group');
                if (installmentsRow) {
                    // Check if store's default currency is MXN (Mexican Peso)
                    let isMexicanCurrency = this.isStoreCurrencyMXN();
                    // Show only if: Drop-in UI is selected AND currency is MXN
                    let shouldShow = !hppSelected && isMexicanCurrency;
                    installmentsRow.style.display = shouldShow ? 'block' : 'none';
                }
            }
        },

        /**
         * Check if the store's default currency is Mexican Peso (MXN)
         * 
         * @returns {boolean}
         */
        isStoreCurrencyMXN: function() {
            // Check admin params passed from PHP
            if (this.adminParams && this.adminParams.defaultCurrency) {
                return this.adminParams.defaultCurrency === 'MXN';
            }
            
            // Fallback: check global prestashop object
            if (
                typeof prestashop !== 'undefined'
                && prestashop.currency
                && prestashop.currency.iso_code
            ) {
                return prestashop.currency.iso_code === 'MXN';
            }
            
            // If we can't determine currency, hide installments by default for safety
            // Only show if explicitly MXN
            return false;
        },

        toggleRequiredSettings: function () {
            var enabled = this.getEnableInputValue();
            var list = $('.required').closest('div').find('input, select'); // Add select elements

            list.each(function() {
                var inputName = $(this).attr('name');
                // Skip the multi-checkbox inputs. These will be validated on backend.
                if (inputName && inputName.endsWith('[]')) {
                    return;
                }

                // Check if the element and its parent form group are visible
                var isVisible = $(this).is(':visible') && $(this).closest('.form-group').is(':visible');
                
                // Don't set required=true for hidden account name dropdowns
                if (inputName && inputName.includes('accountNameDropdown')) {
                    if (!isVisible) {
                        $(this).prop('required', false);
                        return;
                    }
                }

                if (isVisible && enabled) {
                    $(this).prop('required', true);
                } else {
                    $(this).prop('required', false);
                }
            });
        },

        /**
         * Capitalize a string
         *
         * @returns {string}
         */
        capitalize: function (string) {
            return string.charAt(0).toUpperCase() + string.slice(1);
        },

        /**
         * Convenience function to get CSS selector for the "Live Mode" input
         *
         * @returns {string}
         */
        getLiveModeSelector: function () {
            return 'input[name=' + this.id + '_isProduction]';
        },

        /**
         * Convenience function to get the value of the "Live Mode" input
         *
         * @returns {number}
         */
        getLiveModeInputValue: function () {
            return Number.parseInt($('input[name=' + this.id + '_isProduction]:checked').attr('value'));
        },

        /**
         * Convenience function to get CSS selector for the "Enable" input
         *
         * @returns {string}
         */
        getEnableSelector: function () {
            return 'input[name=' + this.id + '_enabled]';
        },

        /**
         * Convenience function to get the value of the "Enable" input
         *
         * @returns {number}
         */
        getEnableInputValue: function () {
            return Number.parseInt($('input[name=' + this.id + '_enabled]:checked').attr('value'));
        },

        /**
         * Convenience function to get the value of the gateway tab list
         *
         * @returns {string}
         */
        getGatewayTabListSelector: function () {
            return '#globalPaymentsTab';
        },

        /**
         * Convenience function to get the value of the active form
         *
         * @returns {string}
         */
        getActiveFormSelector: function () {
            return '#globalPaymentsTabContent > .active';
        },

        /**
         * Convenience function to get Credentials Check button selector
         */
        getCredentialsCheckButtonSelector: function () {
            return '#' + this.id + '_credentialsCheck';
        },

        /**
         * Convenience function to get payment interface selector
         */
        getPaymentInterfaceSelector: function() {
            return '#' + this.id + '_integrationType';
        },

        /**
         * Sets the id of the current gateway
         *
         */
        setId: function () {
            this.id = $(this.getActiveFormSelector()) ? $(this.getActiveFormSelector()).attr('id') : '';
        },

        setPayForOrderButton: function () {
            $('#globalpayments-form-show').fancybox({
                'hideOnContentClick': true,
            });
            $("#globalpayments-pay-form input[type='submit']").bind('click', function() {
                return false;
            });
            $(".payment-option").bind('click', function() {
                $('#globalpayments-form-show').fancybox({
                    'hideOnContentClick': true,
                }).resize();
            });
        }
    };
    new GlobalPaymentsAdmin(globalpayments_admin_params);
}(
    /**
     * Global `jQuery` reference
     *
     * @type {any}
     */
    (window).jQuery,
    (window).globalpayments_admin_params || {}
));
