<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Animation_Controls; 
use Sifency\Traits\Sifency_Animation_Manager;  
use Sifency\Traits\Sifency_Label_Content_Controls_Trait;
use Sifency\Traits\Sifency_Separator_Content_Controls_Trait;
use Sifency\Traits\Sifency_Render_Meta_Trait;
use Sifency\Traits\Sifency_Core_Text_Style;

class Sifency_Widget_Portfolio_Rating extends Widget_Base {
	use Sifency_Animation_Controls;
	use Sifency_Label_Content_Controls_Trait;
	use Sifency_Separator_Content_Controls_Trait;
	use Sifency_Render_Meta_Trait;
	use Sifency_Core_Text_Style;

	public function get_name() {
		return 'sifency-portfolio-rating';
	}

	public function get_title() {
		return esc_html__( 'Portfolio Rating', 'sifency-addons' );
	}

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

	public function get_categories() {
		return [ 'sifency-portfolio-components' ]; 
	}

	public function get_keywords() {
		return [ 'portfolio', 'rating', 'stars', 'review', 'meta', 'dynamic', 'sifency' ];
	}

    public function get_style_depends() {
	    return [ 'sifency-theme-builder-style', 'sifency-typo-style' ];
    }

	protected function register_controls() {
		// --- Content Tab: Rating Settings ---
		$this->start_controls_section(
			'section_rating_content',
			[
				'label' => esc_html__( 'Rating Settings', 'sifency-addons' ),
			]
		);

		$this->add_control(
			'placeholder_rating',
			[
				'label'       => esc_html__( 'Placeholder Rating', 'sifency-addons' ),
				'type'        => Controls_Manager::NUMBER,
				'min'         => 0,
				'max'         => 5,
				'step'        => 0.1,
				'default'     => 4.5,
				'description' => esc_html__( 'This rating will be shown in the editor when the dynamic field is empty.', 'sifency-addons' ),
			]
		);

		$this->add_control(
			'max_rating',
			[
				'label'   => esc_html__( 'Maximum Rating', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 10,
				'step'    => 1,
				'default' => 5,
			]
		);

		$this->add_control(
			'unmarked_style',
			[
				'label'   => esc_html__( 'Unmarked Style', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => [
					'solid'   => [ 'title' => esc_html__( 'Solid', 'sifency-addons' ), 'icon' => 'eicon-star' ],
					'outline' => [ 'title' => esc_html__( 'Outline', 'sifency-addons' ), 'icon' => 'eicon-star-o' ],
				],
				'default' => 'outline',
			]
		);

		$this->add_control(
			'show_rating_text',
			[
				'label'        => esc_html__( 'Show Rating Text', 'sifency-addons' ),
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'sifency-addons' ),
				'label_off'    => esc_html__( 'Hide', 'sifency-addons' ),
				'return_value' => 'yes',
				'default'      => 'yes',
				'separator'    => 'before',
			]
		);

		$this->add_responsive_control(
			'align',
			[
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [
					'flex-start' => [ 'title' => esc_html__( 'Left', 'sifency-addons' ), 'icon' => 'eicon-text-align-left' ],
					'center'     => [ 'title' => esc_html__( 'Center', 'sifency-addons' ), 'icon' => 'eicon-text-align-center' ],
					'flex-end'   => [ 'title' => esc_html__( 'Right', 'sifency-addons' ), 'icon' => 'eicon-text-align-right' ],
				],
				'default'   => 'flex-start',
				'selectors' => [ '{{WRAPPER}} .sifency-meta-item' => 'justify-content: {{VALUE}};' ],
			]
		);

		$this->end_controls_section();

        $this->add_sifency_advanced_text_style([
            'prefix'   => 'label_style',
            'label'    => 'Label Style',
            'selector' => '.sifency-label-style',
			'target_children' => false,
        ]);

        $this->add_sifency_advanced_text_style([
            'prefix'   => 'separator_style',
            'label'    => 'Separator Style',
            'selector' => '.sifency-separator-style',
			'target_children' => false,
        ]);

		// --- Style Tab: Stars Styling ---
		$this->start_controls_section(
			'section_stars_style',
			[
				'label' => esc_html__( 'Stars', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_responsive_control(
			'star_size',
			[
				'label'      => esc_html__( 'Size', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 100 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-star-rating' => 'font-size: {{SIZE}}{{UNIT}};' ],
			]
		);

		$this->add_responsive_control(
			'star_spacing',
			[
				'label'      => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 50 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-star-rating .sifency-star' => 'letter-spacing: {{SIZE}}{{UNIT}};' ],
			]
		);

		$this->add_control(
			'star_color_marked',
			[
				'label'     => esc_html__( 'Color (Marked)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#f2b01e',
				'selectors' => [ '{{WRAPPER}} .sifency-star-rating-filled' => 'color: {{VALUE}};' ],
			]
		);

		$this->add_control(
			'star_color_unmarked',
			[
				'label'     => esc_html__( 'Color (Unmarked)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#cccccc',
				'selectors' => [ '{{WRAPPER}} .sifency-star-rating-unfilled' => 'color: {{VALUE}};' ],
			]
		);

		$this->end_controls_section();

		// --- Style Tab: Rating Text Styling ---
		$this->start_controls_section(
			'section_rating_text_style',
			[
				'label'     => esc_html__( 'Rating Text', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 'show_rating_text' => 'yes' ],
			]
		);

		$this->add_control(
			'rating_text_color',
			[
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-rating-text' => 'color: {{VALUE}};' ],
			]
		);
		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name'     => 'rating_text_typography',
				'selector' => '{{WRAPPER}} .sifency-rating-text',
			]
		);
		$this->add_responsive_control(
			'rating_text_spacing',
			[
				'label'      => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'range'      => [ 'px' => [ 'min' => 0, 'max' => 50 ] ],
				'selectors'  => [ '{{WRAPPER}} .sifency-rating-text' => 'margin-left: {{SIZE}}{{UNIT}};' ],
			]
		);

		$this->end_controls_section();

		// Call the methods from our reusable traits
		$this->add_sifency_label_content_controls();
		$this->add_sifency_separator_content_controls();
        $this->add_sifency_advanced_text_style([
            'prefix'   => 'meta_style',
            'label'    => 'Portfolio Dynamic Rating',
            'selector' => '.sifency-meta-style',
			'target_children' => false,
        ]);
		$this->add_sifency_animation_controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();

		$rating_value = sifency_get_field( 'portfolio_rating' );
		$display_rating = 0;
		if ( ! empty( $rating_value ) || '0' === $rating_value ) {
			$display_rating = (float) $rating_value;
		} 
		elseif ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
			$display_rating = (float) $settings['placeholder_rating'];
		} else {
			return;
		}

		$animation_manager = new Sifency_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( [] );
		
		$max_rating    = (int) $settings['max_rating'];
		$percentage    = ( $display_rating / $max_rating ) * 100;
		$star_icon_unmarked = ( 'solid' === $settings['unmarked_style'] ) ? 'eicon-star' : 'eicon-star-o';
		$star_icon_marked   = 'eicon-star';

		?>
		<div class="sifency-meta-item" <?php echo $animation_attrs; // phpcs:ignore ?>>
			<div class="sifency-meta-item-inner">
				<?php if ( 'yes' === $settings['show_label'] && ! empty( $settings['label_text'] ) ) : ?>
					<span class="sifency-label-style"><?php echo esc_html( $settings['label_text'] ); ?></span>
				<?php endif; ?>
				<?php if ( 'yes' === $settings['show_separator'] && ! empty( $settings['separator_text'] ) ) : ?>
					<span class="sifency-separator-style"><?php echo esc_html( $settings['separator_text'] ); ?></span>
				<?php endif; ?>

				<div class="sifency-star-rating-wrapper">
					<div class="sifency-star-rating">
						<div class="sifency-star-rating-unfilled">
							<?php for ( $i = 0; $i < $max_rating; $i++ ) {
								echo '<i class="sifency-star ' . esc_attr( $star_icon_unmarked ) . '"></i>';
							} ?>
						</div>
						<div class="sifency-star-rating-filled" style="width: <?php echo esc_attr( $percentage ); ?>%;">
							<?php for ( $i = 0; $i < $max_rating; $i++ ) {
								echo '<i class="sifency-star ' . esc_attr( $star_icon_marked ) . '"></i>';
							} ?>
						</div>
					</div>
					<?php if ( 'yes' === $settings['show_rating_text'] ) : ?>
						<span class="sifency-rating-text">(<?php echo esc_html( number_format( $display_rating, 1 ) ); ?>)</span>
					<?php endif; ?>
				</div>
			</div>
		</div>
		<?php
	}
}