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

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Image_Size;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Utils;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Orbit_Image extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Custom_CSS_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-orbit-image',
			'title'          => 'Sifency Orbit Glowing Image',
			'icon'           => 'eicon-image-box',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'gsap', 'gsap-scrolltrigger', 'sifency-orbit-image-script' ),
			'style_depends'  => array( 'sifency-master-style' ),
			'keywords'       => array( 'image', 'orbit', 'glow', 'rocket', 'light', 'border', 'gsap' ),
		);
	}

	protected function register_controls() {
		// --- Content Section ---
		$this->start_controls_section(
			'section_image',
			array( 'label' => esc_html__( 'Image Settings', 'sifency-addons' ) )
		);

		$this->add_control(
			'image',
			array(
				'label'   => esc_html__( 'Choose Image', 'sifency-addons' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => array( 'url' => Utils::get_placeholder_image_src() ),
			)
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'    => 'image',
				'default' => 'large',
			)
		);

		$this->end_controls_section();

		// --- Orbit Light Settings ---
		$this->start_controls_section(
			'section_orbit_settings',
			array( 'label' => esc_html__( 'Orbit Light Settings', 'sifency-addons' ) )
		);

		$this->add_control(
			'light_color_1',
			array(
				'label'       => esc_html__( 'Core Light Color', 'sifency-addons' ),
				'type'        => Controls_Manager::COLOR,
				'default'     => '#ff3c00',
				'description' => 'The brightest part of the moving light.',
				'selectors'   => array( '{{WRAPPER}} .sifency-orbit-border' => '--orbit-color-1: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'light_color_2',
			array(
				'label'       => esc_html__( 'Trail Light Color', 'sifency-addons' ),
				'type'        => Controls_Manager::COLOR,
				'default'     => '#ffc800',
				'description' => 'The fading tail of the light.',
				'selectors'   => array( '{{WRAPPER}} .sifency-orbit-border' => '--orbit-color-2: {{VALUE}};' ),
			)
		);

		$this->add_control(
			'border_thickness',
			array(
				'label'     => esc_html__( 'Border Thickness', 'sifency-addons' ),
				'type'      => Controls_Manager::SLIDER,
				'range'     => array(
					'px' => array(
						'min'  => 1,
						'max'  => 20,
						'step' => 1,
					),
				),
				'default'   => array( 'size' => 4 ),
				'selectors' => array( '{{WRAPPER}} .sifency-orbit-border' => '--orbit-thickness: {{SIZE}}px;' ),
			)
		);

		$this->add_control(
			'base_speed',
			array(
				'label'       => esc_html__( 'Idle Rotation Speed (s)', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min'  => 1,
						'max'  => 20,
						'step' => 1,
					),
				),
				'default'     => array( 'size' => 8 ),
				'description' => 'How many seconds it takes for 1 full rotation when NOT scrolling (Lower is faster).',
			)
		);

		$this->add_control(
			'scroll_multiplier',
			array(
				'label'       => esc_html__( 'Scroll Speed Multiplier', 'sifency-addons' ),
				'type'        => Controls_Manager::SLIDER,
				'range'       => array(
					'px' => array(
						'min'  => 1,
						'max'  => 10,
						'step' => 0.5,
					),
				),
				'default'     => array( 'size' => 3 ),
				'description' => 'How much the speed increases when you scroll fast.',
			)
		);

		$this->end_controls_section();

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

		$this->add_responsive_control(
			'width',
			array(
				'label'      => esc_html__( 'Width', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vh', 'vw', '%', 'em', 'rem' ),
				'range'      => array(
					'%'  => array(
						'min' => 20,
						'max' => 100,
					),
					'px' => array(
						'min' => 200,
						'max' => 800,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 100,
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-orbit-wrapper' => 'width: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->add_responsive_control(
			'aspect_ratio',
			array(
				'label'      => esc_html__( 'Aspect Ratio / Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vh', 'vw', '%', 'em', 'rem' ),
				'range'      => array(
					'px' => array(
						'min' => 200,
						'max' => 1000,
					),
					'vh' => array(
						'min' => 20,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'px',
					'size' => 450,
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-orbit-wrapper' => 'height: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->add_control(
			'border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'sifency-addons' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array(
					'{{WRAPPER}} .sifency-orbit-wrapper' => '--orbit-radius: {{TOP}}{{UNIT}}; border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
				'default'    => array(
					'top'    => '50',
					'bottom' => '50',
					'left'   => '50',
					'right'  => '50',
					'unit'   => '%',
				), // Circle by default
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'image_shadow',
				'selector' => '{{WRAPPER}} .sifency-orbit-wrapper',
			)
		);

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

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

		$image_url = Group_Control_Image_Size::get_attachment_image_src( $settings['image']['id'], 'image', $settings );
		if ( ! $image_url ) {
			$image_url = $settings['image']['url'];
		}

		$config = array(
			'baseSpeed'  => $settings['base_speed']['size'] ?? 8,
			'multiplier' => $settings['scroll_multiplier']['size'] ?? 3,
		);

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$wrapper_classes   = array( 'sifency-orbit-wrapper' );
		$wrapper_attrs     = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		if ( ! empty( $wrapper_id ) ) {
			$wrapper_attrs .= ' id="' . esc_attr( $wrapper_id ) . '"';
		}
		?>
		<div <?php echo wp_kses_post( $wrapper_attrs ); ?> data-config='<?php echo esc_attr( wp_json_encode( $config ) ); ?>'>
			
			<!-- The Rotating Light Border -->
			<div class="sifency-orbit-border"></div>

			<!-- Inner Image Container (Hides the parts of the border we don't want) -->
			<div class="sifency-orbit-inner">
				<img src="<?php echo esc_url( $image_url ); ?>" alt="Glowing Orbit Image">
			</div>

		</div>
		<?php
	}
}
