<?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\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_WooCommerce_Guard;
class Sifency_Widget_Product_Reviews extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_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-reviews',
			'title'         => 'Sifency Product Reviews',
			'icon'          => 'eicon-review',
			'category'      => 'sifency-addons-woo-widgets',
			'keywords'      => array( 'product', 'reviews', 'comments', 'rating', 'feedback', 'woocommerce' ),
			'style_depends' => array( 'sifency-frontend-style' ),
		);}

	protected function register_controls() {
		if ( ! $this->check_and_guard_woocommerce() ) {
			return;
		}
		$this->add_sifency_addons_help_controls();
		$this->start_controls_section(
			'section_style_general',
			array(
				'label' => esc_html__( 'General', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-product-reviews' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'link_color',
			array(
				'label'     => esc_html__( 'Link Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-product-reviews a' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'link_hover_color',
			array(
				'label'     => esc_html__( 'Link Hover Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-product-reviews a:hover' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();

		// --- Style Section: Headings ---
		$this->start_controls_section(
			'section_style_headings',
			array(
				'label' => esc_html__( 'Headings', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'headings_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} #reviews .comments-title, {{WRAPPER}} #reviews #reply-title' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'headings_typography',
				'selector' => '{{WRAPPER}} #reviews .comments-title, {{WRAPPER}} #reviews #reply-title',
			)
		);
		$this->end_controls_section();

		// --- Style Section: Stars ---
		$this->start_controls_section(
			'section_style_stars',
			array(
				'label' => esc_html__( 'Star Ratings', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'stars_size',
			array(
				'label'     => esc_html__( 'Size', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .star-rating' => 'font-size: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_control(
			'stars_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .star-rating span::before, {{WRAPPER}} .star-rating::before' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'stars_unmarked_color',
			array(
				'label'     => esc_html__( 'Unmarked Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .star-rating::before' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();

		// --- Style Section: Review Form ---
		$this->start_controls_section(
			'section_style_form',
			array(
				'label' => esc_html__( 'Review Form', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'heading_labels',
			array(
				'type'  => Controls_Manager::HEADING,
				'label' => esc_html__( 'Labels', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'label_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .comment-form label' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'label_typography',
				'selector' => '{{WRAPPER}} .comment-form label',
			)
		);

		$this->add_control(
			'heading_fields',
			array(
				'type'      => Controls_Manager::HEADING,
				'label'     => esc_html__( 'Fields', 'sifency-addons' ),
				'separator' => 'before',
			)
		);
		$this->add_control(
			'field_color',
			array(
				'label'     => esc_html__( 'Text Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .comment-form input:not([type=submit]), {{WRAPPER}} .comment-form textarea' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'field_bg_color',
			array(
				'label'     => esc_html__( 'Background Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .comment-form input:not([type=submit]), {{WRAPPER}} .comment-form textarea' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'field_border',
				'selector' => '{{WRAPPER}} .comment-form input:not([type=submit]), {{WRAPPER}} .comment-form textarea',
			)
		);

		$this->add_control(
			'heading_button',
			array(
				'type'      => Controls_Manager::HEADING,
				'label'     => esc_html__( 'Submit Button', 'sifency-addons' ),
				'separator' => 'before',
			)
		);
		$this->start_controls_tabs( 'button_styles' );
		$this->start_controls_tab( 'button_normal', array( 'label' => 'Normal' ) );
		$this->add_control(
			'button_bg',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .comment-form input[type="submit"]' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'button_text_color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .comment-form input[type="submit"]' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'button_border',
				'selector' => '{{WRAPPER}} .comment-form input[type="submit"]',
			)
		);
		$this->end_controls_tab();
		$this->start_controls_tab( 'button_hover', array( 'label' => 'Hover' ) );
		$this->add_control(
			'button_hover_bg',
			array(
				'label'     => 'Background',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .comment-form input[type="submit"]:hover' => 'background-color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'button_hover_text_color',
			array(
				'label'     => 'Text Color',
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .comment-form input[type="submit"]:hover' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'button_hover_border',
				'selector' => '{{WRAPPER}} .comment-form input[type="submit"]:hover',
			)
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();
		$this->end_controls_section();

		$this->add_Sifency_Addons_Animation_Controls();
		$this->update_control(
			'sifency_animation_target',
			array(
				'type'    => Controls_Manager::HIDDEN,
				'default' => 'wrapper',
			)
		);
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		if ( ! class_exists( 'woocommerce' ) ) {
			return; }

		$settings             = $this->get_settings_for_display();
		$animation_manager    = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_attrs_string = $animation_manager->get_wrapper_attributes( array( 'sifency-product-reviews' ) );

		echo '<div id="<?php echo esc_attr ( $wrapper_id ); ?>" ' . wp_kses_post( $wrapper_attrs_string ) . '>';

		if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$this->render_editor_placeholder();
		} elseif ( ! is_product() ) {
				echo '<div class="sifency-placeholder">' . esc_html__( 'Reviews can only be displayed on a single product page.', 'sifency-addons' ) . '</div>';
		} else {
			// Ensure global $product is a WC_Product object. Some themes/plugins
			// may set this global to an ID or other scalar value which breaks
			// WooCommerce templates that call methods like get_review_count().
			global $product; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

			if ( ! is_a( $product, 'WC_Product' ) ) {
				if ( function_exists( 'wc_get_product' ) ) {
					$product_obj = wc_get_product( $product );
					if ( ! $product_obj && isset( $GLOBALS['post']->ID ) ) {
						$product_obj = wc_get_product( $GLOBALS['post']->ID );
					}
					if ( $product_obj ) {
						$product = $product_obj;
					}
				}
			}

			if ( is_a( $product, 'WC_Product' ) ) {
				comments_template();
			} else {
				echo '<div class="sifency-placeholder">' . esc_html__( 'Unable to load product reviews for this product.', 'sifency-addons' ) . '</div>';
			}
		}

		echo '</div>';
	}

	/**
	 * Render the placeholder content for the editor.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function render_editor_placeholder() {
		?>
		<div id="reviews" class="woocommerce-Reviews">
			<div id="comments">
				<h2 class="woocommerce-Reviews-title">
					<?php echo esc_html__( '2 reviews for "Sample Product"', 'sifency-addons' ); ?>
				</h2>
				<ol class="commentlist">
					<li class="review">
						<img src="<?php echo esc_url( get_avatar_url( 'placeholder1@example.com' ) ); ?>" class="avatar" height="60" width="60" alt="">
						<div class="comment-text">
							<div class="star-rating" role="img" aria-label="Rated 5 out of 5">
								<span style="width:100%"><?php echo esc_html__( 'Rated 5 out of 5', 'sifency-addons' ); ?></span>
							</div>
							<p class="meta">
								<strong class="woocommerce-review__author"><?php echo esc_html__( 'John Doe', 'sifency-addons' ); ?></strong>
								<time class="woocommerce-review__published-date" datetime="2023-10-27T11:00:00+00:00"><?php echo esc_html__( 'October 27, 2023', 'sifency-addons' ); ?></time>
							</p>
							<div class="description">
								<p><?php echo esc_html__( 'This is a fantastic product! Highly recommended.', 'sifency-addons' ); ?></p>
							</div>
						</div>
					</li>
					<li class="review">
						<img src="<?php echo esc_url( get_avatar_url( 'placeholder2@example.com' ) ); ?>" class="avatar" height="60" width="60" alt="">
						<div class="comment-text">
							<div class="star-rating" role="img" aria-label="Rated 4 out of 5">
								<span style="width:80%"><?php echo esc_html__( 'Rated 4 out of 5', 'sifency-addons' ); ?></span>
							</div>
							<p class="meta">
								<strong class="woocommerce-review__author"><?php echo esc_html__( 'Jane Smith', 'sifency-addons' ); ?></strong>
								<time class="woocommerce-review__published-date" datetime="2023-10-26T15:30:00+00:00"><?php echo esc_html__( 'October 26, 2023', 'sifency-addons' ); ?></time>
							</p>
							<div class="description">
								<p><?php echo esc_html__( 'Good quality, but shipping was a bit slow.', 'sifency-addons' ); ?></p>
							</div>
						</div>
					</li>
				</ol>
			</div>
			<div id="review_form_wrapper">
				<div id="review_form">
					<div id="respond" class="comment-respond">
						<h3 id="reply-title" class="comment-reply-title"><?php echo esc_html__( 'Add a review', 'sifency-addons' ); ?></h3>
						<form class="comment-form">
							<p class="comment-form-rating">
								<label for="rating"><?php echo esc_html__( 'Your rating', 'sifency-addons' ); ?></label>
								<span class="stars"><a></a><a></a><a></a><a></a><a></a></span>
							</p>
							<p class="comment-form-comment">
								<label for="comment"><?php echo esc_html__( 'Your review', 'sifency-addons' ); ?></label>
								<textarea id="comment" name="comment" cols="45" rows="8"></textarea>
							</p>
							<p class="comment-form-author">
								<label for="author"><?php echo esc_html__( 'Name', 'sifency-addons' ); ?></label>
								<input id="author" name="author" type="text" value="" size="30">
							</p>
							<p class="comment-form-email">
								<label for="email"><?php echo esc_html__( 'Email', 'sifency-addons' ); ?></label>
								<input id="email" name="email" type="email" value="" size="30">
							</p>
							<p class="form-submit">
								<input name="submit" type="submit" id="submit" class="submit" value="<?php echo esc_attr__( 'Submit', 'sifency-addons' ); ?>">
							</p>
						</form>
					</div>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Render the widget output in the editor for live preview.
	 *
	 * @since 1.0.4
	 * @access protected
	 */
	protected function _content_template() {
		?>
		<div class="sifency-product-reviews">
			<div id="reviews" class="woocommerce-Reviews">
				<div id="comments">
					<h2 class="woocommerce-Reviews-title">
						<?php
						printf(
							/* translators: 1: Number of reviews, 2: Product name. */
							esc_html__( '%1$d reviews for %2$s', 'sifency-addons' ),
							2,
							'<span>"' . esc_html__( 'Sample Product', 'sifency-addons' ) . '"</span>'
						);
						?>
					</h2>
					<ol class="commentlist">
						<li class="review">
							<img src="<?php echo esc_url( get_avatar_url( 'placeholder1@example.com' ) ); ?>" class="avatar" height="60" width="60" alt="<?php esc_attr_e( 'Reviewer avatar', 'sifency-addons' ); ?>">
							<div class="comment-text">
								<div class="star-rating" role="img"
									<?php

									printf(
										'aria-label="%s"',
										/* translators: 1: Star rating given, 2: Maximum possible rating. */
										esc_attr( sprintf( __( 'Rated %1$d out of %2$d', 'sifency-addons' ), 5, 5 ) )
									);
									?>
									>
									<span style="width:100%">
										<?php
										printf(
											/* translators: 1: Star rating given, 2: Maximum possible rating. */
											esc_html__( 'Rated %1$d out of %2$d', 'sifency-addons' ),
											5,
											5
										);
										?>
									</span>
								</div>
								<p class="meta">
									<strong class="woocommerce-review__author"><?php esc_html_e( 'John Doe', 'sifency-addons' ); ?></strong>
									<time class="woocommerce-review__published-date" datetime="2023-10-27">
										<?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( '2023-10-27' ) ) ); ?>
									</time>
								</p>
								<div class="description">
									<p><?php esc_html_e( 'This is a fantastic product! Highly recommended.', 'sifency-addons' ); ?></p>
								</div>
							</div>
						</li>
						<li class="review">
							<img src="<?php echo esc_url( get_avatar_url( 'placeholder2@example.com' ) ); ?>" class="avatar" height="60" width="60" alt="<?php esc_attr_e( 'Reviewer avatar', 'sifency-addons' ); ?>">
							<div class="comment-text">
								<div class="star-rating" role="img"
									<?php
									printf(
										'aria-label="%s"',
										/* translators: 1: Star rating given, 2: Maximum possible rating. */
										esc_attr( sprintf( __( 'Rated %1$d out of %2$d', 'sifency-addons' ), 4, 5 ) )
									);
									?>
									>
									<span style="width:80%">
										<?php
										printf(
											/* translators: 1: Star rating given, 2: Maximum possible rating. */
											esc_html__( 'Rated %1$d out of %2$d', 'sifency-addons' ),
											4,
											5
										);
										?>
									</span>
								</div>
								<p class="meta">
									<strong class="woocommerce-review__author"><?php esc_html_e( 'Jane Smith', 'sifency-addons' ); ?></strong>
									<time class="woocommerce-review__published-date" datetime="2023-10-26">
										<?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( '2023-10-26' ) ) ); ?>
									</time>
								</p>
								<div class="description">
									<p><?php esc_html_e( 'Good quality, but shipping was a bit slow.', 'sifency-addons' ); ?></p>
								</div>
							</div>
						</li>
					</ol>
				</div>
				<div id="review_form_wrapper">
					<div id="review_form">
						<div id="respond" class="comment-respond">
							<h3 id="reply-title" class="comment-reply-title"><?php esc_html_e( 'Add a review', 'sifency-addons' ); ?></h3>
							<form class="comment-form" method="post" id="commentform">
								<p class="comment-form-rating">
									<label for="rating"><?php esc_html_e( 'Your rating', 'sifency-addons' ); ?><span class="required">*</span></label>
									<select name="rating" id="rating" required>
										<option value=""><?php esc_html_e( 'Rate&hellip;', 'sifency-addons' ); ?></option>
										<option value="5"><?php esc_html_e( 'Perfect', 'sifency-addons' ); ?></option>
										<option value="4"><?php esc_html_e( 'Good', 'sifency-addons' ); ?></option>
										<option value="3"><?php esc_html_e( 'Average', 'sifency-addons' ); ?></option>
										<option value="2"><?php esc_html_e( 'Not that bad', 'sifency-addons' ); ?></option>
										<option value="1"><?php esc_html_e( 'Very poor', 'sifency-addons' ); ?></option>
									</select>
								</p>
								<p class="comment-form-comment">
									<label for="comment"><?php esc_html_e( 'Your review', 'sifency-addons' ); ?><span class="required">*</span></label>
									<textarea id="comment" name="comment" cols="45" rows="8" required></textarea>
								</p>
								<p class="comment-form-author">
									<label for="author"><?php esc_html_e( 'Name', 'sifency-addons' ); ?><span class="required">*</span></label>
									<input id="author" name="author" type="text" value="" size="30" aria-required="true" required>
								</p>
								<p class="comment-form-email">
									<label for="email"><?php esc_html_e( 'Email', 'sifency-addons' ); ?><span class="required">*</span></label>
									<input id="email" name="email" type="email" value="" size="30" aria-required="true" required>
								</p>
								<p class="form-submit">
									<input name="submit" type="submit" id="submit" class="submit" value="<?php esc_attr_e( 'Submit', 'sifency-addons' ); ?>">
									<?php wp_nonce_field( 'comment_form', 'comment_post_ID' ); ?>
								</p>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	}
}
