<?php

namespace ElementPack\Modules\FancySlider\Widgets;

use ElementPack\Base\Module_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Image_Size;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Text_Stroke;
use Elementor\Repeater;
use ElementPack\Utils;

use ElementPack\Traits\Global_Swiper_Controls;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Fancy_Slider extends Module_Base {

	use Global_Swiper_Controls;

	public function get_name() {
		return 'bdt-fancy-slider';
	}

	public function get_title() {
		return BDTEP . esc_html__( 'Fancy Slider', 'bdthemes-element-pack' );
	}

	public function get_icon() {
		return 'bdt-wi-fancy-slider';
	}

	public function get_categories() {
		return [ 'element-pack' ];
	}

	public function get_keywords() {
		return [ 'card', 'slider', 'fancy', 'slideshow', 'advanced' ];
	}

	public function get_style_depends() {
		return $this->ep_is_edit_mode()
			? [ 'swiper', 'ep-styles' ]
			: [ 'swiper', 'ep-font', 'ep-fancy-slider' ];
	}

	public function get_script_depends() {
		return $this->ep_is_edit_mode()
			? [ 'swiper', 'imagesloaded', 'ep-scripts' ]
			: [ 'swiper', 'imagesloaded', 'ep-fancy-slider' ];
	}

	public function get_custom_help_url() {
		return 'https://youtu.be/UGBnjbp90eA';
	}

	public function has_widget_inner_wrapper(): bool {
        return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
    }
	protected function is_dynamic_content(): bool {
		return false;
	}

	protected function register_controls() {
		$this->register_query_section_controls();
	}

	private function register_query_section_controls() {

		$this->start_controls_section(
			'section_content_sliders',
			[ 
				'label' => esc_html__( 'Items', 'bdthemes-element-pack' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'sub_title',
			[ 
				'label'       => esc_html__( 'Sub Title', 'bdthemes-element-pack' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Subtitle Goes Here', 'bdthemes-element-pack' ),
				'label_block' => true,
				'dynamic'     => [ 'active' => true ],
			]
		);

		$repeater->add_control(
			'title',
			[ 
				'label'       => esc_html__( 'Title', 'bdthemes-element-pack' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Slide Title Here', 'bdthemes-element-pack' ),
				'label_block' => true,
				'dynamic'     => [ 'active' => true ],
			]
		);

		$repeater->add_control(
			'title_link',
			[ 
				'label'         => esc_html__( 'Title Link', 'bdthemes-element-pack' ),
				'type'          => Controls_Manager::URL,
				'default'       => [ 'url' => '' ],
				'show_external' => false,
				'dynamic'       => [ 'active' => true ],
				'condition'     => [ 
					'title!' => ''
				]
			]
		);

		$repeater->add_control(
			'slide_button',
			[ 
				'label'       => esc_html__( 'Button Text', 'bdthemes-element-pack' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Read More', 'bdthemes-element-pack' ),
				'label_block' => true,
				'dynamic'     => [ 'active' => true ],
			]
		);

		$repeater->add_control(
			'button_link',
			[ 
				'label'         => esc_html__( 'Button Link', 'bdthemes-element-pack' ),
				'type'          => Controls_Manager::URL,
				'default'       => [ 'url' => '#' ],
				'show_external' => false,
				'dynamic'       => [ 'active' => true ],
				'condition'     => [ 
					'slide_button!' => ''
				]
			]
		);

		$repeater->add_control(
			'slide_image',
			[ 
				'label'   => esc_html__( 'Image', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::MEDIA,
				'dynamic' => [ 'active' => true ],
				'default' => [ 
					'url' => BDTEP_ASSETS_URL . 'images/gallery/item-' . rand( 1, 3 ) . '.svg',
				],
			]
		);

		$repeater->add_control(
			'description',
			[ 
				'label'       => esc_html__( 'Text', 'bdthemes-element-pack' ),
				'type'        => Controls_Manager::WYSIWYG,
				'default'     => esc_html__( 'Lorem ipsum dolor sit amet consectetur, adipisicing elit. Recusandae voluptate repellendus magni illo ea animi?', 'bdthemes-element-pack' ),
				'label_block' => true,
				'dynamic'     => [ 'active' => true ],
			]
		);

		$this->add_control(
			'slides',
			[ 
				'type'        => Controls_Manager::REPEATER,
				'fields'      => $repeater->get_controls(),
				'default'     => [ 
					[ 
						'title'       => esc_html__( 'Fancy Slider Item One', 'bdthemes-element-pack' ),
						'slide_image' => [ 'url' => BDTEP_ASSETS_URL . 'images/gallery/item-4.svg' ],
					],
					[ 
						'title'       => esc_html__( 'Fancy Slider Item Two', 'bdthemes-element-pack' ),
						'slide_image' => [ 'url' => BDTEP_ASSETS_URL . 'images/gallery/item-6.svg' ]
					],
					[ 
						'title'       => esc_html__( 'Fancy Slider Item Three', 'bdthemes-element-pack' ),
						'slide_image' => [ 'url' => BDTEP_ASSETS_URL . 'images/gallery/item-7.svg' ]
					],
				],
				'title_field' => '{{{ title }}}',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_content_fancy_slider',
			[ 
				'label' => esc_html__( 'Additional Settings', 'bdthemes-element-pack' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_group_control(
			Group_Control_Image_Size::get_type(),
			[ 
				'name'    => 'thumbnail_size',
				'label'   => esc_html__( 'Image Size', 'bdthemes-element-pack' ),
				'exclude' => [ 'custom' ],
				'default' => 'full',
			]
		);

		$this->add_responsive_control(
			'slide_text_align',
			[ 
				'label'     => __( 'Alignment', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => [ 
					'left'    => [ 
						'title' => __( 'Left', 'bdthemes-element-pack' ),
						'icon'  => 'eicon-text-align-left',
					],
					'center'  => [ 
						'title' => __( 'Center', 'bdthemes-element-pack' ),
						'icon'  => 'eicon-text-align-center',
					],
					'right'   => [ 
						'title' => __( 'Right', 'bdthemes-element-pack' ),
						'icon'  => 'eicon-text-align-right',
					],
					'justify' => [ 
						'title' => __( 'Justified', 'bdthemes-element-pack' ),
						'icon'  => 'eicon-text-align-justify',
					],
				],
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-content' => 'text-align: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'show_subtitle',
			[ 
				'label'   => esc_html__( 'Show Sub Title', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'show_title',
			[ 
				'label'   => esc_html__( 'Show Title', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'show_description',
			[ 
				'label'   => esc_html__( 'Show Description', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'show_button',
			[ 
				'label'   => esc_html__( 'Show Button', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'show_slide_image',
			[ 
				'label'   => esc_html__( 'Show Slide Image', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'title_tags',
			[ 
				'label'     => __( 'Title HTML Tag', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'h2',
				'options'   => element_pack_title_tags(),
				'condition' => [ 
					'show_title' => 'yes'
				]
			]
		);

		$this->end_controls_section();

		//Navigation Controls
		$this->start_controls_section(
			'section_content_navigation',
			[ 
				'label' => __( 'Navigation', 'bdthemes-element-pack' ),
			]
		);

		//Global Navigation Controls
		$this->register_navigation_controls();

		$this->end_controls_section();

		$this->start_controls_section(
			'section_carousel_settings',
			[ 
				'label' => __( 'Slider Settings', 'bdthemes-element-pack' ),
			]
		);

		$this->add_control(
			'autoplay',
			[ 
				'label'   => __( 'Autoplay', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',

			]
		);

		$this->add_control(
			'autoplay_speed',
			[ 
				'label'     => esc_html__( 'Autoplay Speed', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::NUMBER,
				'default'   => 5000,
				'condition' => [ 
					'autoplay' => 'yes',
				],
			]
		);

		$this->add_control(
			'pauseonhover',
			[ 
				'label' => esc_html__( 'Pause on Hover', 'bdthemes-element-pack' ),
				'type'  => Controls_Manager::SWITCHER,
			]
		);

		$this->add_control(
			'loop',
			[ 
				'label'   => __( 'Loop', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',

			]
		);


		$this->add_control(
			'speed',
			[ 
				'label'   => __( 'Animation Speed (ms)', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::SLIDER,
				'default' => [ 
					'size' => 500,
				],
				'range'   => [ 
					'px' => [ 
						'min'  => 100,
						'max'  => 5000,
						'step' => 50,
					],
				],
			]
		);

		$this->add_control(
			'observer',
			[ 
				'label'       => __( 'Observer', 'bdthemes-element-pack' ),
				'description' => __( 'When you use carousel in any hidden place (in tabs, accordion etc) keep it yes.', 'bdthemes-element-pack' ),
				'type'        => Controls_Manager::SWITCHER,
			]
		);

		$this->end_controls_section();


		//Style
		$this->start_controls_section(
			'section_style_slider',
			[ 
				'label' => esc_html__( 'Slider Item', 'bdthemes-element-pack' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'item_background',
			[ 
				'label'     => esc_html__( 'Background', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-item' => 'background-color: {{VALUE}};'
				]
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[ 
				'name'     => 'item_border',
				'label'    => esc_html__( 'Border', 'bdthemes-element-pack' ),
				'selector' => '{{WRAPPER}} .bdt-ep-fancy-slider-item',
			]
		);

		$this->add_responsive_control(
			'item_border_radius',
			[ 
				'label'      => esc_html__( 'Border Radius', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_responsive_control(
			'item_content_padding',
			[ 
				'label'      => esc_html__( 'Padding', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors'  => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-content' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_iamge',
			[ 
				'label'     => esc_html__( 'Image', 'bdthemes-element-pack' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 
					'show_slide_image' => [ 'yes' ],
				],
			]
		);

		$this->add_control(
			'show_background_overlay',
			[ 
				'label' => esc_html__( 'Overlay Color', 'bdthemes-element-pack' ),
				'type'  => Controls_Manager::SWITCHER,
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[ 
				'name'      => 'image_background',
				'types'     => [ 'gradient' ],
				'selector'  => '{{WRAPPER}} .bdt-ep-fancy-slider-img:after',
				'condition' => [ 
					'show_background_overlay' => 'yes',
				],
			]
		);

		$this->add_control(
			'iamge_bg_color',
			[ 
				'label'     => esc_html__( 'Background', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-img' => 'background: {{VALUE}};',
				],
				'separator' => 'before'
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[ 
				'name'      => 'iamge_border',
				'label'     => esc_html__( 'Border', 'bdthemes-element-pack' ),
				'separator' => 'before',
				'selector'  => '{{WRAPPER}} .bdt-ep-fancy-slider-img, {{WRAPPER}} .bdt-ep-fancy-slider-img img',
			]
		);

		$this->add_responsive_control(
			'iamge_border_radius',
			[ 
				'label'      => esc_html__( 'Border Radius', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-img, {{WRAPPER}} .bdt-ep-fancy-slider-img img, {{WRAPPER}} .bdt-ep-fancy-slider-img:after' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[ 
				'name'     => 'iamge_box_shadow',
				'selector' => '{{WRAPPER}} .bdt-ep-fancy-slider-img, {{WRAPPER}} .bdt-ep-fancy-slider-img img',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_title',
			[ 
				'label'     => esc_html__( 'Title', 'bdthemes-element-pack' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 
					'show_title' => [ 'yes' ],
				],
			]
		);

		$this->add_control(
			'title_color',
			[ 
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-title, {{WRAPPER}} .bdt-ep-fancy-slider-title a' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_responsive_control(
			'title_spacing',
			[ 
				'label'     => esc_html__( 'Spacing', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-title' => 'padding-bottom: {{SIZE}}{{UNIT}}',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[ 
				'name'     => 'title_typography',
				'label'    => esc_html__( 'Typography', 'bdthemes-element-pack' ),
				'selector' => '{{WRAPPER}} .bdt-ep-fancy-slider-title',
			]
		);

		$this->add_group_control(
			Group_Control_Text_Stroke::get_type(),
			[ 
				'name'     => 'title_text_stroke',
				'label'    => __( 'Text Stroke', 'bdthemes-element-pack' ) . BDTEP_NC,
				'selector' => '{{WRAPPER}} .bdt-ep-fancy-slider-title',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_sub_title',
			[ 
				'label'     => esc_html__( 'Sub Title', 'bdthemes-element-pack' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 
					'show_subtitle' => [ 'yes' ],
				],
			]
		);

		$this->add_control(
			'sub_title_color',
			[ 
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-subtitle' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_responsive_control(
			'sub_title_spacing',
			[ 
				'label'     => esc_html__( 'Spacing', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-subtitle' => 'margin-bottom: {{SIZE}}{{UNIT}}',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[ 
				'name'     => 'sub_title_typography',
				'label'    => esc_html__( 'Typography', 'bdthemes-element-pack' ),
				'selector' => '{{WRAPPER}} .bdt-ep-fancy-slider-subtitle',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_description',
			[ 
				'label'     => esc_html__( 'Text', 'bdthemes-element-pack' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 
					'show_description' => [ 'yes' ],
				],
			]
		);

		$this->add_control(
			'description_color',
			[ 
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-text' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_responsive_control(
			'description_spacing',
			[ 
				'label'     => esc_html__( 'Spacing', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::SLIDER,
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-text' => 'padding-bottom: {{SIZE}}{{UNIT}}',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[ 
				'name'     => 'description_typography',
				'label'    => esc_html__( 'Typography', 'bdthemes-element-pack' ),
				'selector' => '{{WRAPPER}} .bdt-ep-fancy-slider-text',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_button',
			[ 
				'label'     => esc_html__( 'Button', 'bdthemes-element-pack' ),
				'tab'       => Controls_Manager::TAB_STYLE,
				'condition' => [ 
					'show_button' => 'yes',
				],
			]
		);

		$this->start_controls_tabs( 'tabs_button_style' );

		$this->start_controls_tab(
			'tab_button_normal',
			[ 
				'label' => esc_html__( 'Normal', 'bdthemes-element-pack' ),
			]
		);

		$this->add_control(
			'button_text_color',
			[ 
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-button a' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[ 
				'name'     => 'button_background',
				'types'    => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .bdt-ep-fancy-slider-button a',
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[ 
				'name'     => 'button_box_shadow',
				'selector' => '{{WRAPPER}} .bdt-ep-fancy-slider-button a',
			]
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			[ 
				'name'      => 'button_border',
				'label'     => esc_html__( 'Border', 'bdthemes-element-pack' ),
				'selector'  => '{{WRAPPER}} .bdt-ep-fancy-slider-button a',
				'separator' => 'before',
			]
		);

		$this->add_responsive_control(
			'button_border_radius',
			[ 
				'label'      => esc_html__( 'Border Radius', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-button a' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'condition'  => [ 
					'border_radius_advanced_show!' => 'yes',
				],
			]
		);

		$this->add_control(
			'border_radius_advanced_show',
			[ 
				'label' => __( 'Advanced Radius', 'bdthemes-element-pack' ),
				'type'  => Controls_Manager::SWITCHER,
			]
		);

		$this->add_responsive_control(
			'border_radius_advanced',
			[ 
				'label'       => esc_html__( 'Radius', 'bdthemes-element-pack' ),
				'description' => sprintf( __( 'For example: <b>%1s</b> or Go <a href="%2s" target="_blank">this link</a> and copy and paste the radius value.', 'bdthemes-element-pack' ), '30% 70% 82% 18% / 46% 62% 38% 54%', 'https://9elements.github.io/fancy-border-radius/' ),
				'type'        => Controls_Manager::TEXT,
				'size_units'  => [ 'px', '%' ],
				'separator'   => 'after',
				'default'     => '30% 70% 82% 18% / 46% 62% 38% 54%',
				'selectors'   => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-button a' => 'border-radius: {{VALUE}}; overflow: hidden;',
				],
				'condition'   => [ 
					'border_radius_advanced_show' => 'yes',
				],
			]
		);

		$this->add_responsive_control(
			'button_padding',
			[ 
				'label'      => esc_html__( 'Padding', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors'  => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-button a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
				'separator'  => 'before',
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[ 
				'name'     => 'button_typography',
				'label'    => esc_html__( 'Typography', 'bdthemes-element-pack' ),
				'selector' => '{{WRAPPER}} .bdt-ep-fancy-slider-button a',
			]
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'tab_button_hover',
			[ 
				'label' => esc_html__( 'Hover', 'bdthemes-element-pack' ),
			]
		);

		$this->add_control(
			'button_hover_color',
			[ 
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-button a:hover' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			[ 
				'name'     => 'button_hover_background',
				'types'    => [ 'gradient' ],
				'selector' => '{{WRAPPER}} .bdt-ep-fancy-slider-button a:hover',
			]
		);

		$this->add_control(
			'button_hover_border_color',
			[ 
				'label'     => esc_html__( 'Border Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'condition' => [ 
					'button_border_border!' => '',
				],
				'selectors' => [ 
					'{{WRAPPER}} .bdt-ep-fancy-slider-button a:hover' => 'border-color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[ 
				'name'     => 'button_hover_box_shadow',
				'selector' => '{{WRAPPER}} .bdt-ep-fancy-slider-button a:hover',
			]
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();

		//Navigation Style
		$this->start_controls_section(
			'section_style_navigation',
			[ 
				'label'      => __( 'Navigation', 'bdthemes-element-pack' ),
				'tab'        => Controls_Manager::TAB_STYLE,
				'conditions' => [ 
					'relation' => 'or',
					'terms'    => [ 
						[ 
							'name'     => 'navigation',
							'operator' => '!=',
							'value'    => 'none',
						],
						[ 
							'name'  => 'show_scrollbar',
							'value' => 'yes',
						],
					],
				],
			]
		);

		//Global Navigation Style Controls
		$this->register_navigation_style_controls( 'swiper-carousel' );

		$this->end_controls_section();
	}

	public function render_header() {
		$settings  = $this->get_settings_for_display();
		$id        = 'bdt-ep-fancy-slider-' . $this->get_id();
		$navigation = isset( $settings['navigation'] ) ? $settings['navigation'] : 'arrows';

		$this->add_render_attribute( 'fancy-slider', 'id', $id );
		$this->add_render_attribute( 'fancy-slider', 'class', 'bdt-ep-fancy-slider' );

		$nav_class_map = [
			'arrows'         => 'bdt-arrows-align-' . ( isset( $settings['arrows_position'] ) ? $settings['arrows_position'] : '' ),
			'dots'           => 'bdt-dots-align-' . ( isset( $settings['dots_position'] ) ? $settings['dots_position'] : '' ),
			'both'           => 'bdt-arrows-dots-align-' . ( isset( $settings['both_position'] ) ? $settings['both_position'] : '' ),
			'arrows-fraction' => 'bdt-arrows-dots-align-' . ( isset( $settings['arrows_fraction_position'] ) ? $settings['arrows_fraction_position'] : '' ),
		];
		if ( isset( $nav_class_map[ $navigation ] ) ) {
			$this->add_render_attribute( 'fancy-slider', 'class', $nav_class_map[ $navigation ] );
		}

		$pagination_map = [
			'arrows-fraction' => 'fraction',
			'progressbar'     => 'progressbar',
		];
		$pagination_type = isset( $pagination_map[ $navigation ] )
			? $pagination_map[ $navigation ]
			: ( ( $navigation === 'both' || $navigation === 'dots' ) ? 'bullets' : '' );

		$speed_size = isset( $settings['speed']['size'] ) ? $settings['speed']['size'] : 500;
		$autoplay_enabled = ! empty( $settings['autoplay'] ) && $settings['autoplay'] === 'yes';
		$observer_enabled = ! empty( $settings['observer'] ) && $settings['observer'] === 'yes';

		$this->add_render_attribute(
			[
				'fancy-slider' => [
					'data-settings' => [
						wp_json_encode( array_filter( [
							'autoplay'       => $autoplay_enabled ? [ 'delay' => isset( $settings['autoplay_speed'] ) ? (int) $settings['autoplay_speed'] : 5000 ] : false,
							'loop'           => ( ! empty( $settings['loop'] ) && $settings['loop'] === 'yes' ),
							'speed'          => (int) $speed_size,
							'effect'         => 'fade',
							'fadeEffect'     => [ 'crossFade' => true ],
							'lazy'           => true,
							'pauseOnHover'   => ( ! empty( $settings['pauseonhover'] ) && $settings['pauseonhover'] === 'yes' ),
							'slidesPerView'  => 1,
							'observer'       => $observer_enabled,
							'observeParents' => $observer_enabled,
							'navigation'     => [
								'nextEl' => '#' . $id . ' .bdt-navigation-next',
								'prevEl' => '#' . $id . ' .bdt-navigation-prev',
							],
							'pagination'     => [
								'el'             => '#' . $id . ' .swiper-pagination',
								'type'           => $pagination_type,
								'clickable'      => 'true',
								'dynamicBullets' => ( ! empty( $settings['dynamic_bullets'] ) && $settings['dynamic_bullets'] === 'yes' ),
							],
							'scrollbar'      => [
								'el'   => '#' . $id . ' .swiper-scrollbar',
								'hide' => 'true',
							],
						] ) )
					]
				]
			]
		);

		$this->add_render_attribute( 'swiper', 'class', 'swiper-carousel swiper' );

		?>
		<div <?php $this->print_render_attribute_string( 'fancy-slider' ); ?>>
			<div <?php $this->print_render_attribute_string( 'swiper' ); ?>>
				<div class="swiper-wrapper">
					<?php
	}

	/**
	 * Renders the slide image (attachment or fallback img tag).
	 *
	 * @param array $content Repeater item with 'slide_image' and 'title'.
	 */
	protected function render_item_image( $content ) {
		if ( empty( $content['slide_image']['url'] ) ) {
			return;
		}
		$settings = $this->get_settings_for_display();
		$img_id   = isset( $content['slide_image']['id'] ) ? $content['slide_image']['id'] : 0;
		$alt      = isset( $content['title'] ) ? $content['title'] : '';

		$thumb_url = $img_id ? Group_Control_Image_Size::get_attachment_image_src( $img_id, 'thumbnail_size', $settings ) : false;
		$size_name = isset( $settings['thumbnail_size_size'] ) ? $settings['thumbnail_size_size'] : 'full';

		if ( $thumb_url && $img_id ) {
			echo wp_get_attachment_image(
				$img_id,
				$size_name,
				false,
				[ 'alt' => esc_attr( $alt ) ]
			);
		} else {
			printf( '<img src="%1$s" alt="%2$s">', esc_url( $content['slide_image']['url'] ), esc_attr( $alt ) );
		}
	}

	/**
	 * Renders a single slide's content (image + text + button).
	 *
	 * @param array  $content    Repeater item.
	 * @param string $title_key  Render attribute key for title link.
	 * @param string $button_key Render attribute key for button link.
	 */
	protected function render_item_content( $content, $title_key, $button_key ) {
		$settings   = $this->get_settings_for_display();
		$show_image = ! empty( $settings['show_slide_image'] ) && $settings['show_slide_image'] === 'yes';
		$show_sub   = ! empty( $settings['show_subtitle'] ) && $settings['show_subtitle'] === 'yes';
		$show_title = ! empty( $settings['show_title'] ) && $settings['show_title'] === 'yes';
		$show_desc  = ! empty( $settings['show_description'] ) && $settings['show_description'] === 'yes';
		$show_btn   = ! empty( $settings['show_button'] ) && $settings['show_button'] === 'yes';
		$title_tag  = Utils::get_valid_html_tag( isset( $settings['title_tags'] ) ? $settings['title_tags'] : 'h2' );

		$title_link_url  = isset( $content['title_link']['url'] ) ? $content['title_link']['url'] : '';
		$button_link_url = isset( $content['button_link']['url'] ) ? $content['button_link']['url'] : '';

		if ( $title_link_url !== '' ) {
			$this->add_link_attributes( $title_key, $content['title_link'] );
		}
		if ( $button_link_url !== '' ) {
			$this->add_link_attributes( $button_key, $content['button_link'] );
		}

		$this->add_render_attribute( 'fancy_title_tags', 'class', 'bdt-ep-fancy-slider-title', true );
		?>
					<div class="bdt-ep-fancy-slider-image">
						<?php if ( ! empty( $content['slide_image']['url'] ) && $show_image ) : ?>
							<div class="bdt-ep-fancy-slider-img swiper-lazy">
								<?php $this->render_item_image( $content ); ?>
								<div class="swiper-lazy-preloader swiper-lazy-preloader-white"></div>
							</div>
						<?php endif; ?>
					</div>

					<div class="bdt-ep-fancy-slider-content">
						<?php if ( ! empty( $content['sub_title'] ) && $show_sub ) : ?>
							<div class="bdt-ep-fancy-slider-subtitle">
								<?php echo wp_kses_post( $content['sub_title'] ); ?>
							</div>
						<?php endif; ?>

						<?php if ( ! empty( $content['title'] ) && $show_title ) : ?>
							<<?php echo esc_attr( $title_tag ); ?> <?php $this->print_render_attribute_string( 'fancy_title_tags' ); ?>>
								<?php if ( $title_link_url !== '' ) : ?>
									<a <?php $this->print_render_attribute_string( $title_key ); ?>>
								<?php endif; ?>
								<?php echo wp_kses_post( $content['title'] ); ?>
								<?php if ( $title_link_url !== '' ) : ?>
									</a>
								<?php endif; ?>
							</<?php echo esc_attr( $title_tag ); ?>>
						<?php endif; ?>

						<?php if ( ! empty( $content['description'] ) && $show_desc ) : ?>
							<div class="bdt-ep-fancy-slider-text">
								<?php echo wp_kses_post( $content['description'] ); ?>
							</div>
						<?php endif; ?>

						<?php if ( ! empty( $content['slide_button'] ) && $show_btn ) : ?>
							<div class="bdt-ep-fancy-slider-button">
								<?php if ( $button_link_url !== '' ) : ?>
									<a <?php $this->print_render_attribute_string( $button_key ); ?>>
								<?php endif; ?>
								<?php echo wp_kses_post( $content['slide_button'] ); ?>
								<?php if ( $button_link_url !== '' ) : ?>
									</a>
								<?php endif; ?>
							</div>
						<?php endif; ?>
					</div>
					<?php
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$slides   = isset( $settings['slides'] ) ? $settings['slides'] : [];

		$this->render_header();

		foreach ( $slides as $index => $slide ) :
			$slide_id = isset( $slide['_id'] ) ? $slide['_id'] : $index;
			?>
						<div class="swiper-slide bdt-ep-fancy-slider-item elementor-repeater-item-<?php echo esc_attr( $slide_id ); ?>">
							<?php $this->render_item_content( $slide, 'title_link' . $index, 'button_link' . $index ); ?>
						</div>
					<?php
		endforeach;

		$this->render_footer();
	}

	protected function content_template() {
		?>
		<#
		var showSubtitle = settings.show_subtitle === 'yes';
		var showTitle    = settings.show_title === 'yes';
		var showDesc     = settings.show_description === 'yes';
		var showBtn      = settings.show_button === 'yes';
		var showImg      = settings.show_slide_image === 'yes';
		var titleTag     = settings.title_tags || 'h2';

		var sliderId = 'bdt-ep-fancy-slider-' + view.getID();
		var nav = settings.navigation || 'arrows';
		var sliderClass = 'bdt-ep-fancy-slider';
		if ( nav === 'arrows' ) {
			sliderClass += ' bdt-arrows-align-' + ( settings.arrows_position || '' );
		} else if ( nav === 'dots' ) {
			sliderClass += ' bdt-dots-align-' + ( settings.dots_position || '' );
		} else if ( nav === 'both' ) {
			sliderClass += ' bdt-arrows-dots-align-' + ( settings.both_position || '' );
		} else if ( nav === 'arrows-fraction' ) {
			sliderClass += ' bdt-arrows-dots-align-' + ( settings.arrows_fraction_position || '' );
		}

		var paginationType = '';
		if ( nav === 'arrows-fraction' ) {
			paginationType = 'fraction';
		} else if ( nav === 'both' || nav === 'dots' ) {
			paginationType = 'bullets';
		} else if ( nav === 'progressbar' ) {
			paginationType = 'progressbar';
		}

		var speedSize = ( settings.speed && settings.speed.size ) ? parseInt( settings.speed.size, 10 ) : 500;
		var autoplayEnabled = settings.autoplay === 'yes';
		var observerEnabled = settings.observer === 'yes';

		var swiperSettings = {
			loop: settings.loop === 'yes',
			speed: speedSize,
			effect: 'fade',
			fadeEffect: { crossFade: true },
			lazy: true,
			pauseOnHover: settings.pauseonhover === 'yes',
			slidesPerView: 1,
			observer: observerEnabled,
			observeParents: observerEnabled,
			navigation: {
				nextEl: '#' + sliderId + ' .bdt-navigation-next',
				prevEl: '#' + sliderId + ' .bdt-navigation-prev'
			},
			pagination: {
				el: '#' + sliderId + ' .swiper-pagination',
				type: paginationType,
				clickable: 'true',
				dynamicBullets: settings.dynamic_bullets === 'yes'
			},
			scrollbar: {
				el: '#' + sliderId + ' .swiper-scrollbar',
				hide: 'true'
			}
		};
		if ( autoplayEnabled ) {
			swiperSettings.autoplay = {
				delay: settings.autoplay_speed ? parseInt( settings.autoplay_speed, 10 ) : 5000
			};
		}

		var dataSettings = JSON.stringify( swiperSettings );
		var navIcon = settings.nav_arrows_icon || '5';
		var hideArrowMobile = settings.hide_arrow_on_mobile ? ' bdt-visible@m' : '';
		var slides = settings.slides || [];
		#>
		<div id="{{ sliderId }}" class="{{ sliderClass }}" data-settings="{{ dataSettings }}">
			<div class="swiper-carousel swiper" role="region" aria-roledescription="carousel" aria-label="<?php echo esc_attr( $this->get_title() ); ?>" dir="<?php echo is_rtl() ? 'rtl' : 'ltr'; ?>">
				<div class="swiper-wrapper">
					<# _.each( slides, function( slide, index ) {
						var repeaterId = slide._id !== undefined && slide._id !== null ? slide._id : index;
					#>
					<div class="swiper-slide bdt-ep-fancy-slider-item elementor-repeater-item-{{ repeaterId }}">
						<div class="bdt-ep-fancy-slider-image">
							<# if ( slide.slide_image && slide.slide_image.url && showImg ) { #>
							<div class="bdt-ep-fancy-slider-img swiper-lazy">
								<img src="{{ slide.slide_image.url }}" alt="{{ slide.title }}">
								<div class="swiper-lazy-preloader swiper-lazy-preloader-white"></div>
							</div>
							<# } #>
						</div>
						<div class="bdt-ep-fancy-slider-content">
							<# if ( slide.sub_title && showSubtitle ) { #>
							<div class="bdt-ep-fancy-slider-subtitle">{{{ slide.sub_title }}}</div>
							<# } #>
							<# if ( slide.title && showTitle ) { #>
							<{{ titleTag }} class="bdt-ep-fancy-slider-title">
								<# if ( slide.title_link && slide.title_link.url ) { #>
									<a href="{{ slide.title_link.url }}"<# if ( slide.title_link && slide.title_link.is_external ) { #> target="_blank"<# } #><# if ( slide.title_link && slide.title_link.nofollow ) { #> rel="nofollow"<# } #>>{{{ slide.title }}}</a>
								<# } else { #>
									{{{ slide.title }}}
								<# } #>
							</{{ titleTag }}>
							<# } #>
							<# if ( slide.description && showDesc ) { #>
							<div class="bdt-ep-fancy-slider-text">{{{ slide.description }}}</div>
							<# } #>
							<# if ( slide.slide_button && showBtn ) { #>
							<div class="bdt-ep-fancy-slider-button">
								<# if ( slide.button_link && slide.button_link.url ) { #>
									<a href="{{ slide.button_link.url }}"<# if ( slide.button_link && slide.button_link.is_external ) { #> target="_blank"<# } #><# if ( slide.button_link && slide.button_link.nofollow ) { #> rel="nofollow"<# } #>>{{{ slide.slide_button }}}</a>
								<# } else { #>
									{{{ slide.slide_button }}}
								<# } #>
							</div>
							<# } #>
						</div>
					</div>
					<# } ); #>
				</div>
				<# if ( settings.show_scrollbar === 'yes' ) { #>
				<div class="swiper-scrollbar"></div>
				<# } #>
			</div>

			<# if ( nav === 'both' ) { #>
			<div class="bdt-position-z-index bdt-position-{{ settings.both_position }}">
				<div class="bdt-arrows-dots-container bdt-slidenav-container">
					<div class="bdt-flex bdt-flex-middle">
						<div class="{{ hideArrowMobile }}">
							<div class="bdt-navigation-prev bdt-slidenav-previous bdt-icon bdt-slidenav">
								<i class="ep-icon-arrow-left-{{ navIcon }}" aria-hidden="true"></i>
							</div>
						</div>
						<# if ( settings.both_position !== 'center' ) { #>
						<div class="swiper-pagination"></div>
						<# } #>
						<div class="{{ hideArrowMobile }}">
							<div class="bdt-navigation-next bdt-slidenav-next bdt-icon bdt-slidenav">
								<i class="ep-icon-arrow-right-{{ navIcon }}" aria-hidden="true"></i>
							</div>
						</div>
					</div>
				</div>
			</div>
			<# if ( settings.both_position === 'center' ) { #>
			<div class="bdt-position-z-index bdt-position-bottom">
				<div class="bdt-dots-container">
					<div class="swiper-pagination"></div>
				</div>
			</div>
			<# } #>
			<# } else if ( nav === 'arrows-fraction' ) { #>
			<div class="bdt-position-z-index bdt-position-{{ settings.arrows_fraction_position }}">
				<div class="bdt-arrows-fraction-container bdt-slidenav-container">
					<div class="bdt-flex bdt-flex-middle">
						<div class="{{ hideArrowMobile }}">
							<div class="bdt-navigation-prev bdt-slidenav-previous bdt-icon bdt-slidenav">
								<i class="ep-icon-arrow-left-{{ navIcon }}" aria-hidden="true"></i>
							</div>
						</div>
						<# if ( settings.arrows_fraction_position !== 'center' ) { #>
						<div class="swiper-pagination"></div>
						<# } #>
						<div class="{{ hideArrowMobile }}">
							<div class="bdt-navigation-next bdt-slidenav-next bdt-icon bdt-slidenav">
								<i class="ep-icon-arrow-right-{{ navIcon }}" aria-hidden="true"></i>
							</div>
						</div>
					</div>
				</div>
			</div>
			<# if ( settings.arrows_fraction_position === 'center' ) { #>
			<div class="bdt-dots-container">
				<div class="swiper-pagination"></div>
			</div>
			<# } #>
			<# } else { #>
			<# if ( nav === 'dots' ) { #>
			<div class="bdt-position-z-index bdt-position-{{ settings.dots_position }}">
				<div class="bdt-dots-container">
					<div class="swiper-pagination"></div>
				</div>
			</div>
			<# } else if ( nav === 'progressbar' ) { #>
			<div class="swiper-pagination bdt-position-z-index bdt-position-{{ settings.progress_position }}"></div>
			<# } #>
			<# if ( nav === 'arrows' ) { #>
			<div class="bdt-position-z-index bdt-position-{{ settings.arrows_position }}{{ hideArrowMobile }}">
				<div class="bdt-arrows-container bdt-slidenav-container">
					<div class="bdt-navigation-prev bdt-slidenav-previous bdt-icon bdt-slidenav">
						<i class="ep-icon-arrow-left-{{ navIcon }}" aria-hidden="true"></i>
					</div>
					<div class="bdt-navigation-next bdt-slidenav-next bdt-icon bdt-slidenav">
						<i class="ep-icon-arrow-right-{{ navIcon }}" aria-hidden="true"></i>
					</div>
				</div>
			</div>
			<# } #>
			<# } #>
		</div>
		<?php
	}
}
