<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class Sifency_Scroll_BG_Changer 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_bg_changer',
			'title'          => 'Sifency Scroll Background Changer',
			'icon'           => 'eicon-paint-brush',
			'category'       => 'sifency-advance-widgets',
			'keywords'       => array( 'background', 'scroll', 'morph', 'color' ),
			'style_depends'  => array( 'sifency-scroll-bg-style' ),
			'script_depends' => array( 'sifency-on-scroll-bg-script' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_target',
			array( 'label' => esc_html__( 'Target Settings', 'sifency-addons' ) )
		);
		$this->add_control(
			'target_selector',
			array(
				'label'   => esc_html__( 'Apply background to', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'body',
				'options' => array(
					'body'   => esc_html__( 'Entire Page (body)', 'sifency-addons' ),
					'parent' => esc_html__( 'Parent Section / Container', 'sifency-addons' ),
					'custom' => esc_html__( 'Custom CSS selector', 'sifency-addons' ),
				),
			)
		);
		$this->add_control(
			'custom_selector',
			array(
				'label'       => esc_html__( 'Custom Selector', 'sifency-addons' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => '#page, .site-content, .my-class',
				'condition'   => array( 'target_selector' => 'custom' ),
			)
		);
		$this->end_controls_section();
		$this->start_controls_section(
			'section_colors',
			array( 'label' => esc_html__( 'Color Steps', 'sifency-addons' ) )
		);
		$repeater = new Repeater();
		$repeater->add_control(
			'scroll_point',
			array(
				'label'      => esc_html__( 'Position (%)', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'range'      => array(
					'%' => array(
						'min' => 0,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 0,
				),
			)
		);
		$repeater->add_control(
			'step_color',
			array(
				'label' => esc_html__( 'Color', 'sifency-addons' ),
				'type'  => Controls_Manager::COLOR,
			)
		);
		$this->add_control(
			'color_steps',
			array(
				'label'       => esc_html__( 'Color Steps', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => '<# print( scroll_point.size + "%  —  " + step_color ) #>',
			)
		);
		$this->add_control(
			'transition_duration',
			array(
				'label'   => esc_html__( 'Transition (seconds)', 'sifency-addons' ),
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 0.1,
						'max'  => 2,
						'step' => 0.05,
					),
				),
				'default' => array( 'size' => 0.5 ),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
	}
	protected function render_dom( array $context ) {
		$settings   = $this->get_settings_for_display();
		$wrapper_id = esc_attr( $context['dom_id'] );
		$steps      = array();
		foreach ( $settings['color_steps'] ?? array() as $item ) {
			$steps[] = array(
				'point' => floatval( $item['scroll_point']['size'] ?? 0 ),
				'color' => $item['step_color'] ?? 'transparent',
			);
		}

		usort(
			$steps,
			function( $a, $b ) {
				return $a['point'] <=> $b['point'];
			}
		);

		$config = array(
			'target'   => $settings['target_selector'] ?? 'body',
			'custom'   => trim( $settings['custom_selector'] ?? '' ),
			'duration' => floatval( $settings['transition_duration']['size'] ?? 0.5 ),
			'steps'    => $steps,
		);

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" 
			class="sifency-scroll-bg-handler"
			data-config="<?php echo esc_attr( wp_json_encode( $config ) ); ?>"
			aria-hidden="true"></div>

		<style>
			.sifency-scroll-bg-target {
				transition: background-color <?php echo esc_attr( $config['duration'] ); ?>s ease-in-out !important;
			}
		</style>
		<?php
	}
}
