<?php
/**
 * FrontendRenderHook class.
 *
 * Renders the SpectatorViews in the frontend via a dynamic WooCommerce hook.
 *
 * @package Ilabs\Firevert\Modules\SpectatorViews\Hooks\Frontend
 */

declare( strict_types=1 );

namespace Ilabs\Firevert\Modules\SpectatorViews\Hooks\Frontend;

use Ilabs\Firevert\Modules\SpectatorViews\Widget\SpectatorViews;
use Ilabs\Firevert\Modules\SpectatorViews\Services\Type\PositionType;

/**
 * Class FrontendRenderHook
 */
final class FrontendRenderHook {

	/**
	 * SpectatorViews instance.
	 *
	 * @var SpectatorViews
	 */
	private SpectatorViews $widget;

	/**
	 * Constructor.
	 *
	 * @param SpectatorViews $widget SpectatorViews instance.
	 */
	public function __construct( SpectatorViews $widget ) {
		$this->widget = $widget;
	}

	/**
	 * Registers the widget render hook based on settings.
	 *
	 * @return void
	 */
	public function register(): void {
		add_action( 'wp', array( $this, 'maybe_register_positioned_hook' ) );
	}

	/**
	 * Conditionally adds the hook to render the widget in a specific position.
	 *
	 * @return void
	 */
	public function maybe_register_positioned_hook(): void {
		if ( ! is_product() ) {
			return;
		}

		$position = $this->widget->get_display_position();

		switch ( $position ) {
			case PositionType::AFTER_ADD_TO_CART_BUTTON:
				add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'render_widget' ) );
				break;

			case PositionType::BEFORE_ADD_TO_CART_FORM:
				add_action( 'woocommerce_before_add_to_cart_form', array( $this, 'render_widget' ) );
				break;

			case PositionType::BEFORE_VARIATIONS_FORM:
				add_action( 'woocommerce_before_variations_form', array( $this, 'render_widget' ) );
				break;

			case PositionType::BEFORE_ADD_TO_CART_BUTTON:
				add_action( 'woocommerce_before_add_to_cart_button', array( $this, 'render_widget' ) );
				break;

			case PositionType::BEFORE_SINGLE_VARIATION:
				add_action( 'woocommerce_before_single_variation', array( $this, 'render_widget' ) );
				break;

			case PositionType::BEFORE_QUANTITY_FIELD:
				add_action( 'woocommerce_before_add_to_cart_quantity', array( $this, 'render_widget' ) );
				break;

			case PositionType::AFTER_QUANTITY_FIELD:
				add_action( 'woocommerce_after_add_to_cart_quantity', array( $this, 'render_widget' ) );
				break;

			case PositionType::AFTER_VARIATIONS_FORM:
				add_action( 'woocommerce_after_variations_form', array( $this, 'render_widget' ) );
				break;

			case PositionType::AFTER_ADD_TO_CART_FORM:
				add_action( 'woocommerce_after_add_to_cart_form', array( $this, 'render_widget' ) );
				break;

			case PositionType::PRODUCT_META_START:
				add_action( 'woocommerce_product_meta_start', array( $this, 'render_widget' ) );
				break;

			case PositionType::PRODUCT_META_END:
				add_action( 'woocommerce_product_meta_end', array( $this, 'render_widget' ) );
				break;

			case PositionType::AFTER_SINGLE_PRODUCT_SUMMARY:
				add_action( 'woocommerce_after_single_product_summary', array( $this, 'render_widget' ) );
				break;

			default:
				break;
		}
	}

	/**
	 * Outputs the widget HTML.
	 *
	 * @return void
	 */
	public function render_widget(): void {
		echo wp_kses_post( $this->widget->render() );
	}
}
