import {
	PIXELIOCARTABANDONRECOVERYMORETIMERSELECTORCLASS
} from "../../../../frontend/assets/js/var/pixelio_cart_abandon_recovery_more_variable";

const { PIXELIOCARTABANDONRECOVERYMORETIMER } = require("../../../../frontend/assets/js/var/pixelio_cart_abandon_recovery_more_variable");

export class PixelioCartAbandonRecoveryMoreTimer {
	constructor() {
		this.remainingSeconds = 0;
		this.timerInterval = null;
		this.minutes = "0";
		this.seconds = "0";
	}

	// Met à jour le timer à chaque tick
	pixelioCartAbandonRecoveryMoreUpdateTimer = () => {
		if (this.remainingSeconds >= 0) {
			this.minutes = String(Math.floor((this.remainingSeconds % 3600) / 60));
			this.seconds = String(this.remainingSeconds % 60);

			this.#callCallbackIfNotNull(this.successCallback);

			this.remainingSeconds--;
			localStorage.setItem(this.name, this.remainingSeconds);
		} else {
			this.#callCallbackIfNotNull(this.finishCallback);
			clearInterval(this.timerInterval);
		}
	};

	// Met à jour le contenu HTML du timer
	pixelioCartAbandonRecoveryMoreUpdateTimerContent(theTimerContent) {
		if (!theTimerContent || theTimerContent.length < 5) return;

		// Minutes
		if (this.minutes.length >= 2) {
			theTimerContent[0].textContent = this.minutes[0] ?? "0";
			theTimerContent[1].textContent = this.minutes[1] ?? "0";
		} else {
			theTimerContent[0].textContent = "0";
			theTimerContent[1].textContent = this.minutes[0] ?? "0";
		}

		// Secondes
		if (this.seconds.length >= 2) {
			theTimerContent[3].textContent = this.seconds[0] ?? "0";
			theTimerContent[4].textContent = this.seconds[1] ?? "0";
		} else {
			theTimerContent[3].textContent = "0";
			theTimerContent[4].textContent = this.seconds[0] ?? "0";
		}
	}

	// Stoppe le timer
	pixelioCartAbandonRecoveryMoreClearTimer() {
		if (this.timerInterval) {
			clearInterval(this.timerInterval);
			this.timerInterval = null;
		}
	}

	// Callback sécurisé
	#callCallbackIfNotNull(callback) {
		if (callback) callback();
	}

	// Démarre le timer
	pixelioCartAbandonRecoveryMoreStartTimer(successCallback, finishCallback, name, reset = false) {
		this.pixelioCartAbandonRecoveryMoreClearTimer();

		this.successCallback = successCallback;
		this.finishCallback = finishCallback;
		this.name = name;
		this.startTimeName = "startTime" + name;
		const currentTime = Date.now();
		if (window.location.href.includes("recovercart")) {
			//init timer
			localStorage.setItem(PIXELIOCARTABANDONRECOVERYMORETIMERSELECTORCLASS, PIXELIOCARTABANDONRECOVERYMORETIMER);
			localStorage.setItem(this.startTimeName,undefined);
			const url = new URL(window.location.href);
			url.searchParams.delete("recovercart");
			window.history.replaceState({}, document.title, url.toString());
		}
		const storedTime = localStorage.getItem(this.startTimeName);
		if (!storedTime || storedTime === "undefined" || reset) {
			localStorage.setItem(this.startTimeName, currentTime);
		}

		const elapsedTime = Math.floor((currentTime - localStorage.getItem(this.startTimeName)) / 1000);

		this.remainingSeconds = PIXELIOCARTABANDONRECOVERYMORETIMER;

		if (elapsedTime >= this.remainingSeconds) {
			this.#callCallbackIfNotNull(this.finishCallback);
		} else {
			this.remainingSeconds -= elapsedTime;
			this.timerInterval = setInterval(this.pixelioCartAbandonRecoveryMoreUpdateTimer, 1000);
			this.pixelioCartAbandonRecoveryMoreUpdateTimer();
		}
	}
}
