<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly


class TH90_S_Sliderthumb2 extends \Elementor\Widget_Base {

	public function get_script_depends() {
		return [ 'slick', 'th90-front' ];
	}

	public function get_name() {
		return 's-sliderthumb2';
	}

	public function get_title() {
		return __( 'Slider with Thumbnail 2', 'atlas-core' );
	}

	public function get_icon() {
		return 'eicon-thumbnails-down th90-widget-icon';
	}

	public function get_categories() {
		return [ sanitize_key( wp_get_theme()->name ) . '-sliders' ];
	}

	public function controls_general() {

		$this->start_controls_section(
			'section_general',
			[
				'label' => __( 'General', 'atlas-core' ),
			]
		);

		$controls = new TH90_Controls();
		$controls->general_query_default( $this );

		$this->add_responsive_control(
			'columns',
			[
				'label' => __( 'Thumbnails Columns', 'atlas-core' ),
				'type'  => \Elementor\Controls_Manager::SLIDER,
				'range' => [
					'px' => [
						'min' => 1,
						'max' => 15,
					],
				],
				'default' => [
					'size' => 2,
					'unit' => 'px',
				],
			]
		);

		$this->add_responsive_control(
			'spaceBetween',
			[
				'label' =>esc_html__( 'Space beetween slider item', 'atlas-core' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 100,
					],
				],
				'default' => [
					'size' => 20,
					'unit' => 'px',
				],
                'selectors' => [
                    '{{WRAPPER}} .slick-list' => 'margin-left: calc(({{SIZE}}{{UNIT}}/2)*-1); margin-right: calc(({{SIZE}}{{UNIT}}/2)*-1);',
                    '{{WRAPPER}} .slick-slide' => 'margin-left: calc({{SIZE}}{{UNIT}}/2); margin-right: calc({{SIZE}}{{UNIT}}/2);',
				],
			]
		);

		$this->add_control(
			'slider_auto',
			[
				'label' => esc_html__('Automatic slider?', 'atlas-core'),
				'type' => \Elementor\Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Yes', 'atlas-core'),
				'label_off' => esc_html__('No', 'atlas-core'),
				'default' => 'no',
			]
		);
		$this->add_control(
			'slider_loop',
			[
				'label' => esc_html__('Loop Mode / Infinite Loop?', 'atlas-core'),
				'type' => \Elementor\Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Yes', 'atlas-core'),
				'label_off' => esc_html__('No', 'atlas-core'),
				'default' => 'no',
			]
		);

		$this->add_control(
			'slider_speed',
			[
				'label' => __( 'Slider speed', 'atlas-core' ),
				'description' => __( 'Speed in milliseconds', 'atlas-core' ),
				'type' => \Elementor\Controls_Manager::NUMBER,
				'min' => 200,
				'max' => 3000,
				'step' => 1,
				'default' => 500,
			]
		);

		/* Default Post Options */
		$this->add_control(
			'post_style',
			[
				'label' => esc_html__( 'Big Post Style', 'textdomain' ),
				'type' => \Elementor\Controls_Manager::HIDDEN,
				'default' => 'hero',

			]
		);
		$this->add_control(
			'_post_style_small_x',
			[
				'label' => esc_html__( 'Small Post Style', 'textdomain' ),
				'type' => \Elementor\Controls_Manager::HIDDEN,
				'default' => 'hero',
				'selectors' => [
                    '{{WRAPPER}} .post-item.post_s .post-small .thumb-container' => 'padding-bottom: 100% !important;',
				],
			]
		);

		$controls->post_default( $this, '', '_b', true );

		$this->end_controls_section();
	}

	public function controls_query() {
		/* Section Query */
		$this->start_controls_section(
			'section_query_filter',
			[
				'label' => __( 'Posts Query', 'atlas-core' ),
			]
		);
		$controls = new TH90_Controls();
		$controls->query_default( $this );
		$this->end_controls_section();
	}

	public function style_controls_posts() {

		$controls = new TH90_Controls();

		$this->start_controls_section(
			'_section_style_post_big',
			[
				'label' => __( 'Post big Style', 'atlas-core' ),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$controls->post_style_default( $this, '', '_b' );
		$this->end_controls_section();

		$this->start_controls_section(
			'_section_style_post_small',
			[
				'label' => __( 'Post small Style', 'atlas-core' ),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$controls->post_style_default( $this, 'small', '_s' );
		$this->end_controls_section();
	}

	protected function register_controls() {

		$this->controls_general();

		$this->controls_query();

		$this->style_controls_posts();

		/* Widget Boxed */
		$controls = new TH90_Controls();
		$controls->box_settings( $this );
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 */
	protected function render() {
		global $block_id;
		$block_id++;
		$atts = $this->get_settings_for_display();
		$atts['categories'] = ! empty( $atts['categories'] ) ? implode( ',', $atts['categories'] ) : '';
		$atts['tags'] = ! empty( $atts['tags'] ) ? implode( ',', $atts['tags'] ) : '';
		$atts['posts'] = ! empty( $atts['posts'] ) ? implode( ',', $atts['posts'] ) : '';
		$atts['formats'] = ! empty( $atts['formats'] ) ? implode( ',', $atts['formats'] ) : '';
		$atts = th90_archive_related_atts( $atts );
		$atts['image_size_b'] = !$atts['image_size_b'] ? 'medium-large' : $atts['image_size_b'];

		/* Module Classes */
		$wrapper_classes = array_merge(
			array(
				'th90-block',
			),
			th90_box_class( $atts )
		);

		/* Slider Classes */
		$slider_classes = array(
			'block-slider',
			'slider-thumbs2',
		);

		/* Slider Config */
		$atts['_heading_nav'] = true;
		$slider_thumb_config = array(
			'loop' => 'yes' == $atts['slider_loop'] ? true : false,
			'autoplay' => 'yes' == $atts['slider_auto'] ? true : false,
			'autoHeight' => false,
			'nav' => true,
			'delay' => 5000,
		);
		/* Responsive Settings */
		$slider_thumb_config['view'] = $slider_thumb_config['t_view'] = $atts['columns']['size'];
		$slider_thumb_config['m_view'] = $slider_thumb_config['ms_view'] = 1;
		if ( isset( $atts['columns_tablet']['size'] ) && $atts['columns_tablet']['size'] ) {
			$slider_thumb_config['t_view'] = $atts['columns_tablet']['size'];
		}
		if ( isset( $atts['columns_mobile_extra']['size'] ) && $atts['columns_mobile_extra']['size'] ) {
			$slider_thumb_config['m_view'] = $atts['columns_mobile_extra']['size'];
		} else {
			if ( isset( $atts['columns_mobile']['size'] ) && $atts['columns_mobile']['size'] ) {
				$slider_thumb_config['m_view'] = $atts['columns_mobile']['size'];
			}
		}
		if ( isset( $atts['columns_mobile']['size'] ) && $atts['columns_mobile']['size'] ) {
			$slider_thumb_config['ms_view'] = $atts['columns_mobile']['size'];
		}

		if ( ! empty( $atts['slider_speed'] ) ) {
			$slider_thumb_config['speed'] = $atts['slider_speed'];
		}

		$responsive_classes = array(
			'slick-slider',
			'd' . $slider_thumb_config['view'],
			't' . $slider_thumb_config['t_view'],
			'm' . $slider_thumb_config['m_view'],
			'ms' . $slider_thumb_config['ms_view'],
		);

		/* Slider Atts */
		$slider_thumb_atts = array(
			'class' => 'th90-slider',
			'id'    => 'slider-thumb-' . absint( $block_id ),
		);

		$atts = th90_blog_atts_convert( $atts, '_b' );

		/* Render HTML */
		?>
		<div id="th90-block_<?php echo absint( $block_id ); ?>" class="<?php echo esc_attr( implode( ' ', array_filter( $wrapper_classes ) ) ); ?>">
			<div class="<?php echo esc_attr( implode( ' ', array_filter( $slider_classes ) ) ); ?>">
				<?php th90_box_heading( $atts ); ?>
				<?php
				/* Render Posts */
				$count = 0;
				$block_query = th90_query( $atts );
				if ( $block_query->have_posts() ) {
					while ( $block_query->have_posts() ) {
						$block_query->the_post();
						$count++;
						if ( 1 == $count ) {
							get_template_part( 'template-parts/posts/post', 'hero', array(
								'block'               => $atts,
								'count'               => $count,
								'slider_thumb_atts'   => $slider_thumb_atts,
								'slider_thumb_config' => $slider_thumb_config,
								'sufix'               => '_b',
								'responsive_classes'  => $responsive_classes,
							) );
						}
						// Do not duplicate posts ----------
						if ( isset( $atts['not_show_duplicate'] ) && 'yes' == $atts['not_show_duplicate'] ) {
							th90_do_not_duplicate( get_the_ID() );
						}
					} wp_reset_postdata();
				}
				?>
			</div>
		</div>
		<?php
	}

	/**
	 * Render the widget output in the editor.
	 *
	 * Written as a Backbone JavaScript template and used to generate the live preview.
	 */
	protected function content_template() {}
}
