import { PixelioCartAbandonRecoveryMoreTimer } from "../../../../admin/assets/js/components/pixelio_cart_abandon_recovery_more_timer";
import {
	PIXELIOCARTABANDONRECOVERYMOREHIDEIT,
	PIXELIOCARTABANDONRECOVERYMORETIMER,
	PIXELIOCARTABANDONRECOVERYMORETIMERCONTENTCLASS,
	PIXELIOCARTABANDONRECOVERYMORETIMERSELECTORCLASS
} from "../var/pixelio_cart_abandon_recovery_more_variable";
import { PixelioCartAbandonRecoveryMoreAjaxRequest } from "./pixelio_cart_abandon_recovery_more_ajax_request";

export class PixelioCartAbandonRecoveryMoreCartTimer extends PixelioCartAbandonRecoveryMoreTimer {

	constructor() {
		super();
		this.ajaxRequest = new PixelioCartAbandonRecoveryMoreAjaxRequest();
		this.showAlert = false;
	}

	pixelioCartAbandonRecoveryMoreStartTimer() {
		super.pixelioCartAbandonRecoveryMoreClearTimer();
		if (jQuery(PIXELIOCARTABANDONRECOVERYMORETIMERSELECTORCLASS).length > 0) {
			super.pixelioCartAbandonRecoveryMoreStartTimer(
				this.pixelioCartAbandonRecoveryMoreChangeCartTimer.bind(this),
				this.pixelioCartAbandonRecoveryMoreHideTimer.bind(this),
				PIXELIOCARTABANDONRECOVERYMORETIMERSELECTORCLASS,
				false
			);
		}
	}

	pixelioCartAbandonRecoveryMoreChangeCartTimer() {
		const $theTimerContent = jQuery(`${PIXELIOCARTABANDONRECOVERYMORETIMERSELECTORCLASS} ${PIXELIOCARTABANDONRECOVERYMORETIMERCONTENTCLASS}`);
		this.pixelioCartAbandonRecoveryMoreUpdateTimerContent($theTimerContent);
	}

	pixelioCartAbandonRecoveryMoreHideTimer() {
		jQuery(PIXELIOCARTABANDONRECOVERYMORETIMERSELECTORCLASS).addClass(PIXELIOCARTABANDONRECOVERYMOREHIDEIT);
		this.showAlert = true;
		const data = new FormData();
		this.ajaxRequest.makeAjaxRequest("pixelio_cart_abandon_recovery_more_clear_cart", data, this.reload_page.bind(this));
	}

	reload_page() {
		if(this.showAlert){
			alert(pixelio_ajax.cartCleared);
		}
		location.reload(true);
		localStorage.setItem(PIXELIOCARTABANDONRECOVERYMORETIMERSELECTORCLASS, -1);
	}

	pixelioCartAbandonRecoveryMoreSetRemaining() {
		localStorage.setItem(PIXELIOCARTABANDONRECOVERYMORETIMERSELECTORCLASS, PIXELIOCARTABANDONRECOVERYMORETIMER);
		localStorage.setItem('startTime' + PIXELIOCARTABANDONRECOVERYMORETIMERSELECTORCLASS, Date.now());
		this.pixelioCartAbandonRecoveryMoreStartTimer();
	}
}
