import {PixelioCartAbandonRecoveryMorePopUp} from "./pixelio_cart_abandon_recovery_more_exit_intent_popup";
import {
	PIXELIOCARTABANDONRECOVERYMOREHIDEIT,
	PIXELIOCARTABANDONRECOVERYMORETHANKSEXITCLASS
} from "../../../../frontend/assets/js/var/pixelio_cart_abandon_recovery_more_variable";
import {
	PixelioCartAbandonRecoveryMoreAjaxRequest
} from "../../../../frontend/assets/js/components/pixelio_cart_abandon_recovery_more_ajax_request";


export class PixelioCartAbandonRecoveryMoreEmailPopUp extends PixelioCartAbandonRecoveryMorePopUp {

	constructor(isFrontend, startTimer) {
		super(
			pixelio_ajax.emailPopupId,
			null,
			null,
			isFrontend,
			null,
			startTimer
		);
		this.ajaxRequest = new PixelioCartAbandonRecoveryMoreAjaxRequest();
		this.CONSENTREFUSEDNAME = pixelio_ajax.consentRefusedName;
		this.CONSENTUNKNOWN = pixelio_ajax.consentUnknown;
		this.CONSENTREFUSED = pixelio_ajax.consentRefused;
		this.newsletterSubmitClass = pixelio_ajax.newsletterSubmitClass;
		this.newsletterSignupAction = pixelio_ajax.newsletterSignupAction;
		this.#emailCapture();
	}

	#emailCapture() {
		const self = this;
		jQuery(document).on('click', this.newsletterSubmitClass, function (e) {
			e.preventDefault();
			const $form = jQuery(this).closest('form');

			const email = $form.find('input[type="email"]').val();

			const $rgpdCheckbox = $form.find(
				'input[type="checkbox"][name="pixelio-abandon-recovery-more-rgpd"], ' +
				'input[type="checkbox"][name="pixelio-cart-abandon-recovery-more-pop-up-rgpd"]'
			);

			const isRgpdActive = Boolean(Number(pixelio_ajax.isRgpdActive));

			const rgpdChecked = $rgpdCheckbox.length > 0
				? $rgpdCheckbox.is(':checked')
				: false;

			const rgpdDefinition = $rgpdCheckbox.length > 0
				? $rgpdCheckbox.closest('label').text().trim()
				: '';

			if (isRgpdActive && $rgpdCheckbox.length > 0 && !rgpdChecked) {
				alert(pixelio_ajax.missing_rgpd);
				return;
			}

			if (!email) {
				alert(pixelio_ajax.missing_email);

				return;
			}

			const data = new FormData();
			data.append('action', 'pixelio_cart_abandon_recovery_more_create_temporary_coupon');

			if (isRgpdActive && $rgpdCheckbox.length > 0) {
				data.append('pixelio-cart-abandon-recovery-more-pop-up-rgpd', rgpdChecked ? '1' : '0');
				data.append('pixelio-cart-abandon-recovery-more-pop-up-rgpd-label', rgpdDefinition);
			}
			data.append(
				'utm_source',
				localStorage.getItem('pixelio_utm_source') || ''
			);

			data.append(
				'utm_campaign',
				localStorage.getItem('pixelio_utm_campaign') || ''
			);

			data.append(
				'utm_content',
				localStorage.getItem('pixelio_utm_content') || ''
			);
			data.append('pixelio-cart-abandon-recovery-more-pop-up-email', email);
			self.ajaxRequest.makeAjaxRequest(
				self.newsletterSignupAction,
				data,
				function (response) {
					if (response.success) {
						const $popup = $form.closest('.pixelio-cart-abandon-recovery-more-pop-up-container');
						const $couponContainer = 	jQuery(".coupon-container");
						const $formWrapper = $form.closest(
							'.pixelio-cart-abandon-recovery-more-hide-div-if-form-showed, .pixelio-cart-abandon-recovery-more-pop-up-form-container'
						);

						localStorage.setItem('pixelio_email', 1);
						localStorage.setItem('exit_popup' + self.className, 1);

						if (pixelio_ajax.isCheckoutOrCart) {
							jQuery('.pixelio-cart-abandon-recovery-more-cart-abandon-recovery-more-sucess-message')
								.removeClass('pixelio-cart-abandon-recovery-more-hide-it');

							jQuery('.pixelio-cart-abandon-recovery-form').hide();
						}

						$form[0].reset();

						$formWrapper.addClass('pixelio-cart-abandon-recovery-more-hide-it');
						$formWrapper.find('.pixelio-cart-abandon-recovery-more-pop-up-no-thanks-exit-popup')
							.addClass('pixelio-cart-abandon-recovery-more-hide-it');

						if ($couponContainer.length > 0) {
							$couponContainer.removeClass('pixelio-cart-abandon-recovery-more-hide-it');
							jQuery(".pixelio-cart-abandon-recovery-more-coupon-code-text").text(response.data.coupon_code);
						}

						self.setConsentToWait(response.gdpr);
					}else if(response.data) {
						alert(response.data.message);
					}else{
						alert(pixelio_ajax.error);
					}
				},
				function (xhr, status, error) {
					alert(pixelio_ajax.error);
				}
			);
		});
	}

	setConsentToWait(gdpr) {
		if(gdpr){
			localStorage.setItem(this.CONSENTREFUSEDNAME, this.CONSENTUNKNOWN);
		}else{
			this.consentGiven();
		}
	}

	consentGiven() {
		localStorage.setItem(this.CONSENTREFUSEDNAME, "2"); // 2 = given
	}

	declinedConsent() {
		if (this.isConsentRefused()) {
			jQuery(pixelio_ajax.emailPopupId).removeClass(pixelio_ajax.hideClass);
		}
	}


	isConsentRefused() {
		const consent = localStorage.getItem(this.CONSENTREFUSEDNAME);
		return consent !== this.CONSENTUNKNOWN && consent !== this.CONSENTREFUSED;
	}
}
