<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
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_Animation_Controls;
use \Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Product_Reviews extends Widget_Base {
    use Sifency_Animation_Controls;

    public function get_name() {
        return 'sifency-product-reviews';
    }

    public function get_title() {
        return __('Sifency Product Reviews', 'sifency-addons');
    }

    public function get_icon() {
        return 'eicon-review';
    }

    public function get_categories() {
        return ['sifency-addons-woo-widgets'];
    }

    public function get_keywords() {
        return ['product', 'reviews', 'comments', 'rating', 'feedback', 'woocommerce'];
    }

    public function get_style_depends() {
	    return [ 'sifency-frontend-style' ];
    }

    protected function register_controls() {
        // --- Dependency Check ---
        if ( ! class_exists('woocommerce') ) {
            $this->start_controls_section('woo_notice', [ 'label' => esc_html__('Notice', 'sifency-addons') ]);
            $this->add_control('woo_notice_control', [ 'type' => Controls_Manager::RAW_HTML, 'raw' => '<div class="elementor-panel-alert elementor-panel-alert-warning">' . esc_html__('WooCommerce is not installed or active.', 'sifency-addons') . '</div>' ]);
            $this->end_controls_section();
            return;
        }

		// --- Style Section: General ---
        $this->start_controls_section(
            'section_style_general',
            [ 'label' => esc_html__('General', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
        );
		$this->add_control(
            'text_color',
            [ 'label' => esc_html__( 'Text Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-product-reviews' => 'color: {{VALUE}};' ] ]
        );
        $this->add_control(
            'link_color',
            [ 'label' => esc_html__( 'Link Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-product-reviews a' => 'color: {{VALUE}};' ] ]
        );
        $this->add_control(
            'link_hover_color',
            [ 'label' => esc_html__( 'Link Hover Color', 'sifency-addons' ), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .sifency-product-reviews a:hover' => 'color: {{VALUE}};' ] ]
        );
		$this->end_controls_section();


		// --- Style Section: Headings ---
        $this->start_controls_section(
            'section_style_headings',
            [ 'label' => esc_html__('Headings', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
        );
		$this->add_control( 'headings_color', [ 'label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} #reviews .comments-title, {{WRAPPER}} #reviews #reply-title' => 'color: {{VALUE}};' ] ]);
		$this->add_group_control( Group_Control_Typography::get_type(), [ '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',
            [ 'label' => esc_html__('Star Ratings', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
        );
		$this->add_responsive_control( 'stars_size', [ 'label' => esc_html__('Size', 'sifency-addons'), 'type' => Controls_Manager::SLIDER, 'selectors' => [ '{{WRAPPER}} .star-rating' => 'font-size: {{SIZE}}{{UNIT}};' ] ]);
		$this->add_control( 'stars_color', [ 'label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .star-rating span::before, {{WRAPPER}} .star-rating::before' => 'color: {{VALUE}};' ] ]);
		$this->add_control( 'stars_unmarked_color', [ 'label' => esc_html__('Unmarked Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .star-rating::before' => 'color: {{VALUE}};' ] ]);
		$this->end_controls_section();

		// --- Style Section: Review Form ---
		$this->start_controls_section(
            'section_style_form',
            [ 'label' => esc_html__('Review Form', 'sifency-addons'), 'tab' => Controls_Manager::TAB_STYLE ]
        );
		$this->add_control('heading_labels', [ 'type' => Controls_Manager::HEADING, 'label' => esc_html__('Labels', 'sifency-addons') ]);
        $this->add_control('label_color', [ 'label' => esc_html__('Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .comment-form label' => 'color: {{VALUE}};' ] ]);
        $this->add_group_control(Group_Control_Typography::get_type(), [ 'name' => 'label_typography', 'selector' => '{{WRAPPER}} .comment-form label' ]);
        
		$this->add_control('heading_fields', [ 'type' => Controls_Manager::HEADING, 'label' => esc_html__('Fields', 'sifency-addons'), 'separator' => 'before' ]);
		$this->add_control('field_color', [ 'label' => esc_html__('Text Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .comment-form input:not([type=submit]), {{WRAPPER}} .comment-form textarea' => 'color: {{VALUE}};' ] ]);
        $this->add_control('field_bg_color', [ 'label' => esc_html__('Background Color', 'sifency-addons'), 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .comment-form input:not([type=submit]), {{WRAPPER}} .comment-form textarea' => 'background-color: {{VALUE}};' ] ]);
		$this->add_group_control(Group_Control_Border::get_type(), [ 'name' => 'field_border', 'selector' => '{{WRAPPER}} .comment-form input:not([type=submit]), {{WRAPPER}} .comment-form textarea' ]);
		
		$this->add_control('heading_button', [ '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', ['label' => 'Normal']);
        $this->add_control( 'button_bg', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .comment-form input[type="submit"]' => 'background-color: {{VALUE}};' ] ]);
        $this->add_control( 'button_text_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .comment-form input[type="submit"]' => 'color: {{VALUE}};' ] ]);
		$this->add_group_control(Group_Control_Border::get_type(), [ 'name' => 'button_border', 'selector' => '{{WRAPPER}} .comment-form input[type="submit"]' ]);
        $this->end_controls_tab();
        $this->start_controls_tab('button_hover', ['label' => 'Hover']);
        $this->add_control( 'button_hover_bg', [ 'label' => 'Background', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .comment-form input[type="submit"]:hover' => 'background-color: {{VALUE}};' ] ]);
        $this->add_control( 'button_hover_text_color', [ 'label' => 'Text Color', 'type' => Controls_Manager::COLOR, 'selectors' => [ '{{WRAPPER}} .comment-form input[type="submit"]:hover' => 'color: {{VALUE}};' ] ]);
		$this->add_group_control(Group_Control_Border::get_type(), [ '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_animation_controls();
        $this->update_control(
            'sifency_animation_target',
            [ 'type' => Controls_Manager::HIDDEN, 'default' => 'wrapper' ]
        );
    }

    /**
     * Render the widget output on the frontend.
     *
     * @since 1.0.4
     * @access protected
     */
    protected function render() {
        if ( ! class_exists('woocommerce') ) { return; }

        $settings = $this->get_settings_for_display();
        $animation_manager = new Sifency_Animation_Manager($settings);
        $wrapper_attrs_string = $animation_manager->get_wrapper_attributes(['sifency-product-reviews']);
        
        echo '<div ' . wp_kses_post ($wrapper_attrs_string) . '>';

        if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
            $this->render_editor_placeholder();
        } else {
            if ( ! is_product() ) {
                echo '<div class="sifency-placeholder">' . esc_html__('Reviews can only be displayed on a single product page.', 'sifency-addons') . '</div>';
            } else {
                comments_template();
            }
        }

        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
    }

}