<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Plugin as ElementorPlugin;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Product_Rating_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_WooCommerce_Guard;
class Sifency_Widget_Product_Rating extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Product_Rating_Controls;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_WooCommerce_Guard;

	protected function get_allowed_template_types() {
		return 'product';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-product-rating',
			'title'         => 'Sifency Product Rating',
			'icon'          => 'eicon-product-rating',
			'category'      => 'sifency-addons-woo-widgets',
			'keywords'      => array( 'sifency', 'product', 'rating', 'stars', 'review', 'woocommerce' ),
			'style_depends' => array( 'sifency-frontend-style' ),
		);}

	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->add_sifency_product_rating_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$this->render_editor_preview();
			return;
		}

		$this->render_live_rating();
	}

	/**
	 * Renders the rating based on a real product for the live site.
	 */
	private function render_live_rating() {
		$product = wc_get_product();
		if ( empty( $product ) ) {
			return; }

		if ( 'no' === get_option( 'woocommerce_enable_reviews' ) ) {
			return; }

		$settings = $this->get_settings_for_display();

		if ( 'yes' !== $settings['show_review_link'] ) {
			add_filter( 'woocommerce_product_get_rating_html', array( $this, 'remove_review_link' ), 10, 2 );
		}

		$this->render_html( 'woocommerce_template_single_rating' );

		if ( 'yes' !== $settings['show_review_link'] ) {
			remove_filter( 'woocommerce_product_get_rating_html', array( $this, 'remove_review_link' ), 10, 2 );
		}
	}

	/**
	 * Renders a fake rating for live preview in the Elementor editor.
	 *
	 * @since 1.0.4
	 * @access private
	 */
	private function render_editor_preview() {
		$settings = $this->get_settings_for_display();
		$rating   = $settings['editor_preview_rating']['size'] ?? 4;
		$count    = $settings['editor_preview_count'] ?? 3;

		$rating_percentage = ( $rating / 5 ) * 100;

		$rating_html = '<div id="<?php echo esc_attr ( $wrapper_id ); ?>" class="woocommerce-product-rating">';

		/* translators: %s: Product rating value. */
		$rating_html .= '<div class="star-rating" role="img" aria-label="' . sprintf( esc_attr__( 'Rated %s out of 5', 'sifency-addons' ), $rating ) . '">';

		/* translators: %s: Product rating value wrapped in strong tag. */
		$rating_html .= '<span style="width:' . esc_attr( $rating_percentage ) . '%">' . sprintf( esc_html__( 'Rated %s out of 5', 'sifency-addons' ), '<strong>' . esc_html( $rating ) . '</strong>' ) . '</span>';
		$rating_html .= '</div>';

		if ( 'yes' === ( $settings['show_review_link'] ?? '' ) && $count > 0 ) {
			$rating_html .= '<a href="#reviews" class="woocommerce-review-link" rel="nofollow">';

			$rating_html .= sprintf(
				/* translators: %s: Number of customer reviews. */
				_n( '(%s customer review)', '(%s customer reviews)', $count, 'sifency-addons' ),
				'<span class="count">' . esc_html( $count ) . '</span>'
			);
			$rating_html .= '</a>';
		}

		$rating_html .= '</div>';

		$this->render_html( $rating_html, false );
	}


	/**
	 * Renders the final HTML output.
	 *
	 * @param string|callable $content The content to render. Can be a string or a callable function.
	 * @param bool            $is_callable True if $content is a function to be called.
	 */
	private function render_html( $content, $is_callable = true ) {
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-product-rating' );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<div 
		<?php

			echo wp_kses_post( $wrapper_attrs );
		?>
		>
			<?php
			if ( $is_callable && is_callable( $content ) ) {
				call_user_func( $content );
			} else {
				echo wp_kses_post( $content );
			}
			?>
		</div>
		<?php
	}

	/**
	 * Callback function to remove the review link from the rating HTML.
	 */
	public function remove_review_link( $html, $rating ) {
		if ( $rating > 0 ) {
			$html = wc_get_rating_html( $rating );
		} else {
			$html = '';
		}
		return $html;
	}

	public function render_plain_content() {
		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$this->render_editor_preview();
		}
	}
}
