<?php
namespace PixelioCartAbandonRecoveryMore\Admin\Services;

if (!defined('ABSPATH')) exit;

use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

class Pixelio_Cart_Abandon_Recovery_More_Popup_Renderer_Service
{
	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper;
	private $pixelio_util;

	public function __construct($wordpress_wrapper, $pixelio_util)
	{
		$this->wordpress_wrapper = $wordpress_wrapper;
		$this->pixelio_util = $pixelio_util;
	}

	public function render_bought_message_popup(): void
	{
		$path = Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_dir_path .
			'admin/views/preview/class-pixelio-cart-abandon-recovery-more-bought-message-popup.php';

		if (!file_exists($path)) {
			return;
		}

		if (!$this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-show-others-buy-checkbox')) {
			return;
		}

		if ($this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-show-others-buy-only-product-checkbox')
			&& !$this->wordpress_wrapper->pixelio_is_product()) {
			return;
		}

		$position = $this->wordpress_wrapper->pixelio_esc_attr(
			$this->wordpress_wrapper->pixelio_get_option('pixelio-cart-abandon-recovery-more-show-others-buy-checkbox-position', 'right')
		);

		$class = Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_slug . "-show-others-buy-checkbox-{$position}";

		echo $this->wordpress_wrapper->pixelio_wp_kses('<div id="pixelio-cart-abandon-recovery-more-bought-message-popup-id" class="' . $class . ' pixelio-cart-abandon-recovery-more-preview-container pixelio-cart-abandon-recovery-more-edit-container">',Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_cart_abandon_recovery_more_allowed_html);
		include $path;
		echo $this->wordpress_wrapper->pixelio_wp_kses('</div>',Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_cart_abandon_recovery_more_allowed_html);
	}

	public function render_exit_popup(): void
	{
		$path = Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_dir_path .
			'admin/views/preview/class-pixelio-cart-abandon-recovery-more-exit-popup.php';

		if (!file_exists($path)) {
			return;
		}

		$show = $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-exit-popup-checkbox');
		$gdpr_ok = (!$this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-gdpr-consent-checkbox')
				&& !$this->wordpress_wrapper->pixelio_is_user_logged_in())
			|| $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-gdpr-consent-checkbox');

		if (!$show || !$gdpr_ok) {
			return;
		}

		echo '<div class="pixelio-cart-abandon-recovery-more-preview-container pixelio-cart-abandon-recovery-more-hide-it" id="pixelio-cart-abandon-recovery-more-preview-container-exit-popup">';
		include $path;
		echo '</div>';
	}

	public function render_capture_user_mail_popup(): void
	{
		$show = $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-capture-email-checkbox');
		$gdpr_ok = (!$this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-gdpr-consent-checkbox')
				&& !is_user_logged_in())
			|| $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-gdpr-consent-checkbox');

		if (!$show || !$gdpr_ok) return;

		$path = Pixelio_Cart_Abandon_Recovery_More_Util::$pixelio_plugin_dir_path .
			'admin/views/preview/class-pixelio-cart-abandon-recovery-more-bought-capture-users-mail-popup.php';

		if (!file_exists($path)) return;

		echo '<div id="pixelio-cart-abandon-recovery-more-preview-container-email-capture" class="pixelio-cart-abandon-recovery-more-preview-container pixelio-cart-abandon-recovery-more-pop-up-container-email-capture pixelio-cart-abandon-recovery-more-hide-it">';
		include $path;
		echo '</div>';
	}
}
