<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Icons_Manager;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Label_Content_Controls;
use Sifency\Traits\Sifency_Addons_TB_Separator_Content_Controls;
use Sifency\Traits\Render_Sifency_Addons_TB_Meta;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Sifency\Classes\Sifency_Themebuilder_Widget_Base;
class Sifency_Widget_Portfolio_Rating extends Sifency_Themebuilder_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Label_Content_Controls;
	use Sifency_Addons_TB_Separator_Content_Controls;
	use Render_Sifency_Addons_TB_Meta;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Help_Controls;

	protected function get_allowed_template_types() {
		return 'portfolio';
	}
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'          => 'sifency-portfolio-rating',
			'title'         => 'Portfolio Rating',
			'icon'          => 'eicon-meta-data',
			'category'      => array( 'sifency-portfolio-components' ),
			'keywords'      => array( 'portfolio', 'rating', 'stars', 'review', 'meta', 'dynamic', 'sifency' ),
			'style_depends' => array( 'sifency-theme-builder-style', 'sifency-typo-style' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_rating_content',
			array(
				'label' => esc_html__( 'Rating Settings', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'placeholder_rating',
			array(
				'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',
			array(
				'label'   => esc_html__( 'Maximum Rating', 'sifency-addons' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 10,
				'step'    => 1,
				'default' => 5,
			)
		);
		$this->add_control(
			'unmarked_style',
			array(
				'label'   => esc_html__( 'Unmarked Style', 'sifency-addons' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'solid'   => array(
						'title' => esc_html__( 'Solid', 'sifency-addons' ),
						'icon'  => 'eicon-star',
					),
					'outline' => array(
						'title' => esc_html__( 'Outline', 'sifency-addons' ),
						'icon'  => 'eicon-star-o',
					),
				),
				'default' => 'outline',
			)
		);
		$this->add_control(
			'show_rating_text',
			array(
				'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',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => esc_html__( 'Left', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'Right', 'sifency-addons' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'flex-start',
				'selectors' => array( '{{WRAPPER}} .sifency-meta-item' => 'justify-content: {{VALUE}};' ),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'label'    => esc_html__( 'Flex Layout', 'sifency-addons' ),
				'selector' => '.sifency-meta-item-inner',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'label_style',
				'label'    => 'Label Style',
				'selector' => '.sifency-label-style',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'separator_style',
				'label'    => 'Separator Style',
				'selector' => '.sifency-separator-style',
			)
		);

		$this->start_controls_section(
			'section_stars_style',
			array(
				'label' => esc_html__( 'Stars', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'star_size',
			array(
				'label'     => esc_html__( 'Size', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-star-rating' => 'font-size: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_responsive_control(
			'star_spacing',
			array(
				'label'     => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 50,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-star-rating .sifency-star' => 'letter-spacing: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->add_control(
			'star_color_marked',
			array(
				'label'     => esc_html__( 'Color (Marked)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#f2b01e',
				'selectors' => array( '{{WRAPPER}} .sifency-star-rating-filled' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'star_color_unmarked',
			array(
				'label'     => esc_html__( 'Color (Unmarked)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#cccccc',
				'selectors' => array( '{{WRAPPER}} .sifency-star-rating-unfilled' => 'color: {{VALUE}};' ),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_rating_text_style',
			array(
				'label'     => esc_html__( 'Rating Text', 'sifency-addons' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => array( 'show_rating_text' => 'yes' ),
			)
		);
		$this->add_control(
			'rating_text_color',
			array(
				'label'     => esc_html__( 'Color', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-rating-text' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			array(
				'name'     => 'rating_text_typography',
				'selector' => '{{WRAPPER}} .sifency-rating-text',
			)
		);
		$this->add_responsive_control(
			'rating_text_spacing',
			array(
				'label'     => esc_html__( 'Spacing', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 0,
						'max' => 50,
					),
				),
				'selectors' => array( '{{WRAPPER}} .sifency-rating-text' => 'margin-left: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_label_content_controls();
		$this->add_sifency_addons_separator_content_controls();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'meta_style',
				'label'    => 'Portfolio Dynamic Rating',
				'selector' => '.sifency-meta-style',
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
	}
	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$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_Addons_Animation_Manager( $settings );
		$animation_attrs   = $animation_manager->get_wrapper_attributes( array() );

		$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 id="<?php echo esc_attr ( $wrapper_id ); ?>" 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
	}
}
