<?php
namespace EcafeAddons\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Utils;

use EcafeAddons\EcafeElementLoad;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class Ecafe_Image_Scroll extends Widget_Base {

	public function get_name() {
		return 'ecafe-image-scroll';
	}

	public function get_title() {
		return esc_html__( 'Image Scroll', 'essential-classy-addons-for-elementor' );
	}

	public function get_icon() {
		return 'ecafe-ele-back-icon ecafe-image-scroll-icon';
	}

	public function get_categories() {
		return array( 'essential' );
	}

	public function get_keywords() {
		return array( 'image', 'image scroll', 'image scroll sequence', 'sequence' );
	}

	public function get_script_depends() {
		return array( 'ecafe-image-scroll' );
	}
	public function get_custom_help_url() {
		return 'https://store.kapasias.com/helpdesk/';
	}
	public function has_widget_inner_wrapper(): bool {
		return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}
	protected function register_controls() {
		$this->start_controls_section(
			'contentSection',
			array(
				'label' => esc_html__( 'Image Scroll', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			)
		);
		$this->add_control(
			'im_folder_path',
			array(
				'label'       => esc_html__( 'Image Folder Path', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::URL,
				'dynamic'     => array(
					'active' => true,
				),
				'placeholder' => esc_html__( 'https://ecaddons.com/images', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'im_prefix',
			array(
				'label'       => esc_html__( 'Prefix', 'essential-classy-addons-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'placeholder' => esc_html__( 'Enter image prefix', 'essential-classy-addons-for-elementor' ),
			)
		);
		$this->add_control(
			'im_string_digit',
			array(
				'label'   => esc_html__( 'String Digit', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => '1',
				'options' => array(
					'1' => esc_html__( '1 - 9', 'essential-classy-addons-for-elementor' ),
					'2' => esc_html__( '01 - 99', 'essential-classy-addons-for-elementor' ),
					'3' => esc_html__( '001 - 999', 'essential-classy-addons-for-elementor' ),
					'4' => esc_html__( '0001 - 9999', 'essential-classy-addons-for-elementor' ),
					'5' => esc_html__( '00001 - 99999', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$this->add_control(
			'im_image_type',
			array(
				'label'   => esc_html__( 'Image Type', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'jpg',
				'options' => array(
					'jpg'  => esc_html__( 'JPG', 'essential-classy-addons-for-elementor' ),
					'jpeg' => esc_html__( 'JPEG', 'essential-classy-addons-for-elementor' ),
					'png'  => esc_html__( 'PNG', 'essential-classy-addons-for-elementor' ),
					'webp' => esc_html__( 'WEBP', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$this->add_control(
			'im_max_image',
			array(
				'label'   => esc_html__( 'Max. Image', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 1,
				'max'     => 5000,
				'step'    => 1,
				'default' => 100,
			)
		);
		$this->add_control(
			'im_effecton',
			array(
				'label'   => esc_html__( 'Effect On', 'essential-classy-addons-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'default',
				'options' => array(
					'default' => esc_html__( 'Default', 'essential-classy-addons-for-elementor' ),
					'body'    => esc_html__( 'Body', 'essential-classy-addons-for-elementor' ),
				),
			)
		);
		$this->end_controls_section();

		/*Notice panel*/
		$ecwidgetdemolink = esc_url( 'https://ecaddons.com/elements/image-sequence-scroll/' );
		$ecwidgetdoclink  = esc_url( 'https://ecaddons.com/docs/image-scroll-widget-tutorial/' );
		include ECAFE_PATH . 'element/widgets/ecafe-widgetpanel-help.php';

		$this->start_controls_section(
			'imageScrollStyling',
			array(
				'label' => esc_html__( 'Position', 'essential-classy-addons-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'imageScrollLeft',
			array(
				'label'      => esc_html__( 'Left Offset', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'range'      => array(
					'%' => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'default'    => array(
					'size' => 0,
				),
				'selectors'  => array(
					'#ec-image-scroll' . $this->get_id() . '' => 'left:{{SIZE}} !important;',
				),
			)
		);
		$this->add_responsive_control(
			'imageScrollTop',
			array(
				'label'      => esc_html__( 'Top Offset', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'range'      => array(
					'%' => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'default'    => array(
					'size' => 0,
				),
				'selectors'  => array(
					'#ec-image-scroll' . $this->get_id() . '' => 'top:{{SIZE}} !important;',
				),
			)
		);
		$this->add_responsive_control(
			'imageScrollWidth',
			array(
				'label'      => esc_html__( 'Width', 'essential-classy-addons-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( '%' ),
				'range'      => array(
					'%' => array(
						'min'  => 1,
						'max'  => 100,
						'step' => 1,
					),
				),
				'default'    => array(
					'size' => 100,
				),
				'selectors'  => array(
					'#ec-image-scroll' . $this->get_id() . '' => 'width:{{SIZE}}% !important;',
				),
			)
		);
		$this->end_controls_section();
	}
	protected function render() {
		$settings = $this->get_settings_for_display();

		$im_folder_path  = ! empty( $settings['im_folder_path']['url'] ) ? esc_url( $settings['im_folder_path']['url'] ) : '';
		$im_string_digit = ! empty( $settings['im_string_digit'] ) ? $settings['im_string_digit'] : 1;
		$imagetype       = ! empty( $settings['im_image_type'] ) ? $settings['im_image_type'] : 'jpg';
		$im_prefix       = ! empty( $settings['im_prefix'] ) ? $settings['im_prefix'] : '';
		$img_max_number  = ! empty( $settings['im_max_image'] ) ? $settings['im_max_image'] : '500';
		$im_effecton     = ! empty( $settings['im_effecton'] ) ? $settings['im_effecton'] : 'default';

		$data = array(
			'id'        => $this->get_id(),
			'imgurl'    => $im_folder_path,
			'imgstring' => $im_string_digit,
			'imgtype'   => $imagetype,
			'imgprefix' => $im_prefix,
			'maximage'  => $img_max_number,
			'effecton'  => $im_effecton,
		);

		$output = '';
		if ( ! empty( $im_folder_path ) ) {
			$output = '<div class="ec-image-scroll-wrapper" data-imagescroll="' . htmlspecialchars( json_encode( $data, true ), ENT_QUOTES, 'UTF-8' ) . '"></div>';
		}

		echo wp_kses_normalize_entities( $output );
	}
}
