<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Repeater;
use Elementor\Plugin;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Sticky_Sections extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-sticky-sections',
			'title'          => 'Sifency Sticky Sections',
			'icon'           => 'eicon-scroll',
			'category'       => array( 'sifency-addons-widgets' ),
			'script_depends' => array( 'sifency-sticky-sections-script', 'lenis' ),
			'style_depends'  => array( 'sifency-pro-creative-style' ),
		);
	}

	protected function register_controls() {

		// --- CONTENT SECTION ---
		$this->start_controls_section( 'section_content', array( 'label' => 'Sticky Sections' ) );

		$repeater = new Repeater();

		$repeater->add_control(
			'content_type',
			array(
				'label'   => 'Content Type',
				'type'    => Controls_Manager::SELECT,
				'default' => 'editor',
				'options' => array(
					'editor'   => 'WP Editor',
					'template' => 'Elementor Template',
				),
			)
		);

		$repeater->add_control(
			'template_id',
			array(
				'label'     => 'Choose Template',
				'type'      => Controls_Manager::SELECT,
				'options'   => function_exists( 'sifency_addons_get_elementor_templates' )
							   ? sifency_addons_get_elementor_templates()
							   : array(),
				'condition' => array( 'content_type' => 'template' ),
			)
		);

		$repeater->add_control(
			'editor_content',
			array(
				'label'     => 'Section Content',
				'type'      => Controls_Manager::WYSIWYG,
				'default'   => '<h3>Sticky Section Title</h3><p>Content goes here...</p>',
				'condition' => array( 'content_type' => 'editor' ),
			)
		);

		$repeater->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'item_bg',
				'label'    => 'Background',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} {{CURRENT_ITEM}} .sifency-sticky-card',
			)
		);

		$this->add_control(
			'sections',
			array(
				'label'       => 'Cards',
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array( 'content_type' => 'editor' ),
					array( 'content_type' => 'editor' ),
				),
				'title_field' => 'Section Item',
			)
		);

		$this->end_controls_section();

		// --- ADVANCED SETTINGS ---
		$this->start_controls_section( 'section_settings', array( 'label' => 'Stack Logic' ) );

		$this->add_control(
			'sticky_top',
			array(
				'label'   => 'Top Spacing (px)',
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min' => 0,
						'max' => 200,
					),
				),
				'default' => array( 'size' => 50 ),
			)
		);

		$this->add_control(
			'card_height',
			array(
				'label'      => 'Card Min Height',
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vh' ),
				'range'      => array(
					'vh' => array(
						'min' => 30,
						'max' => 100,
					),
				),
				'default'    => array(
					'unit' => 'vh',
					'size' => 70,
				),
				'selectors'  => array( '{{WRAPPER}} .sifency-sticky-card' => 'min-height: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->add_control(
			'enable_scaling',
			array(
				'label'        => 'Scale Down Background Cards',
				'type'         => Controls_Manager::SWITCHER,
				'label_on'     => 'Yes',
				'label_off'    => 'No',
				'return_value' => 'yes',
				'default'      => 'yes',
			)
		);

		$this->end_controls_section();
		$this->add_sifency_addons_help_controls();
	}

	protected function render_dom( array $context ) {
		$wrapper_id = esc_attr( $context['dom_id'] );
		$settings   = $this->get_settings_for_display();
		?>
		<div class="sifency-sticky-stack-wrapper" 
			 data-top="<?php echo esc_attr( $settings['sticky_top']['size'] ); ?>"
			 data-scale="<?php echo esc_attr( $settings['enable_scaling'] ); ?>">
			
			<?php foreach ( $settings['sections'] as $index => $item ) : ?>
				<div class="sifency-sticky-item elementor-repeater-item-<?php echo esc_attr( $item['_id'] ); ?>">
					<div class="sifency-sticky-card">
						<?php
						if ( 'template' === $item['content_type'] && ! empty( $item['template_id'] ) ) {
                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor builder template output
							echo Plugin::$instance->frontend->get_builder_content_for_display( $item['template_id'] );
						} else {
							echo wp_kses_post( $item['editor_content'] );
						}
						?>
					</div>
				</div>
			<?php endforeach; ?>

		</div>
		<?php
	}
}
