<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Plugin as ElementorPlugin;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Slider_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Widget_Nestedslider extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Slider_Controls;
	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-nested-slider',
			'title'          => 'Sifency Template Slider',
			'icon'           => 'eicon-slides',
			'category'       => array( 'sifency-advance-widgets' ),
			'keywords'       => array( 'slider', 'carousel', 'template', 'section', 'layout' ),
			'style_depends'  => array( 'swiper', 'sifency-frontend-style', 'sifency-icon' ),
			'script_depends' => array( 'swiper', 'sifency-frontend-script', 'sifency-carousel-handler' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->start_controls_section( 'section_slides', array( 'label' => 'Slides' ) );
		$repeater = new Repeater();
		$repeater->add_control(
			'slide_title',
			array(
				'label'   => 'Title',
				'type'    => Controls_Manager::TEXT,
				'default' => 'Slide Title',
			)
		);
		$repeater->add_control(
			'slide_template',
			array(
				'label'       => 'Select Template',
				'type'        => Controls_Manager::SELECT,
				'options'     => function_exists( 'sifency_addons_get_elementor_templates' ) ? sifency_addons_get_elementor_templates() : array(),
				'description' => __( 'Tip: For best results, use templates saved as "Sections".', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'slides',
			array(
				'label'       => 'Slides',
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => array( array( 'slide_title' => 'Slide #1' ), array( 'slide_title' => 'Slide #2' ) ),
				'title_field' => '{{{ slide_title }}}',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_slider_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_Sifency_Addons_Animation_Controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		if ( empty( $settings['slides'] ) ) {
			return; }

		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$slide_count       = count( $settings['slides'] );
		$slider_options    = $this->get_sifency_swiper_options( $settings, $slide_count );

		$this->add_render_attribute(
			'slider_container',
			array(
				'class'               => array( 'swiper-container', 'sifency-nested-slider-wrapper', 'sifency-carousel-wrapper' ),
				'data-swiper-options' => wp_json_encode( $slider_options ),
			)
		);

		$animation_attrs = $animation_manager->get_wrapper_attributes();
		if ( ! empty( $animation_attrs ) ) {
			preg_match( '/class="([^"]+)"/', $animation_attrs, $class_matches );
			if ( isset( $class_matches[1] ) ) {
				$this->add_render_attribute( '_wrapper', 'class', $class_matches[1] );
			}
			preg_match_all( '/(data-[^=]+="[^"]+")/', $animation_attrs, $data_matches );
			if ( isset( $data_matches[1] ) ) {
				foreach ( $data_matches[1] as $attr ) {
					list($key, $value) = explode( '=', $attr );
					$this->add_render_attribute( '_wrapper', $key, trim( $value, '"' ) );
				}
			}
		}
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'slider_container' ) . ' ' . $animation_attrs ); ?>>
			<div class="swiper-wrapper">
				<?php foreach ( $settings['slides'] as $slide ) : ?>
					<?php
					$slide_attrs = $animation_manager->get_item_attributes( array( 'swiper-slide' ) );
					?>
					<div <?php echo wp_kses_post( $slide_attrs ); ?>>
						<?php
						if ( ! empty( $slide['slide_template'] ) ) {
                             // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor already escapes content internally.
							echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $slide['slide_template'] );
						}
						?>
					</div>
				<?php endforeach; ?>
			</div>
		
			<?php $this->render_sifency_addons_slider_nav( $settings ); ?>
			<?php $this->render_sifency_addons_slider_pagination( $settings ); ?>
		</div>
		<?php
	}
}
