<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Plugin as ElementorPlugin;
use Sifency\Traits\Sifency_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Slider_Controls;
use Sifency\Traits\Sifency_Animation_Manager;

class Sifency_Widget_Nestedslider extends Widget_Base {
    use Sifency_Animation_Controls;
    use Sifency_Addons_Slider_Controls;

    public function get_name() {
        return 'sifency-nested-slider';
    }

    public function get_title() {
        return __('Sifency Nested Slider', 'sifency-addons');
    }

    public function get_icon() {
        return 'eicon-slides';
    }

    public function get_categories() {
        return ['sifency-advance-widgets'];
    }
    
    public function get_script_depends() {
        return $this->get_carousel_script_depends();
    }

    public function get_style_depends() {
        return $this->get_carousel_style_depends();
    }
    
    private function sifency_addons_sifency_addons_get_elementor_templates() {
        $options = [];
        if ( class_exists( '\Elementor\Plugin' ) ) {
            $templates = ElementorPlugin::$instance->templates_manager->get_source( 'local' )->get_items();
            if ( ! empty( $templates ) ) {
                foreach ( $templates as $template ) {
                    $options[ $template['template_id'] ] = $template['title'] . ' (' . $template['type'] . ')';
                }
            }
        }
        return $options;
    }

    protected function register_controls() {
        // --- Slides Section ---
        $this->start_controls_section( 'section_slides', [ 'label' => 'Slides' ] );
        $repeater = new Repeater();
        $repeater->add_control( 'slide_title', [ 'label' => 'Title', 'type' => Controls_Manager::TEXT, 'default' => 'Slide Title' ] );
        $repeater->add_control(
			'slide_template',
			[ 
                'label' => 'Select Template', 
                'type' => Controls_Manager::SELECT,
                'options' => $this->sifency_addons_sifency_addons_get_elementor_templates(),
                'description' => __('Tip: For best results, use templates saved as "Sections".', 'sifency-addons'),
            ]
		);
        $this->add_control(
            'slides',
            [
                'label' => 'Slides', 'type' => Controls_Manager::REPEATER, 'fields' => $repeater->get_controls(),
                'default' => [ [ 'slide_title' => 'Slide #1' ], [ 'slide_title' => 'Slide #2' ] ],
                'title_field' => '{{{ slide_title }}}',
            ]
        );
                $this->add_control(
            'layout_style',
            [
                'label'   => esc_html__( 'Layout', 'sifency-addons' ),
                'type'    => Controls_Manager::SELECT,
                'options' => [
                    'grid'     => esc_html__( 'Grid', 'sifency-addons' ),
                    'carousel' => esc_html__( 'Carousel', 'sifency-addons' ),
                ],
                'default' => 'grid',
            ]
        );
        $this->end_controls_section();

        $this->add_sifency_carousel_controls();
        $this->add_sifency_animation_controls();
    }

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

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

        $this->add_render_attribute('_wrapper', [
            'class' => ['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, '"'));
                }
            }
        }
        ?>
        <div class="swiper-container">
            <div class="swiper-wrapper">
                <?php foreach ($settings['slides'] as $slide) : ?>
                    <?php
                    $slide_attrs = $animation_manager->get_item_attributes(['swiper-slide']);
                    ?>
                    <div <?php echo wp_kses_post ($slide_attrs); ?>>
                        <?php
                        if ( ! empty($slide['slide_template']) ) {
                            echo wp_kses_post (\Elementor\Plugin::$instance->frontend->get_builder_content_for_display($slide['slide_template']));
                        }
                        ?>
                    </div>
                <?php endforeach; ?>
            </div>
        
            <?php if ( ! empty($settings['show_arrows']) && 'yes' === $settings['show_arrows'] ) : ?>
                <div class="sifency-slider-nav-container">
                <div class="swiper-button-prev sifency-slider-nav-arrow sifency-slider-nav-prev"><i class="icon-left"></i></div>
                <div class="swiper-button-next sifency-slider-nav-arrow sifency-slider-nav-next"><i class="icon-right"></i></div>
                </div>
            <?php endif; ?>
            <?php if ( ! empty($settings['show_pagination']) ) : ?>
                <div class="swiper-pagination"></div>
            <?php endif; ?>
        </div>
        <?php
    }
}