
import {
	PIXELIOCARTABANDONRECOVERYMOREPREVIEWCONTAINERCLASS ,
} from "../var/pixelio_cart_abandon_recovery_more_variable";
import { PixelioCartAbandonRecoveryMoreAddToCartHandler } from "./pixelio_cart_abandon_recovery_more_add_to_cart_handler";
import { PixelioCartAbandonRecoveryMoreRemoveFromCartHandler } from "./pixelio_cart_abandon_recovery_more_remove_from_cart_handler";
import { PixelioCartAbandonRecoveryMorePopupVisibilityHandler } from "./pixelio_cart_abandon_recovery_more_popup_visibility_handler";
import { PixelioCartAbandonRecoveryMoreIdleHandler } from "./pixelio_cart_abandon_recovery_more_idle_handler";
import {
    PixelioCartAbandonRecoveryMoreEmailPopUp
} from "../../../../admin/assets/js/components/pixelio_cart_abandon_recovery_more_email_popup_on_add_to_cart";
import {
    PixelioCartAbandonRecoveryMorePopUp
} from "../../../../admin/assets/js/components/pixelio_cart_abandon_recovery_more_exit_intent_popup";
import {PixelioCartAbandonRecoveryMoreAjaxRequest} from "./pixelio_cart_abandon_recovery_more_ajax_request";
import {PixelioCartAbandonRecoveryMoreEventTracker} from "./pixelio_cart_abandon_recovery_more_event_tracker";
import {PixelioCartAbandonRecoveryMoreCartTimer} from "./pixelio_cart_abandon_recovery_more_cart_timer";
import {PixelioCartAbandonRecoveryMoreEngagementTracker} from "./pixelio_cart_abandon_recovery_more_engagement_tracker";

export class PixelioCartAbandonRecoveryMoreEventHandler {
    constructor(callbackUnload = null, callbackScroll = null, callbackNewPage = null) {
        this.callbackUnload = callbackUnload;
        this.callbackScroll = callbackScroll;
        this.callbackNewPage = callbackNewPage;

        this.ajaxRequest = new PixelioCartAbandonRecoveryMoreAjaxRequest();
        this.eventTracker = new PixelioCartAbandonRecoveryMoreEventTracker();

        this.emailCapture = new PixelioCartAbandonRecoveryMoreEmailPopUp(true, false);
        this.cartTimer = new PixelioCartAbandonRecoveryMoreCartTimer();

        this.popupVisibilityHandler =
            new PixelioCartAbandonRecoveryMorePopupVisibilityHandler(
                PIXELIOCARTABANDONRECOVERYMOREPREVIEWCONTAINERCLASS
            );

        this.exitPopup = new PixelioCartAbandonRecoveryMorePopUp(
            PIXELIOCARTABANDONRECOVERYMOREPREVIEWCONTAINERCLASS,
            null,
            this.popupVisibilityHandler.setToHide.bind(this.popupVisibilityHandler),
            true,
            null,
            true
        );

        this.addToCartHandler = new PixelioCartAbandonRecoveryMoreAddToCartHandler(
            this.ajaxRequest,
            this.eventTracker,
            this.emailCapture,
            this.cartTimer
        );

        this.removeFromCartHandler = new PixelioCartAbandonRecoveryMoreRemoveFromCartHandler(
            this.ajaxRequest,
            this.cartTimer,
            this.addToCartHandler
        );

        this.idleHandler = new PixelioCartAbandonRecoveryMoreIdleHandler(
            this.exitPopup,
            this.eventTracker,
            this.popupVisibilityHandler
        );

        this.engagementTracker = new PixelioCartAbandonRecoveryMoreEngagementTracker(
            this.eventTracker,
            this.callbackNewPage
        );

        this.init();
    }

    init() {
        this.addToCartHandler.showPopupAfterAddingInCart();

        this.initDomEvents();

        this.engagementTracker.init();
    }

    initDomEvents() {
        jQuery(document).on("click", (event) => {
            this.handleClick(event);
        });

        jQuery(document).on("mousemove keypress", () => {
            this.idleHandler.resetIdleTime();
        });
    }

    handleClick(event) {
        const $button = jQuery(event.target);
        if (this.addToCartHandler.isAddToCartButton($button)) {
            this.addToCartHandler.handle($button);
            return;
        }

        if (this.removeFromCartHandler.isRemoveFromCartButton($button)) {
            this.removeFromCartHandler.handle();
        }
    }
}
