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

use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Box_Shadow;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Reveal_Sections 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-reveal-sections',
			'title'          => 'Sifency Reveal Sections (Scale)',
			'icon'           => 'eicon-clone',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'gsap', 'gsap-scrolltrigger', 'sifency-reveal-sections-script' ),
			'style_depends'  => array( 'sifency-master-style' ),
			'keywords'       => array( 'reveal', 'scale', 'uncover' ),
		);
	}

	protected function register_controls() {
		// --- Content Section (Repeater) ---
		$this->start_controls_section(
			'section_content',
			array( 'label' => esc_html__( 'Stacked Sections', 'sifency-addons' ) )
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'template_id',
			array(
				'label'       => __( 'Choose Template', 'sifency-addons' ),
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'options'     => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
			)
		);

		$repeater->add_control(
			'section_bg',
			array(
				'label' => esc_html__( 'Section Background', 'sifency-addons' ),
				'type'  => Controls_Manager::COLOR,
			)
		);

		$this->add_control(
			'sections_list',
			array(
				'label'       => esc_html__( 'Sections', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => 'Template ID: {{{ template_id }}}',
			)
		);

		$this->end_controls_section();

		// --- Effect Settings ---
		$this->start_controls_section(
			'section_effect',
			array( 'label' => esc_html__( 'Effect Settings', 'sifency-addons' ) )
		);

		$this->add_control(
			'scale_amount',
			array(
				'label'   => esc_html__( 'Scale Down To', 'sifency-addons' ),
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 0.5,
						'max'  => 0.95,
						'step' => 0.05,
					),
				),
				'default' => array( 'size' => 0.85 ),
			)
		);

		$this->add_control(
			'darken_amount',
			array(
				'label'   => esc_html__( 'Darken Background (%)', 'sifency-addons' ),
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 0,
						'max'  => 1,
						'step' => 0.1,
					),
				),
				'default' => array( 'size' => 0.6 ),
			)
		);

		$this->add_control(
			'disable_mobile',
			array(
				'label'   => esc_html__( 'Disable on Mobile', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->end_controls_section();

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

		$this->add_responsive_control(
			'panel_width',
			array(
				'label'      => esc_html__( 'Panel Width', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%', 'px', 'vw' ),
				'range'      => array(
					'%'  => array(
						'min' => 50,
						'max' => 100,
					),
					'px' => array(
						'min' => 300,
						'max' => 1400,
					),
				),
				'default'    => array(
					'unit' => '%',
					'size' => 100,
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-reveal-panel' => 'width: {{SIZE}}{{UNIT}}; margin: 0 auto;' ),
			)
		);

		$this->add_responsive_control(
			'panel_min_height',
			array(
				'label'      => esc_html__( 'Min Height', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'vh', 'px', 'em' ),
				'range'      => array(
					'vh' => array(
						'min' => 10,
						'max' => 100,
					),
					'px' => array(
						'min' => 100,
						'max' => 1000,
					),
				),
				'default'    => array(
					'unit' => 'vh',
					'size' => 100,
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-reveal-panel' => 'min-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-reveal-panel' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
				'default'    => array(
					'top'    => '0',
					'bottom' => '0',
					'left'   => '0',
					'right'  => '0',
					'unit'   => 'px',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'panel_box_shadow',
				'label'    => esc_html__( 'Box Shadow', 'sifency-addons' ),
				'selector' => '{{WRAPPER}} .sifency-reveal-panel',
			)
		);

		$this->end_controls_section();
	}

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

		if ( empty( $sections ) || count( $sections ) < 2 ) {
			echo '<div class="elementor-alert elementor-alert-warning">Please add at least 2 sections.</div>';
			return;
		}

		$config = array(
			'scale'  => $settings['scale_amount']['size'] ?? 0.85,
			'darken' => $settings['darken_amount']['size'] ?? 0.6,
			'mobile' => $settings['disable_mobile'] === 'yes' ? true : false,
		);

		static $template_cache = array();
		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>"
			class="sifency-reveal-wrapper"
			data-config='<?php echo esc_attr( wp_json_encode( $config ) ); ?>'>

			<?php
			foreach ( $sections as $index => $sec ) :
				if ( empty( $sec['template_id'] ) ) {
					continue;
				}

				$is_last = ( $index === count( $sections ) - 1 ) ? 'sifency-reveal-last' : '';
				$bg      = ! empty( $sec['section_bg'] ) ? esc_attr( $sec['section_bg'] ) : 'transparent';

				if ( ! isset( $template_cache[ $sec['template_id'] ] ) ) {
					$template_cache[ $sec['template_id'] ] =
						\Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $sec['template_id'] );
				}
				?>
				<div class="sifency-reveal-pin-wrap <?php echo esc_attr( $is_last ); ?>">
					<div class="sifency-reveal-panel" style="background-color: <?php echo esc_attr( $bg ); ?>;">
						
						<div class="sifency-reveal-inner">
							<?php echo wp_kses_post( $template_cache[ $sec['template_id'] ] ); ?>
						</div>

						<div class="sifency-reveal-overlay"></div>
					</div>
				</div>
			<?php endforeach; ?>

		</div>
		<?php
	}
}
