<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Image_Size;
use Elementor\Utils;
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_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;
class Sifency_Widget_Imageslider extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Slider_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	use \Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;
	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-image-slider',
			'title'          => 'Sifency Image Slider',
			'icon'           => 'eicon-slider-device',
			'category'       => array( 'sifency-advance-widgets' ),
			'keywords'       => array( 'slider', 'image', 'carousel', 'gallery' ),
			'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(
			'image',
			array(
				'label'   => esc_html__( 'Image', 'sifency-addons' ),
				'type'    => Controls_Manager::MEDIA,
				'default' => array(
					'url' => Utils::get_placeholder_image_src(),
				),
			)
		);
		$repeater->add_control(
			'link',
			array(
				'label'       => esc_html__( 'Link', 'sifency-addons' ),
				'type'        => Controls_Manager::URL,
				'placeholder' => esc_html__( 'https://your-link.com', 'sifency-addons' ),
			)
		);
		$this->add_control(
			'slides',
			array(
				'label'   => 'Slides',
				'type'    => Controls_Manager::REPEATER,
				'fields'  => $repeater->get_controls(),
				'default' => array(
					array( 'image' => Utils::get_placeholder_image_src() ),
					array( 'image' => Utils::get_placeholder_image_src() ),
					array( 'image' => Utils::get_placeholder_image_src() ),
				),
			)
		);
		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			array(
				'name'      => 'thumbnail',
				'default'   => 'large',
				'separator' => 'none',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_slider_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'            => '.sifency-slide-image',
				'base_hover_selector' => '.sifency-slide-image:hover',
				'selectors'           => array(
					'image'      => '.sifency-slide-image img',
					'container'  => '.sifency-slide-image img',
					'dimensions' => '.sifency-slide-image img',
				),
				'show_overlay'        => false,
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		$this->add_Sifency_Addons_Custom_CSS_Controls();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$this->sifency_addons_render_custom_css();
		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-image-slider-wrapper', 'sifency-carousel-wrapper' ),
				'data-swiper-options' => wp_json_encode( $slider_options ),
			)
		);

		$animation_attrs = $animation_manager->get_wrapper_attributes();

		?>
		<?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 $index => $slide ) :
					$slide_key  = 'slide_' . $index;
					$item_attrs = $animation_manager->get_item_attributes( array( 'swiper-slide', 'sifency-slide-item' ) );

					$image_html = Group_Control_Image_Size::get_attachment_image_html( $slide, 'thumbnail', 'image' );
					?>
					<div <?php echo wp_kses_post( $item_attrs ); ?>>
						
						<?php
						if ( ! empty( $slide['link']['url'] ) ) :
							$this->add_link_attributes( $slide_key, $slide['link'] );
							?>
							<a <?php echo wp_kses_post( $this->get_render_attribute_string( $slide_key ) ); ?> class="sifency-slide-image">
								<?php echo wp_kses_post( $image_html ); ?>
							</a>
						<?php else : ?>
							<div class="sifency-slide-image">
								<?php echo wp_kses_post( $image_html ); ?>
							</div>
						<?php endif; ?>

					</div>
				<?php endforeach; ?>
			</div>
		
			<?php $this->render_sifency_addons_slider_nav( $settings ); ?>
			<?php $this->render_sifency_addons_slider_pagination( $settings ); ?>

		</div>
		<?php
	}
}
