<?php

/**
 * WooCommerce Product Class.
 *
 * @package Kubelio\WooCommerce
 * @since 1.0.0
 */

namespace Kubelio\WooCommerce;

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

/**
 * Product Class
 */
class Product {

	/**
	 * Constructor.
	 */
	public function __construct() {
		// Custom Gallery
		add_action('after_setup_theme', array($this, 'product_images_remove'));
		add_action('woocommerce_before_single_product_summary', array($this, 'product_images_added'), 10);

		// Breadcrumbs
		remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);
		add_action('woocommerce_before_single_product_summary', array($this, 'add_theme_breadcrumbs'), 1);

		// Sections Wrapper
		add_action('woocommerce_after_single_product_summary', array($this, 'sections_start'), 1);
		add_action('woocommerce_after_single_product_summary', array($this, 'sections_end'), 99);

		// Pagination
		remove_action('woocommerce_after_shop_loop', 'woocommerce_pagination', 10);
		add_action('woocommerce_after_shop_loop', array($this, 'add_theme_pagination'), 10);

		// Tabs
		add_filter('woocommerce_product_description_heading', '__return_null');
		add_filter('woocommerce_product_additional_information_heading', '__return_null');

		// Price & Flashsale
		remove_action('woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10);
		remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
		add_action('woocommerce_single_product_summary', array($this, 'wrap_price'), 10);

		// Extra Links & Social Share
		add_action('woocommerce_share', array($this, 'extra_link'), 10);
		add_action('kubelio_product_extra_link', array($this, 'social_share'), 20);

		add_action('woocommerce_before_quantity_input_field', array($this, 'quantity_button_minus'));
		add_action('woocommerce_after_quantity_input_field', array($this, 'quantity_button_plus'));
		add_filter('woocommerce_cart_item_quantity', array($this, 'remove_quantity_sold_individually'), 10, 3);

		// Review form modal (rendered at page root to avoid stacking-context issues).
		add_action('wp_footer', array($this, 'review_form_modal'));
	}

	/**
	 * Remove Default Product Images
	 */
	public function product_images_remove() {
		remove_action('woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20);
		remove_action('woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20);
	}

	/**
	 * Add Custom Product Gallery
	 */
	public function product_images_added() {
		get_template_part('woocommerce/single-product/gallery');
	}

	/**
	 * Add theme breadcrumbs
	 */
	public function add_theme_breadcrumbs() {
		echo kubelio_get_breadcrumbs(array('class' => array('mb-30'))); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}

	/**
	 * Wrap Product Sections
	 */
	public function sections_start() {
		echo '<div class="rt-product-sections wp-block-group is-layout-flow">';
	}

	/**
	 * Wrap Product Sections end
	 */
	public function sections_end() {
		echo '</div>';
	}

	/**
	 * Add theme pagination
	 */
	public function add_theme_pagination() {
		echo kubelio_get_pagination(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}

	/**
	 * Wrap product price with flash sale
	 */
	public function wrap_price() {
		echo '<div class="rt-product-price">';
		woocommerce_template_single_price();
		woocommerce_show_product_sale_flash();
		do_action('kubelio_after_product_price');
		echo '</div>';
	}

	/**
	 * Extra product link
	 */
	public function extra_link() {
		echo "<div class='rt-product-extra-link has-x-small-font-size has-font-semibold has-body-color has-link-current-color flex flex-wrap items-center gap-30'>";
		do_action('kubelio_product_extra_link');
		echo "</div>";
	}

	/**
	 * Social share
	 */
	public function social_share() {
		get_template_part('woocommerce/single-product/share-links');
	}

	/**
	 * Minus icon on quantity
	 */
	public function quantity_button_minus() {
		$class = (is_single()) ? 'is-size-large' : '';
		echo '<div class="rt-quantity js-quantity-increment ' . esc_attr($class) . '">';
		echo '<button type="button" class="rt-quantity__button increment-down" aria-label="' . esc_attr__('Decrease quantity', 'kubelio') . '">−</button>';
	}

	/**
	 * Plus icon on quantity
	 */
	public function quantity_button_plus() {
		echo '<button type="button" class="rt-quantity__button increment-up" aria-label="' . esc_attr__('Increase quantity', 'kubelio') . '">+</button>';
		echo '</div>';
	}

	/**
	 * Remove quantity on sold individually
	 *
	 * @param string $product_quantity The product quantity HTML.
	 * @param string $cart_item_key    The cart item key.
	 * @param array  $cart_item        The cart item array.
	 * @return string
	 */
	public function remove_quantity_sold_individually($product_quantity, $cart_item_key, $cart_item) {
		$product = $cart_item['data'];
		if ($product->is_sold_individually()) {
			return '';
		}
		return $product_quantity;
	}

	/**
	 * Output the review form modal at the page root (footer).
	 */
	public function review_form_modal() {
		if (kubelio_is_woocommerce('product')) {
			get_template_part('woocommerce/single-product/review-form-modal');
		}
	}
}
