<?php

namespace PixelioCartAbandonRecoveryMore\Admin\Views;

use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper;
use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Uuid_Repository;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

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

class Pixelio_Cart_Abandon_Recovery_More_Form_Builder
{
	private Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper $woocommerce_wrapper;
	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper;
	private Pixelio_Cart_Abandon_Recovery_More_Util $pixelio_util;
	private Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository $user_consent_repo;
	private Pixelio_Cart_Abandon_Recovery_More_Uuid_Repository $uuid_repo;

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

	public function get_cart_recovery_form_html(): string
	{
		if (!function_exists('WC') || !$this->woocommerce_wrapper->pixelio_get_cart() || $this->woocommerce_wrapper->pixelio_is_cart_empty()) {
			return '';
		}
		$uuid_visit = $this->wordpress_wrapper->pixelio_sanitize_text_field($this->wordpress_wrapper->pixelio_wc_get_session('uuid'));
		$user_id = $this->uuid_repo->pixelio_get_user_id_by_uuid($uuid_visit);
		$user_email = $this->user_consent_repo->pixelio_get_email_by_user_id($user_id);
		$consent = $this->user_consent_repo->pixelio_is_subscribed($user_email, Pixelio_Cart_Abandon_Recovery_More_Util::$finalite_cart);
		$html_parts = [];
		if (!$consent) {
			$html_parts[] = '<div class="pixelio-cart-abandon-recovery-form">';
			$html_parts[] = sprintf(
				'<p><strong>%s</strong></p>',
				$this->wordpress_wrapper->pixelio_esc_html('Save your cart by providing your email:')
			);
			$html_parts[] = '<form id="pixelio-cart-abandon-recovery-form" style="display:flex; align-items:center; gap:8px;">';
			$html_parts[] = '<div class="pixelio-cart-abandon-recovery-form-with-rgpd">';
			$html_parts[] = sprintf(
				'<input type="email" id="pixelio-cart-abandon-recovery-more-email" autocomplete="email" name="pixelio-cart-abandon-recovery-more-pop-up-email"
            placeholder="%s" required style="flex:1; padding:6px 8px; margin-bottom:0; 	border: 1px solid #ccc!important;
">',
				$this->wordpress_wrapper->pixelio_esc_html('Your email')
			);
			if ($this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-gdpr-consent-checkbox')) {
				$html_parts[] = sprintf(
					'<label style="display:flex; align-items:center; gap:4px; margin-top:4px;">
                    <input type="checkbox" name="pixelio-cart-abandon-recovery-more-pop-up-rgpd" class="pixelio-cart-abandon-recovery-more-pop-up-rgpd " required style="width:16px; height:16px;">
                    %s
                </label>',
					$this->wordpress_wrapper->pixelio_esc_html('I consent to the processing of my personal data to save my cart and to receive follow-up communications regarding my cart')
				);
			}
			$html_parts[] = '</div>';

			$html_parts[] = sprintf(
				'<button type="submit" class="pixelio-cart-abandon-recovery-more-newsletters-submit"
            style="width:50%%; max-width:200px; height:auto; border:none; border-radius:4px;     margin: 0 !important; background-color:#0d367a; color:white; cursor:pointer; ">%s</button>',
				$this->wordpress_wrapper->pixelio_esc_html('Save')
			);


			$html_parts[] = '</form>';
			$html_parts[] = '</div>';
			$html_parts[] = '</div>';
			$html_parts[] = '<p class="pixelio-cart-abandon-recovery-more-cart-abandon-recovery-more-sucess-message pixelio-cart-abandon-recovery-more-hide-it">' .
				$this->wordpress_wrapper->pixelio_esc_html('Thank you, your cart is saved. You will soon get a link to recover it by email. Check your spam')
				. '
</p">';
		}
		return implode("\n", $html_parts);
	}
}
