<?php
namespace PixelioCartAbandonRecoveryMore\Admin\Services;
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_Cart_Item_Repository;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Cart_Repository;

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

class Pixelio_Cart_Abandon_Recovery_More_Cart_Service
{
	private Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper $woocommerce_wrapper;
	private Pixelio_Cart_Abandon_Recovery_More_Cart_Repository $cart_model;
	private Pixelio_Cart_Abandon_Recovery_More_Cart_Item_Repository $cart_item;
	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper;

	public function __construct($woocommerce_wrapper, $cart_model, $cart_item,$wordpress_wrapper)
	{
		$this->woocommerce_wrapper = $woocommerce_wrapper;
		$this->cart_model = $cart_model;
		$this->cart_item = $cart_item;
		$this->wordpress_wrapper = $wordpress_wrapper;
	}

	public function recover_cart_from_get(): void
	{

		if (!isset($_GET['recover_cart'])) {
			return;
		}

		$token = $this->wordpress_wrapper->pixelio_sanitize_text_field($this->wordpress_wrapper->pixelio_wp_unslash($_GET['recover_cart']));
		$cart = $this->cart_model->pixelio_find_cart_by_token($token);

		if (empty($cart)) {
			$this->woocommerce_wrapper->pixelio_wc_add_notice('Your recovery cart is empty or expired.');
			$this->wordpress_wrapper->pixelio_wp_safe_redirect(wc_get_cart_url()."?pixelio_expired");
			exit;
		}

		$this->woocommerce_wrapper->pixelio_empty_cart();

		$cart_id = $this->cart_model->pixelio_find_by_uuid($cart[0]->uuid);
		$this->woocommerce_wrapper->pixelio_wc_set_session('uuid',$cart[0]->uuid);
		$cart_items = $this->cart_item->pixelio_find_item_by_cart_id($cart_id);
		if (empty($cart_items)) {

			$this->woocommerce_wrapper->pixelio_wc_add_notice('Your recovery cart is empty or expired.');
			$this->wordpress_wrapper->pixelio_wp_safe_redirect(add_query_arg('pixelio_expired', '1', 	$this->woocommerce_wrapper->pixelio_wc_get_cart_url()));
			exit;
		}

		foreach ($cart_items as $cart_item) {
			$this->cart_item->pixelio_remove_cart_by_product_and_cart_id($cart_id,$cart_item->woo_product_id);
			$item_key = $this->woocommerce_wrapper->pixelio_add_to_cart($cart_item->woo_product_id, $cart_item->quantity);

			if ($item_key) {
				$this->woocommerce_wrapper->pixelio_add_content_to_cart($item_key, 'pixelio_uuid', $this->wordpress_wrapper->pixelio_wc_get_session('uuid'));
				if (isset($_GET["template_id"])) {

					$this->woocommerce_wrapper->pixelio_add_content_to_cart($item_key, 'pixelio_template_id', $_GET["template_id"]);
				}
			}
		}

		$this->wordpress_wrapper->pixelio_set_cookie('recovered', '1', time() + 3600, '/');
		$this->cart_model->pixelio_cart_abandon_recovery_more_update_cart_by_id($cart[0]->id, -1, 5);

		$this->wordpress_wrapper->pixelio_wp_safe_redirect($this->woocommerce_wrapper->pixelio_wc_get_cart_url()."?recovercart");

		exit;
	}
}
