jQuery(function ($) {
	'use strict';

	/**
	 * Object to handle Verifone admin functions.
	 */
	var wc_verifone_admin = {

		/**
		 * Initialize.
		 */
		init: function () {

			// Payment methods selector
			$(document).ready(function () {
				$('#woocommerce_verifone_payment_methods').selectWoo({
					placeholder: window.wp.i18n.__('Select payment methods in use', 'wc-verifone')
				});

				var selectAllLink = $('#verifone-activate-all-payment-methods-trigger');
				selectAllLink.on('click', function (event) {
					event.preventDefault();
					var selectWooField = $('#woocommerce_verifone_payment_methods');
					selectWooField.find('option').prop('selected', true);
					selectWooField.trigger('change');
				});
			});

			// Hide advanced settings by default
			$(document).ready(function () {
				$('#woocommerce_verifone_section_advanced_settings').toggle();
				$('#woocommerce_verifone_section_advanced_settings').nextUntil('p.submit').toggle();
			});

			// Toggle advanced settings
			$(document.body).on('click', '#verifone-section-advanced-settings-trigger', function (e) {
				e.preventDefault();
				$('#woocommerce_verifone_section_advanced_settings').toggle();
				$('#woocommerce_verifone_section_advanced_settings').nextUntil('p.submit').toggle();

				// Get the new toggle text from data-alt attribute and store the current text in data-alt
				var currentText = $(this).text();
				var altText = $(this).data('alt');
				$(this).text(altText);
				$(this).data('alt', currentText);
			});

			// Toggle between merchant_agreement_code and merchant_agreement_code_test based on is_live_mode
			$(document).ready(function () {
				// Initial state
				var isLiveMode = $('#woocommerce_verifone_is_live_mode').val();
				if (isLiveMode === '1') {
					// Production mode
					$('#woocommerce_verifone_merchant_agreement_code').closest('tr').show();
					$('#woocommerce_verifone_merchant_agreement_code_test').closest('tr').hide();
				} else {
					// Test mode
					$('#woocommerce_verifone_merchant_agreement_code').closest('tr').hide();
					$('#woocommerce_verifone_merchant_agreement_code_test').closest('tr').show();
				}

				// Reset link listener
				$('#verifone-reset-settings-trigger').on('click', function (e) {
					e.preventDefault();

					// Ask for confirmation
					const confirmation = confirm($(this).data('confirm'));
					if (!confirmation) {
						return;
					}

					// Proceed with the reset action by going to the reset URL
					window.location.href = $(this).attr('href');
				});

				// Mode listener
				$('#woocommerce_verifone_is_live_mode').on('change', function () {
					var isLiveMode = $(this).val();
					if (isLiveMode === '1') {
						// Production mode
						$('#woocommerce_verifone_merchant_agreement_code').closest('tr').show();
						$('#woocommerce_verifone_merchant_agreement_code_test').closest('tr').hide();
					} else {
						// Test mode
						$('#woocommerce_verifone_merchant_agreement_code').closest('tr').hide();
						$('#woocommerce_verifone_merchant_agreement_code_test').closest('tr').show();
					}
				});
			});

			// Refresh payment methods
			$(document.body).on('click', '#verifone-refresh-payment-methods-trigger', function (e) {
				e.stopPropagation();

				var data = {
					'action': 'verifone_refresh_payment_methods',
					'nonce': woocommerceVerifone.nonce,
				};

				jQuery.post(ajaxurl, data, function (response) {
					if (response.code === 200) {
						$('#verifone-payment-methods-list').html(response.methods);
						$('#verifone-payment-methods-last-updated').html(response.last_updated);
					} else {
						alert(response.data);
					}
				});
			});

			// Onboarding
			// Copy the code to actual settings fields on input
			$(document).on('input', '.verifone-bluecommerce-onboarding input[name=woocommerce_verifone_merchant_agreement]', function () {
				$('#woocommerce_verifone_merchant_agreement_code').val($(this).val());
				$('#woocommerce_verifone_merchant_agreement_code_test').val($(this).val());
			});

			// Submit the main settings form on submit
			$(document).on('click', '.verifone-bluecommerce-onboarding input[type=submit]', function (e) {
				e.preventDefault();
				$('#mainform button[type=submit]').click();
			});

			// Remove tcsonboarding parameter from url if present
			if (window.location.href.indexOf("tcsonboarding") > -1) {
				var url = new URL(window.location.href);
				url.searchParams.delete("tcsonboarding");
				window.history.replaceState({}, document.title, url.toString());
			}

			// Site health info
			// if body has class site-health
			if ($('body').hasClass('site-health')) {
				console.log('Site health page detected');
				// Find div.health-check-accordion-block-wc-verifone and td which starts with -----BEGIN PUBLIC KEY-----
				$('div.health-check-accordion-block-wc-verifone td:contains("-----BEGIN PUBLIC KEY-----")').each(function () {
					// Replace the text with a <pre> element
					var publicKey = $(this).text().trim();
					$(this).html('<pre style="white-space: pre-wrap; word-break: break-all;">' + publicKey + '</pre>');
				});
			}
		},
	};

	if (window.location.href.indexOf("verifone") > -1) {
		wc_verifone_admin.init();
	}

});
