<?php

namespace Brandy\WooCommerce;

use Brandy\Traits\SingletonTrait;

class Cart {
	use SingletonTrait;

	protected function __construct() {
		add_filter( 'woocommerce_add_to_cart_fragments', array( $this, 'add_count_items_fragment' ) );
		add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false', PHP_INT_MAX );
		remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_button_view_cart', 10 );
		remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );
		add_action( 'wp_ajax_brandy_update_my_cart', array( $this, 'update_my_cart' ) );
		add_action( 'wp_ajax_nopriv_brandy_update_my_cart', array( $this, 'update_my_cart' ) );
		add_action( 'wp_footer', array( $this, 'render_mini_cart_drawer' ) );
	}

	public function update_my_cart() {
		try {
			$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( $_POST['nonce'] ) : '';

			if ( ! wp_verify_nonce( $nonce, 'brandy_update_cart' ) ) {
				throw new Error( __( 'Invalid nonce', 'brandy' ) );
			}

			$quantity = isset( $_POST['quantity'] ) ? sanitize_text_field( $_POST['quantity'] ) : 0;

			$cart_item_key = isset( $_POST['product_key'] ) ? sanitize_text_field( $_POST['product_key'] ) : '';

			$cart_item = WC()->cart->get_cart_item( $cart_item_key );

			$stock_amount_cart_item = apply_filters(
				'woocommerce_stock_amount_cart_item',
				apply_filters(
					'woocommerce_stock_amount',
					preg_replace(
						'/[^0-9\.]/',
						'',
						filter_var( $quantity, FILTER_SANITIZE_NUMBER_INT )
					)
				),
				$cart_item_key
			);

			$cart_validation = apply_filters(
				'woocommerce_update_cart_validation',
				true,
				$cart_item_key,
				$cart_item,
				$stock_amount_cart_item
			);

			if ( $cart_validation ) {
				WC()->cart->set_quantity(
					$cart_item_key,
					$stock_amount_cart_item,
					true
				);

				WC()->cart->calculate_totals();
			}

			wp_send_json_success(
				array(
					'success' => true,
				)
			);
		} catch ( \Error $err ) {
			wp_send_json_success(
				array(
					'success' => false,
					'message' => $err->getMessage(),
				)
			);
		}
		die();
	}

	public function add_count_items_fragment( $fragments ) {
		$fragments['count'] = \WC()->cart->get_cart_contents_count();
		return $fragments;
	}

	/**
	 * Render mini cart drawer in footer (body tag)
	 */
	public function render_mini_cart_drawer() {
		global $brandy_cart_drawer_settings, $brandy_cart_settings;

		if ( empty( $brandy_cart_drawer_settings ) ) {
			return;
		}

		// Only render one drawer (not an array anymore, just a single config)
		$settings        = $brandy_cart_drawer_settings['settings'];
		$open_direction  = $brandy_cart_drawer_settings['open_direction'];
		
		// Set global cart settings for mini-cart template
		$brandy_cart_settings = $settings;
		?>
		<div class="brandy-drawer brandy-cart-drawer brandy-mini-cart" data-open-direction="<?php echo esc_attr( $open_direction ); ?>">
			<div class="widget_shopping_cart_content">
			<?php
			if ( ! \is_wc_installed() ) {
				if ( ! function_exists( 'brandy_render_mini_cart_notice' ) ) {
					?>
					<div class="brandy-mini-cart-wrapper">
						<div class="brandy-mini-cart-top">
							<div class="brandy-mini-cart__title">
								<h2 class="brandy-mini-cart__title__text"><?php esc_html_e( 'Your cart', 'brandy' ); ?></h2>
								<button class="brandy-mini-cart__close" type="button" tabindex="0" title="Close mini cart"><span class="sr-only">Close panel</span><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" aria-hidden="true" class="h-6 w-6"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"></path></svg></button>
							</div>
							<div class="brandy-mini-cart__notice">
								<div>
								<svg width="70" height="70" viewBox="0 0 70 70" fill="none" xmlns="http://www.w3.org/2000/svg">
								<rect width="70" height="70" rx="16" fill="#8337FF"/>
								<path d="M54.9618 37.7016C53.4579 37.7016 52.4553 36.5768 52.4553 35.01C52.4553 33.4433 53.4779 32.2984 54.9618 32.2984C56.4456 32.2984 57.5084 33.4433 57.5084 35.01C57.5084 36.5768 56.4657 37.7016 54.9618 37.7016ZM54.9618 42C58.9521 42 62 39.0273 62 35.01C62 30.9928 58.9521 28 54.9618 28C50.9714 28 47.9436 30.9727 47.9436 35.01C47.9436 39.0473 50.9915 42 54.9618 42ZM39.9228 37.7016C38.4189 37.7016 37.3962 36.5768 37.3962 35.01C37.3962 33.4433 38.4189 32.2984 39.9228 32.2984C41.4267 32.2984 42.4493 33.4433 42.4493 35.01C42.4493 36.5768 41.4467 37.7016 39.9228 37.7016ZM39.9228 42C43.8931 42 46.941 39.0273 46.961 35.01C46.961 30.9727 43.8931 28 39.9228 28C35.9525 28 32.8845 30.9727 32.8845 35.01C32.8845 39.0473 35.9525 42 39.9228 42ZM14.8979 42C16.4619 42 17.7453 41.2367 18.6877 39.429L20.8132 35.4519V38.8264C20.8132 40.8149 22.0965 42 24.0817 42C25.6457 42 26.7887 41.3171 27.8916 39.429L32.7843 31.1736C33.847 29.3458 33.085 28 30.739 28C29.4757 28 28.6535 28.4017 27.9116 29.7877L24.5429 36.1148V30.4907C24.5429 28.8034 23.7408 28 22.277 28C21.0939 28 20.1716 28.5022 19.4497 29.9082L16.2815 36.1148V30.5509C16.2815 28.7432 15.5395 28 13.7549 28H10.0854C8.70182 28 8 28.6428 8 29.8278C8 31.0129 8.74192 31.6958 10.0854 31.6958H11.5893V38.8063C11.5893 40.8149 12.9328 42 14.8979 42Z" fill="white"/>
								</svg>
								</div>
								<h3><?php esc_html_e( 'WooCommerce plugin required', 'brandy' ); ?></h3>
								<p><?php printf( esc_html__( 'Please install %s plugin to use this feature', 'brandy' ), '<strong>WooCommerce</strong>' ); ?></p>
								<a class="wp-block-button__link" href="<?php echo esc_url( admin_url( 'plugin-install.php?s=woocommerce&tab=search&type=term' ) ); ?>" class="button button-primary"><?php esc_html_e( 'Go to Plugins', 'brandy' ); ?></a>
							</div>
						</div>
					</div>
					<?php
				} else {
					brandy_render_mini_cart_notice();
				}
			}
			if ( function_exists( 'woocommerce_mini_cart' ) ) {
				\woocommerce_mini_cart();
			}
			?>
			</div>
		</div>
		<div class="brandy-cart-overlay"></div>
		<?php
	}

}

Cart::get_instance();
