<?php

namespace PixelioCartAbandonRecoveryMore\Admin\Wrapper;


use WC_DateTime;
use WC_Session_Handler;

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

class Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper
{

	function pixelio_is_cart_empty(): bool
	{
		return WC()->cart->is_empty();
	}

	function pixelio_WC_Shipping()
	{
		return new \WC_Shipping();
	}

	public function pixelio_add_action(
		string   $hook,
		callable $callback,
		int      $priority = 10,
		int      $accepted_args = 1
	): void
	{
		add_action($hook, $callback, $priority, $accepted_args);
	}

	public function pixelio_add_filter(
		string   $hook,
		callable $callback,
		int      $priority = 10,
		int      $accepted_args = 1
	): void
	{
		add_filter($hook, $callback, $priority, $accepted_args);
	}

	function pixelio_is_checkout(): bool
	{
		return is_checkout();
	}

	public function pixelio_get_total(): float
	{
		if (!function_exists('WC') || !WC()->cart) {
			return 0;
		}

		return (float)WC()->cart->get_total('edit');
	}

	public function pixelio_get_currency(): string
	{
		if (!function_exists('get_woocommerce_currency')) {
			return '';
		}

		return get_woocommerce_currency();
	}

	function pixelio_wc_price($price): string
	{
		return wc_price($price);
	}

	function pixelio_get_cart(): ?\WC_Cart
	{
		return WC()->cart;
	}

	public function pixelio_get_coupon_model(): \WC_Coupon
	{
		return new \WC_Coupon();
	}


	function pixelio_wc_get_order($order_id)
	{
		return wc_get_order($order_id);
	}

	function pixelio_wc_get_orders($args): array
	{
		if (!function_exists('wc_get_orders')) {
			return [];
		}

		$defaults = [
			'status' => ['completed', 'processing'],
			'limit' => -1,
		];

		$args = array_merge($defaults, $args);

		$orders = wc_get_orders($args);

		return is_array($orders) ? $orders : [];
	}

	function pixelio_get_cart_item($cart_item_key): array
	{
		return WC()->cart->get_cart_item($cart_item_key);
	}


	function pixelio_get_product()
	{
		global $product;
		return $product;
	}

	function pixelio_get_base_country()
	{
		return WC()->countries->get_base_country();
	}

	function pixelio_wc_get_product($id): false|\WC_Product|null
	{
		return wc_get_product($id);
	}

	function pixelio_add_content_to_cart($item_key, $item_name, $value): void
	{

		WC()->cart->cart_contents[$item_key][$item_name] = $value;

	}

	public function pixelio_get_WC_DateTime()
	{
		return new WC_DateTime();
	}

	public function pixelio_wc_set_session($name, $value)
	{
		if (null === WC()->session) {
			WC()->session = new WC_Session_Handler();
			WC()->session->init();
		}
		WC()->session->set($name, $value);
	}

	public function pixelio_wc_add_notice(string $string, string $notice_type = 'error')
	{
		wc_add_notice($string, $notice_type);
	}

	function pixelio_wc_get_cart_url(): string
	{
		return wc_get_cart_url();
	}

	function pixelio_add_to_cart($product_id, $quantity)
	{
		try {
			return WC()->cart->add_to_cart($product_id, $quantity);
		} catch (\Exception $e) {

		}
		return false;
	}

	function pixelio_is_cart(): bool
	{
		return is_cart();
	}

	function pixelio_empty_cart(): void
	{
		WC()->cart->empty_cart();
	}

	public function pixelio_woocommerce_register_additional_checkout_field(array $pixelio_cart_abandon_recovery_more_get_rgpd_field_config): void
	{
		try {
			woocommerce_register_additional_checkout_field($pixelio_cart_abandon_recovery_more_get_rgpd_field_config);

		} catch (\Exception $e) {

		}
	}

	public function pixelio_is_product()
	{
		return is_product();
	}

	public function pixelio_wc_get_page_permalink(string $string)
	{
		return wc_get_page_permalink($string);
	}

	public function pixelio_wc_get_session(string $name, $default = null)
	{
		if (!function_exists('WC')) {
			return $default;
		}

		if (null === WC()->session) {
			WC()->session = new WC_Session_Handler();
			WC()->session->init();
		}

		return WC()->session->get($name, $default);
	}
}
