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

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

class Sifency_3D_Carousel 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-3d-carousel',
			'title'          => 'Sifency 3D Carousel',
			'icon'           => 'eicon-slider-3d',
			'category'       => array( 'sifency-creative-widgets' ),
			'script_depends' => array( 'swiper', 'sifency-3d-carousel-script' ),
			'style_depends'  => array( 'sifency-master-style' ),
			'keywords'       => array( 'slider', 'carousel', '3d', 'coverflow' ),
		);
	}

	protected function register_controls() {
		// --- Content Section ---
		$this->start_controls_section(
			'section_slides',
			array( 'label' => esc_html__( 'Carousel Slides', 'sifency-addons' ) )
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'slide_type',
			array(
				'label'   => esc_html__( 'Content Type', 'sifency-addons' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'image',
				'options' => array(
					'image'    => 'Image Only',
					'template' => 'Elementor Template',
				),
			)
		);

		// Image Control
		$repeater->add_control(
			'image',
			array(
				'label'     => esc_html__( 'Choose Image', 'sifency-addons' ),
				'type'      => Controls_Manager::MEDIA,
				'condition' => array( 'slide_type' => 'image' ),
			)
		);

		// Template Control
		$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(),
				'condition'   => array( 'slide_type' => 'template' ),
			)
		);

		$this->add_control(
			'slides',
			array(
				'label'       => esc_html__( 'Slides', 'sifency-addons' ),
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'title_field' => 'Slide {{{ _id }}}',
			)
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'    => 'image',
				'default' => 'large',
			)
		);

		$this->end_controls_section();

		// --- 3D Effect Settings ---
		$this->start_controls_section(
			'section_3d_settings',
			array( 'label' => esc_html__( '3D Coverflow Settings', 'sifency-addons' ) )
		);

		$this->add_control(
			'rotate',
			array(
				'label'   => esc_html__( 'Rotation Angle', 'sifency-addons' ),
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 0,
						'max'  => 90,
						'step' => 1,
					),
				),
				'default' => array( 'size' => 30 ),
			)
		);

		$this->add_control(
			'depth',
			array(
				'label'   => esc_html__( '3D Depth (Z-Axis)', 'sifency-addons' ),
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => 0,
						'max'  => 500,
						'step' => 10,
					),
				),
				'default' => array( 'size' => 150 ),
			)
		);

		$this->add_control(
			'stretch',
			array(
				'label'   => esc_html__( 'Stretch (Space between)', 'sifency-addons' ),
				'type'    => Controls_Manager::SLIDER,
				'range'   => array(
					'px' => array(
						'min'  => -100,
						'max'  => 100,
						'step' => 1,
					),
				),
				'default' => array( 'size' => 0 ),
			)
		);

		$this->add_control(
			'autoplay',
			array(
				'label'   => esc_html__( 'Autoplay', 'sifency-addons' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			)
		);

		$this->end_controls_section();

		// --- Style Section ---
		$this->start_controls_section(
			'section_style',
			array(
				'label' => esc_html__( 'Slide Style', 'sifency-addons' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'slide_width',
			array(
				'label'      => esc_html__( 'Slide Width', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'vw' ),
				'range'      => array(
					'px' => array(
						'min' => 200,
						'max' => 800,
					),
				),
				'default'    => array(
					'size' => 350,
					'unit' => 'px',
				),
				'selectors'  => array( '{{WRAPPER}} .swiper-slide' => 'width: {{SIZE}}{{UNIT}};' ),
			)
		);

		$this->add_responsive_control(
			'slide_height',
			array(
				'label'      => esc_html__( 'Slide Height / Aspect Ratio', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', 'vh' ),
				'range'      => array(
					'px' => array(
						'min' => 200,
						'max' => 800,
					),
				),
				'default'    => array(
					'size' => 450,
					'unit' => 'px',
				),
				'selectors'  => array( '{{WRAPPER}} .swiper-slide' => '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-3d-inner' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ),
				'default'    => array(
					'top'    => '16',
					'bottom' => '16',
					'left'   => '16',
					'right'  => '16',
					'unit'   => 'px',
				),
			)
		);

		$this->end_controls_section();
	}

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

		if ( empty( $slides ) ) {
			return;
		}

		// Pass settings to JS
		$slider_config = array(
			'rotate'   => $settings['rotate']['size'] ?? 30,
			'depth'    => $settings['depth']['size'] ?? 150,
			'stretch'  => $settings['stretch']['size'] ?? 0,
			'autoplay' => $settings['autoplay'] === 'yes',
		);

		?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" class="sifency-3d-carousel-wrapper">
			<!-- Swiper Main Container -->
			<div class="swiper sifency-3d-swiper" data-config='<?php echo esc_attr( wp_json_encode( $slider_config ) ); ?>'>
				<div class="swiper-wrapper">
					
					<?php foreach ( $slides as $slide ) : ?>
						<div class="swiper-slide elementor-repeater-item-<?php echo esc_attr( $slide['_id'] ); ?>">
							<div class="sifency-3d-inner">
								
								<?php
								if ( $slide['slide_type'] === 'image' && ! empty( $slide['image']['url'] ) ) :
									$img_url = Group_Control_Image_Size::get_attachment_image_src( $slide['image']['id'], 'image', $settings );
									if ( ! $img_url ) {
										$img_url = $slide['image']['url'];
									}
									?>
									<img src="<?php echo esc_url( $img_url ); ?>" alt="3D Carousel Image">
								
									<?php
								elseif ( $slide['slide_type'] === 'template' && ! empty( $slide['template_id'] ) ) :
									// Magic: Render Elementor Template inside Slide
									echo wp_kses_post( \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $slide['template_id'] ) );
								endif;
								?>

							</div>
						</div>
					<?php endforeach; ?>

				</div>
			</div>
		</div>
		<?php
	}
}
