<?php
if ( ! class_exists( 'Draly_Dropshipping_Starter_Elementor_Slider' ) ) {
	class Draly_Dropshipping_Starter_Elementor_Slider extends Draly_Dropshipping_Starter_Elementor {
		public $name = 'slider';
		public $title = 'Slider';
		public $icon = 'eicon-slides';

		/**
		 * Register the widget controls.
		 *
		 * Adds different input fields to allow the user to change and customize the widget settings.
		 *
		 * @since 1.0.0
		 *
		 * @access protected
		 */
		protected function register_controls() {
			//General Section
			$this->start_controls_section(
				'general_section',
				[
					'label' => esc_html__( 'General', 'draly-dropshipping-starter' ),
				]
			);
			$this->add_control(
				'style',
				[
					'label'       => esc_html__( 'Style', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SELECT,
					'options'     => array(
						'style-01' => esc_html__( 'Style 01', 'draly-dropshipping-starter' ),
						'style-02' => esc_html__( 'Style 02', 'draly-dropshipping-starter' ),
						'style-03' => esc_html__( 'Style 03', 'draly-dropshipping-starter' ),
						'style-04' => esc_html__( 'Style 04', 'draly-dropshipping-starter' ),
						'style-05' => esc_html__( 'Style 05', 'draly-dropshipping-starter' ),
					),
					'default'     => 'style-01',
					'label_block' => true
				]
			);
			$this->add_control(
				'height', [
					'label'       => esc_html__( 'Height (px)', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SLIDER,
					'size_units'  => [ 'px' ],
					'range'       => [
						'px' => [
							'min' => 100,
							'max' => 2000,
						],
					],
					'label_block' => true
				]
			);
			$this->add_control(
				'height_mobile', [
					'label'       => esc_html__( 'Height Mobile (px)', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SLIDER,
					'size_units'  => [ 'px' ],
					'range'       => [
						'px' => [
							'min' => 100,
							'max' => 2000,
						],
					],
					'label_block' => true
				]
			);
			$this->add_control(
				'height_mobile_extra_small', [
					'label'       => esc_html__( 'Height Mobile (Extra Small) (px)', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SLIDER,
					'size_units'  => [ 'px' ],
					'range'       => [
						'px' => [
							'min' => 100,
							'max' => 2000,
						],
					],
					'label_block' => true
				]
			);
			$repeater = new \Elementor\Repeater();
			$repeater->add_control(
				'image', [
					'label'       => esc_html__( 'Image', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::MEDIA,
					'default'     => [
						'url' => \Elementor\Utils::get_placeholder_image_src(),
					],
					'label_block' => true
				]
			);
			$repeater->add_control(
				'image_mobile', [
					'label'       => esc_html__( 'Image Mobile', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::MEDIA,
					'label_block' => true
				]
			);
			$repeater->add_control(
				'image_mobile_extra_small', [
					'label'       => esc_html__( 'Image Mobile (Extra Small)', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::MEDIA,
					'label_block' => true
				]
			);
			$repeater->add_control(
				'icon', [
					'label'       => esc_html__( 'Icon', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::MEDIA,
					'label_block' => true
				]
			);
			$repeater->add_control(
				'subtitle', [
					'label'       => esc_html__( 'Subtitle', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::TEXTAREA,
					'placeholder' => esc_html__( 'Enter your subtitle', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$repeater->add_control(
				'title', [
					'label'       => esc_html__( 'Title', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::TEXTAREA,
					'placeholder' => esc_html__( 'Enter your title', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$repeater->add_control(
				'desc', [
					'label'       => esc_html__( 'Description', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::WYSIWYG,
					'placeholder' => esc_html__( 'Enter your description', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$repeater->add_control(
				'title_color',
				[
					'label'     => esc_html__( 'Title Color', 'draly-dropshipping-starter' ),
					'type'      => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} {{CURRENT_ITEM}} .title' => 'color: {{VALUE}}',
					],
				]
			);
			$repeater->add_control(
				'text_color',
				[
					'label'     => esc_html__( 'Text Color', 'draly-dropshipping-starter' ),
					'type'      => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} {{CURRENT_ITEM}} .desc' => 'color: {{VALUE}}',
					],
				]
			);
			$repeater->add_control(
				'link_text', [
					'label'       => esc_html__( 'Link Text', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::TEXT,
					'placeholder' => esc_html__( 'Enter your link text', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$repeater->add_control(
				'link_url', [
					'label'         => esc_html__( 'Link URL', 'draly-dropshipping-starter' ),
					'type'          => \Elementor\Controls_Manager::URL,
					'placeholder'   => esc_html__( 'https://your-link.com', 'draly-dropshipping-starter' ),
					'show_external' => true,
					'default'       => [
						'url'         => '',
						'is_external' => true,
						'nofollow'    => false,
					],
					'label_block'   => true
				]
			);
			$repeater->add_control(
				'button_color',
				[
					'label'     => esc_html__( 'Button Color', 'draly-dropshipping-starter' ),
					'type'      => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} {{CURRENT_ITEM}} .slider-button' => 'background-color: {{VALUE}}',
					],
				]
			);
			$this->add_control(
				'list',
				[
					'label'       => __( 'List', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::REPEATER,
					'fields'      => $repeater->get_controls(),
					'title_field' => '{{ title }}',
				]
			);
			$this->end_controls_section();
			//Layout Section
			$this->start_controls_section(
				'layout_section',
				[
					'label' => esc_html__( 'Layout', 'draly-dropshipping-starter' ),
				]
			);
			$this->add_control(
				'owl_vertical',
				[
					'label'       => esc_html__( 'Vertical Slide Mode', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SELECT,
					'options'     => [
						'true'  => esc_html__( 'Yes', 'draly-dropshipping-starter' ),
						'false' => esc_html__( 'No', 'draly-dropshipping-starter' ),
					],
					'default'     => 'false',
					'label_block' => true
				]
			);
			$this->add_control(
				'owl_verticalswiping',
				[
					'label'       => esc_html__( 'Vertical Swipe Mode', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SELECT,
					'options'     => [
						'true'  => esc_html__( 'Yes', 'draly-dropshipping-starter' ),
						'false' => esc_html__( 'No', 'draly-dropshipping-starter' ),
					],
					'default'     => 'false',
					'condition'   => array(
						'owl_vertical' => 'true'
					),
					'label_block' => true
				]
			);
			$this->add_control(
				'owl_autoplay',
				[
					'label'       => esc_html__( 'AutoPlay', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SELECT,
					'options'     => [
						'true'  => esc_html__( 'Yes', 'draly-dropshipping-starter' ),
						'false' => esc_html__( 'No', 'draly-dropshipping-starter' ),
					],
					'default'     => 'false',
					'description' => esc_html__( 'Enables Autoplay', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$this->add_control(
				'owl_autoplayspeed',
				[
					'label'       => esc_html__( 'Autoplay Speed', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::NUMBER,
					'min'         => 1000,
					'default'     => 5000,
					'description' => esc_html__( 'Autoplay Speed in milliseconds', 'draly-dropshipping-starter' ),
					'condition'   => array(
						'owl_autoplay' => 'true'
					),
					'label_block' => true
				]
			);
			$this->add_control(
				'owl_navigation',
				[
					'label'       => esc_html__( 'Navigation', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SELECT,
					'options'     => [
						'true'  => esc_html__( 'Yes', 'draly-dropshipping-starter' ),
						'false' => esc_html__( 'No', 'draly-dropshipping-starter' ),
					],
					'default'     => 'false',
					'description' => esc_html__( 'Prev/Next Arrows', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$this->add_control(
				'owl_dots',
				[
					'label'       => esc_html__( 'Dots', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SELECT,
					'options'     => [
						'true'  => esc_html__( 'Yes', 'draly-dropshipping-starter' ),
						'false' => esc_html__( 'No', 'draly-dropshipping-starter' ),
					],
					'default'     => 'true',
					'description' => esc_html__( 'Show dot indicators', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$this->add_control(
				'owl_loop',
				[
					'label'       => esc_html__( 'Loop', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::SELECT,
					'options'     => [
						'true'  => esc_html__( 'Yes', 'draly-dropshipping-starter' ),
						'false' => esc_html__( 'No', 'draly-dropshipping-starter' ),
					],
					'default'     => 'true',
					'description' => esc_html__( 'Infinite loop sliding', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$this->add_control(
				'owl_slidespeed',
				[
					'label'       => esc_html__( 'Slide Speed', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::NUMBER,
					'min'         => 300,
					'default'     => 300,
					'description' => esc_html__( 'Slide Speed in milliseconds', 'draly-dropshipping-starter' ),
					'label_block' => true
				]
			);
			$this->add_control(
				'owl_slide_margin',
				[
					'label'       => esc_html__( 'Margin', 'draly-dropshipping-starter' ),
					'type'        => \Elementor\Controls_Manager::NUMBER,
					'min'         => 0,
					'default'     => 0,
					'description' => esc_html__( 'Distance( or space) between 2 items', 'draly-dropshipping-starter' ),
					'condition'   => array(
						'owl_vertical' => 'false'
					),
					'label_block' => true
				]
			);
			$this->end_controls_section();

		}

		/**
		 * Render the widget output on the frontend.
		 *
		 * Written in PHP and used to generate the final HTML.
		 *
		 * @since 1.0.0
		 *
		 * @access protected
		 */
		protected function render() {
			$atts                      = $this->get_settings_for_display();
			$css_class                 = array( 'draly-slider' );
			$css_class[]               = $atts['style'];
			$owl_settings              = Draly_Dropshipping_Starter_Helper::carousel_data_attributes( 'owl_', $atts );
			echo '<style>';
			if ( ! empty( $atts['height']['size'] ) ) {
				echo esc_html( $this->get_unique_selector() ) . ' .slider-inner { height: ' . esc_attr( $atts['height']['size'] ) . 'px; }';
			}
			if ( ! empty( $atts['height_mobile']['size'] ) ) {
				echo '@media(max-width: 767px) { ' . esc_html( $this->get_unique_selector() ) . ' .slider-inner { height: ' . esc_attr( $atts['height_mobile']['size'] ) . 'px; } }';
			}
			if ( ! empty( $atts['height_mobile_extra_small']['size'] ) ) {
				echo '@media(max-width: 480.98px) { ' . esc_html( $this->get_unique_selector() ) . ' .slider-inner { height: ' . esc_attr( $atts['height_mobile_extra_small']['size'] ) . 'px; } }';
			}
			echo '</style>';
			if ( ! empty( $atts['list'] ) ) {
				?>
                <div class="<?php echo esc_attr( implode( ' ', $css_class ) ); ?>">
                    <div class="owl-slick" <?php echo esc_attr( $owl_settings ); ?>>
						<?php
						$i = 0;
						foreach ( $atts['list'] as $list ) {
							$i ++;
							?>
                            <div class="slider-item elementor-repeater-item-<?php echo esc_attr( $list['_id'] ) ?>">
                                <div class="slider-inner">
									<?php
									$image                    = ! empty( wp_get_attachment_url( $list['image']['id'] ) ) ? set_url_scheme( wp_get_attachment_url( $list['image']['id'] ), 'https' ) : \Elementor\Utils::get_placeholder_image_src();
									$image_mobile             = ! empty( wp_get_attachment_url( $list['image_mobile']['id'] ) ) ? set_url_scheme( wp_get_attachment_url( $list['image_mobile']['id'] ), 'https' ) : $image;
									$image_mobile_extra_small = ! empty( wp_get_attachment_url( $list['image_mobile_extra_small']['id'] ) ) ? set_url_scheme( wp_get_attachment_url( $list['image_mobile_extra_small']['id'] ), 'https' ) : $image_mobile;
									$loading                  = ( $i == 1 ) ? 'eager' : 'lazy';
									$fetchpriority            = ( $i == 1 ) ? 'high' : 'auto';
									?>
                                    <picture>
                                        <source media="(max-width: 480.98px)"
                                                srcset="<?php echo esc_url( $image_mobile_extra_small ); ?>">
                                        <source media="(max-width: 767px)"
                                                srcset="<?php echo esc_url( $image_mobile ); ?>">
                                        <img src="<?php echo esc_url( $image ); ?>"
                                             alt="img" loading="<?php echo esc_attr( $loading ); ?>"
                                             fetchpriority="<?php echo esc_attr( $fetchpriority ); ?>">
                                    </picture>
                                    <div class="slider-info">
										<?php if ( $list['subtitle'] ): ?>
                                            <div class="subtitle">
												<?php if ( $list['icon']['id'] ):
													echo wp_get_attachment_image( $list['icon']['id'], 'full' );
												endif; ?>
                                                <span><?php echo esc_html( $list['subtitle'] ); ?></span>
                                            </div>
										<?php endif; ?>
										<?php if ( $list['title'] ): ?>
                                            <h1 class="title"><?php echo wp_kses_post( $list['title'] ); ?></h1>
										<?php endif; ?>
										<?php if ( $list['desc'] ): ?>
                                            <div class="desc"><?php echo wp_kses_post( $list['desc'] ); ?></div>
										<?php endif; ?>
										<?php
										if ( $list['link_text'] && $list['link_url']['url'] ):
											$target = $list['link_url']['is_external'] ? '_blank' : '_self';
											?>
                                            <a class="slider-button"
                                               href="<?php echo esc_url( $list['link_url']['url'] ); ?>"
                                               target="<?php echo esc_attr( $target ); ?>"><?php echo esc_html( $list['link_text'] ); ?></a>
										<?php endif; ?>
                                    </div>
                                </div>
                            </div>
						<?php } ?>
                    </div>
                </div>
				<?php
			}
		}
	}
}