<?php
/**
 * CartButtons class.
 *
 * @package DokanKits\Features\Cart
 * @since   3.0.0
 */

namespace DokanKits\Features\Cart;

use DokanKits\Features\Feature;
use WC_Product;

/**
 * Cart Buttons Feature
 *
 * Provides functionality to hide WooCommerce add to cart buttons and
 * optionally auto-complete virtual/downloadable product orders.
 *
 * @since   3.0.0
 * @package DokanKits\Features\Cart
 */
class CartButtons extends Feature {

	/**
	 * Feature option key for enabling/disabling this feature
	 *
	 * @since 3.0.0
	 * @var string
	 */
	protected string $option_key = 'hide_add_to_cart_button_checkbox';

	/**
	 * Feature settings group for admin UI organization
	 *
	 * @since 3.0.0
	 * @var string
	 */
	protected string $settings_group = 'cart';

	/**
	 * Feature settings section within the group
	 *
	 * @since 3.0.0
	 * @var string
	 */
	protected string $settings_section = 'buttons';

	/**
	 * Number of sub-features contained in this feature
	 *
	 * @since 3.0.0
	 * @var int
	 */
	protected int $total_features = 1;

	/**
	 * Register hooks with WordPress
	 *
	 * Sets up the necessary actions and filters for hiding cart buttons
	 * and auto-completing orders when appropriate.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function register_hooks(): void {
		add_action( 'init', array( $this, 'hide_add_to_cart_buttons' ) );
	}

	/**
	 * Hide add to cart buttons
	 *
	 * Removes add-to-cart buttons from loops and single product pages,
	 * and adds CSS to hide any remaining buttons.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function hide_add_to_cart_buttons(): void {
		if ( ! $this->is_enabled() ) {
			return;
		}

		remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
		add_action( 'wp_head', array( $this, 'add_hide_button_css' ) );
	}

	/**
	 * Add CSS to hide add to cart buttons
	 *
	 * Outputs inline CSS in the frontend to hide any add-to-cart buttons
	 * that couldn't be removed via hook removal.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function add_hide_button_css(): void {
		echo '
         <style>
            .single-product .product-type-simple .single_add_to_cart_button,
            .single-product .product-type-variable .single_add_to_cart_button,
            .product .add_to_cart_button {
                display: none !important;
            }
		 </style>
		';
	}
}
