<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Scroll_Progress extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-scroll-progress',
			'title'          => 'Sifency Scroll Progress',
			'icon'           => 'eicon-loader',
			'category'       => array( 'sifency-creative-widgets' ),
			// GSAP and ScrollTrigger
			'script_depends' => array( 'gsap', 'gsap-scrolltrigger', 'sifency-scroll-progress-script', 'lenis' ),
			'style_depends'  => array( 'sifency-creative-style' ),
			'keywords'       => array( 'scroll', 'progress', 'indicator', 'reading bar', 'circular', 'back to top' ),
		);
	}

	protected function register_controls() {
		// --- Layout Section ---
		$this->start_controls_section(
			'section_layout',
			array( 'label' => esc_html__( 'Indicator Layout', 'sifency-addons' ) )
		);

		$this->add_control(
			'indicator_type',
			array(
				'label'   => esc_html__( 'Indicator Style', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'circle',
				'options' => array(
					'circle' => 'Circular Ring (Modern)',
					'hbar'   => 'Horizontal Reading Bar',
					'vbar'   => 'Vertical Line',
				),
			)
		);

		$this->add_control(
			'position',
			array(
				'label'     => esc_html__( 'Position', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'bottom-right',
				'options'   => array(
					'top-left'     => 'Top Left',
					'top-right'    => 'Top Right',
					'bottom-left'  => 'Bottom Left',
					'bottom-right' => 'Bottom Right',
				),
				'condition' => array(
					'indicator_type' => array( 'circle', 'vbar' ),
				),
			)
		);

		$this->add_control(
			'hbar_position',
			array(
				'label'     => esc_html__( 'Bar Position', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'default'   => 'top',
				'options'   => array(
					'top'    => array(
						'title' => 'Top',
						'icon'  => 'eicon-v-align-top',
					),
					'bottom' => array(
						'title' => 'Bottom',
						'icon'  => 'eicon-v-align-bottom',
					),
				),
				'condition' => array( 'indicator_type' => 'hbar' ),
			)
		);

		$this->add_control(
			'show_percentage',
			array(
				'label'     => esc_html__( 'Show % Text', 'sifency-addons' ),
				'type'      => Controls_Manager::SWITCHER,
				'default'   => 'yes',
				'condition' => array( 'indicator_type' => 'circle' ),
			)
		);

		$this->add_control(
			'back_to_top',
			array(
				'label'       => esc_html__( 'Enable Back to Top (Click)', 'sifency-addons' ),
				'type'        => Controls_Manager::SWITCHER,
				'default'     => 'yes',
				'description' => 'Clicking the indicator smoothly scrolls back to the top (Lenis Compatible).',
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
		// --- Style Section ---
		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Colors & Styling', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_control(
			'track_color',
			array(
				'label'     => esc_html__( 'Track Color (Background)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => 'rgba(0,0,0,0.1)',
				'selectors' => array(
					'{{WRAPPER}} .sifency-progress-track' => 'stroke: {{VALUE}}; background-color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'fill_color',
			array(
				'label'     => esc_html__( 'Fill Color (Active Progress)', 'sifency-addons' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#000000',
				'selectors' => array(
					'{{WRAPPER}} .sifency-progress-fill' => 'stroke: {{VALUE}}; background-color: {{VALUE}};',
					'{{WRAPPER}} .sifency-progress-text' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'mix_blend',
			array(
				'label'     => esc_html__( 'Mix Blend Mode (Modern Effect)', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'normal',
				'options'   => array(
					'normal'     => 'Normal',
					'difference' => 'Difference (Inverts color based on background)',
					'exclusion'  => 'Exclusion',
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-scroll-progress-wrapper' => 'mix-blend-mode: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'size',
			array(
				'label'     => esc_html__( 'Size / Thickness', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min' => 2,
						'max' => 100,
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .sifency-type-circle svg' => 'width: {{SIZE}}px; height: {{SIZE}}px;',
					'{{WRAPPER}} .sifency-type-hbar'       => 'height: {{SIZE}}px;',
					'{{WRAPPER}} .sifency-type-vbar'       => 'width: {{SIZE}}px;',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'      => 'percentage_typography',
				'selector'  => '{{WRAPPER}} .sifency-progress-text',
				'condition' => array( 'indicator_type' => 'circle' ),
			)
		);

		$this->end_controls_section();
	}

	protected function render_dom( array $context ) {
		$settings   = $this->get_settings_for_display();
		$wrapper_id = esc_attr( $context['dom_id'] );

		$type = $settings['indicator_type'];
		$pos  = $type === 'hbar' ? $settings['hbar_position'] : $settings['position'];

		$data_attributes = array(
			'data-type' => $type,
			'data-btt'  => $settings['back_to_top'] === 'yes' ? 'true' : 'false',
		);

		// Combine CSS classes for positioning
		$wrapper_classes = "sifency-scroll-progress-wrapper sifency-type-{$type} sifency-pos-{$pos}";
		if ( $settings['back_to_top'] === 'yes' ) {
			$wrapper_classes .= ' sifency-clickable';
		}

		?>
		<!-- Appending to body is best practice for fixed elements, but Elementor outputs here. CSS will handle 'fixed' -->
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="<?php echo esc_attr( $wrapper_classes ); ?>" 
			data-type="<?php echo esc_attr( $data_attributes['data-type'] ); ?>"
			data-btt="<?php echo esc_attr( $data_attributes['data-btt'] ); ?>">
			
			<?php if ( $type === 'circle' ) : ?>
				<!-- Circular SVG -->
				<svg viewBox="0 0 100 100" class="sifency-progress-svg">
					<circle class="sifency-progress-track" cx="50" cy="50" r="46"></circle>
					<circle class="sifency-progress-fill" cx="50" cy="50" r="46"></circle>
				</svg>
				<?php if ( $settings['show_percentage'] === 'yes' ) : ?>
					<span class="sifency-progress-text">0%</span>
				<?php endif; ?>

			<?php else : ?>
				<!-- Horizontal or Vertical Bar -->
				<div class="sifency-progress-track">
					<div class="sifency-progress-fill"></div>
				</div>
			<?php endif; ?>

		</div>
		<?php
	}
}
