import {
	PixelioCartAbandonRecoveryMoreLoaderSpinner
} from "../components/pixelio_cart_abandon_recovery_more_loader_spinner";
import { PixelioCartAbandonRecoveryMoreAdminProgressBar } from "../components/pixelio_cart_abandon_recovery_more_admin_progress_bar";
import {
	PixelioCartAbandonRecoveryMoreUpdate
} from "../components/pixelio_cart_abandon_recovery_more_update_popup";
jQuery(function ($) {
	var spinner = new PixelioCartAbandonRecoveryMoreLoaderSpinner();
	if (pixelioData.has_api_key) {
		if (typeof window.nextStep === 'function') {
			window.nextStep();
		}
	}

	$('.pixelio-cart-abandon-recovery-more-email-choice-tags .pixelio-cart-abandon-recovery-more-tag').on('click', function (e) {
		e.preventDefault();
		var $tag = $(this);
		$('.pixelio-cart-abandon-recovery-more-email-choice-tags .pixelio-cart-abandon-recovery-more-tag').removeClass('pixelio-cart-abandon-recovery-more-selected');
		$tag.addClass('pixelio-cart-abandon-recovery-more-selected');
		window.nextStep();
	});


	var $cards = $('.pixelio-cart-abandon-recovery-more-card-without-next-button');
	$cards.on('click', function (e) {
		e.preventDefault();
		var $card = $(this);
		$cards.removeClass('pixelio-cart-abandon-recovery-more-selected');
		$card.addClass('pixelio-cart-abandon-recovery-more-selected');
		window.nextStep();
		var level = $(".pixelio-cart-abandon-recovery-more-step-2 .pixelio-cart-abandon-recovery-more-selected").data("value");
	});


	$('.pixelio-cart-abandon-recovery-more-email-choice-tags span').hover(
		function () {
			var mode = $(this).data('mode');
			$('.pixelio-cart-abandon-recovery-more-' + mode + '-description').removeClass('pixelio-cart-abandon-recovery-more-hide-it');
		},
		function () {
			var mode = $(this).data('mode');
			$('.pixelio-cart-abandon-recovery-more-' + mode + '-description').addClass('pixelio-cart-abandon-recovery-more-hide-it');
		}
	);


	function reactivateFinalizeButton() {
		$(this).prop('disabled', false);
	}

	$('#pixelio-cart-abandon-recovery-more-finish-config').on('click', function (e) {
		e.preventDefault();
		$(this).prop('disabled', true);
		var allSelected = true;
		$('.steps').each(function () {
			if ($(this).find('.pixelio-cart-abandon-recovery-more-tag.pixelio-cart-abandon-recovery-more-selected, .pixelio-cart-abandon-recovery-more-card.pixelio-cart-abandon-recovery-more-selected').length === 0) {
				allSelected = false;
				$(this).addClass('step-error');
			} else {
				$(this).removeClass('step-error');
			}
		});

		var shopType = $("#pixelio-cart-abandon-recovery-more-pixelio-shop-type").val();
		if (!shopType) {
			alert(pixelioData.selectShopType);
			$(this).prop('disabled', false);
			return;
		}
		if (!allSelected) {
			alert(pixelioData.selectOptionEachStep);
			$(this).prop('disabled', false);
			return;
		}
		const checkbox = document.getElementById('pixelio-cart-abandon-recovery-more-term-of-use');

		if (checkbox && !checkbox.checked) {
			alert(pixelioData.acceptTermOfUse);
			$(this).prop('disabled', false);
			return;

		}
		var apiKey = pixelioData.apiKey;
		var configData = {
			typeOfUse: $(".pixelio-cart-abandon-recovery-more-step-3 .pixelio-cart-abandon-recovery-more-selected").data("mode"),
			level: $(".pixelio-cart-abandon-recovery-more-step-2 .pixelio-cart-abandon-recovery-more-selected").data("value"),
			isUe: $('#pixelio-cart-abandon-recovery-more-ue').is(':checked') ? '1' : '0',
			pixelioTermOfUse: $('#pixelio-cart-abandon-recovery-more-term-of-use').is(':checked') ? '1' : '0',
			pixelioTermOfUseConsentDescription: $('#pixelio-cart-abandon-recovery-more-term-of-use').parent().find('label').html(),
			analyseConsentDescription:  "NOT USED",
			shopType: shopType,
			language: pixelioData.language
		};

		spinner.showPixelioLoader();
		var button = this;
		// Appel AJAX à l'API externe
		$.ajax({
			url: pixelioData.apiUrl + 'first-config',
			method: 'POST',
			contentType: 'application/json',
			headers: { 'x-api-key': apiKey },
			data: JSON.stringify(configData),
			success: function (response) {
				if (response.configuration && response.level_json) {
					var settings = response.level_json;

					$.ajax({
						url: ajaxurl,
						method: 'POST',
						dataType: 'json',
						data: { value: JSON.stringify(settings),
							config: 'done',
							shopType: shopType,
							is_ia: $(".pixelio-cart-abandon-recovery-more-step-3 .pixelio-cart-abandon-recovery-more-selected").data("mode"), action: 'pixelio_save_json_options',	_ajax_nonce: pixelioData.nonce },
						success: function () {
							reactivateFinalizeButton.call(button);
							spinner.hidePixelioLoader();
							window.location.href = 'admin.php?page=rivio-ai-cart-recovery-marketing-email-automation';
						},
						error: function (err) {
							alert(pixelioData.wpServerError);
							$(this).prop('disabled', false);
							spinner.hidePixelioLoader();
						}
					});

				} else {
					spinner.hidePixelioLoader();
					alert(pixelioData.invalidConfig);
					reactivateFinalizeButton.call(button);

				}
			},
			error: function (err) {
				spinner.hidePixelioLoader();
				alert(pixelioData.wpServerError);
				reactivateFinalizeButton.call(button);
			}
		});
	});

	// Vérification du domaine
	$('#pixelio-cart-abandon-recovery-more-verify-domain').on('click', function (e) {
		e.preventDefault();

		var domain = $('#pixelio-cart-abandon-recovery-more-domain').val().trim();
		var email = $('#pixelio-cart-abandon-recovery-more-email').val().trim();
		var $resultDiv = $('#pixelio-cart-abandon-recovery-more-verification-result');
		$resultDiv.html('<span style="color:blue;">' + pixelioData.verifyingDomain + '</span>');

		if (!domain) {
			$resultDiv.html('<span style="color:red;">' + pixelioData.enterDomain + '</span>');
			return;
		}

		$.ajax({
			url: ajaxurl,
			type: 'POST',
			dataType: 'json',
			data: {
				action: 'pixelio_verify_domain',
				domain: domain,
				email: email,
				_ajax_nonce: pixelioData.nonce
			},
			success: function (response) {
				if (response.success) {
					$resultDiv.html('<span style="color:green;">' + response.data.message + '</span>');
					$("#pixelio-cart-abandon-recovery-more-domain").prop('disabled', true);
					$(".pixelio-cart-abandon-recovery-more-step-1").data('step-state', 1);
					$("#pixelio-cart-abandon-recovery-more-verify-domain").prop('disabled', true);
					window.location.reload();
				} else {
					$resultDiv.html('<span style="color:red;">' + response.data + '</span>');
					if (response.data.solution) {
						$resultDiv.append('<br><small>Solution: ' + response.data.solution + '</small>');
					}
				}
			},
			error: function () {
				$resultDiv.html('<span style="color:red;">' + pixelioData.unexpectedError + '</span>');
			}
		});
	});

});
