<?php

namespace Elementor;

use WP_Query;

class ThemeDraft_Recent_Post_Widget extends Widget_Base {

	public function get_name() {

		return "themedraft_recent_post";
	}

	public function get_title() {
		return __( "Recent Posts", "themedraft-core" );
	}

	public function get_icon() {

		return "eicon-post-excerpt";
	}

	public function get_categories() {
		return [ 'themedraft_elements' ];
	}


	protected function register_controls() {
		//Content tab start
		$this->start_controls_section(
			'recent_post_settings',
			[
				'label' => __( 'Post Settings', 'themedraft-core' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'post_count',
			[
				'label'   => __( 'Number Of Post To Show', 'themedraft-core' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => - 1,
				'max'     => '',
				'step'    => 1,
				'default' => 4,
			]
		);

		$this->add_control(
			'category',
			[
				'label'       => __( 'Categories', 'themedraft-core' ),
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'multiple'    => true,
				'options'     => themedraft_post_categories(),
			]
		);

		$this->add_control(
			'exclude_post',
			[
				'label'       => __( 'Exclude Posts', 'themedraft-core' ),
				'type'        => Controls_Manager::SELECT2,
				'label_block' => true,
				'multiple'    => true,
				'description' => __( 'Select post id.', 'themedraft-core' ),
				'options'     => themedraft_get_post_id_as_list(),
			]
		);


		$this->add_control(
			'post_offset',
			[
				'label'   => __( 'Post Offset', 'themedraft-core' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 0,
				'max'     => '',
				'step'    => 1,
				'default' => 0,
			]
		);

		$this->add_control(
			'post_orderby',
			[
				'label'   => __( 'Order By', 'themedraft-core' ),
				'type'    => Controls_Manager::SELECT,
				'options' => themedraft_post_orderby_options(),
				'default' => 'date',

			]
		);

		$this->add_control(
			'post_order',
			[
				'label'   => __( 'Order', 'themedraft-core' ),
				'type'    => Controls_Manager::SELECT,
				'options' => [
					'asc'  => 'Ascending',
					'desc' => 'Descending'
				],
				'default' => 'desc',

			]
		);

		$this->add_control(
			'button_text',
			[
				'label'       => __( 'Button Text', 'themedraft-core' ),
				'label_block'       => true,
				'type'        => Controls_Manager::TEXT,
				'default'     => 'Read More',
			]
		);

		$this->add_control(
			'entrance_animation',
			[
				'label' => __( 'Entrance Animation', 'themedraft-core' ),
				'type' => Controls_Manager::ANIMATION,
				'prefix_class' => 'animated ',
			]
		);

		$this->add_control(
			'post_animation_duration',
			[
				'label'     => __( 'Animation Duration', 'themedraft-core' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => 'data-wow-duration="1.25s"',
				'options'   => [
					'data-wow-duration="2s"'    => __( 'Slow', 'themedraft-core' ),
					'data-wow-duration="1.25s"' => __( 'Normal', 'themedraft-core' ),
					'data-wow-duration=".75s"'  => __( 'Fast', 'themedraft-core' ),
				],
				'condition' => [
					'entrance_animation!' => '',
				]
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'recent_post_layout_settings',
			[
				'label' => __( 'Post Layout', 'themedraft-core' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'columns_desktop',
			[
				'label'   => __( 'Columns On Desktop', 'themedraft-core' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'col-lg-4',
				'options' => [
					'col-lg-12' => __( '1 Column', 'themedraft-core' ),
					'col-lg-6'  => __( '2 Column', 'themedraft-core' ),
					'col-lg-4'  => __( '3 Column', 'themedraft-core' ),
					'col-lg-3'  => __( '4 Column', 'themedraft-core' ),
				],
			]
		);

		$this->add_control(
			'columns_tab',
			[
				'label'   => __( 'Columns On Tablet', 'themedraft-core' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'col-sm-6',
				'options' => [
					'col-sm-12' => __( '1 Column', 'themedraft-core' ),
					'col-sm-6'  => __( '2 Column', 'themedraft-core' ),
					'col-sm-4'  => __( '3 Column', 'themedraft-core' ),
					'col-sm-3'  => __( '4 Column', 'themedraft-core' ),
				],
			]
		);

		$this->add_control(
			'show_excerpt',
			[
				'label'   => __( 'Excerpt', 'themedraft-core' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'excerpt_lenth',
			[
				'label'     => __( 'Excerpt Lenth', 'themedraft-core' ),
				'type'      => Controls_Manager::NUMBER,
				'min'       => 0,
				'max'       => '',
				'step'      => 1,
				'default'   => 16,
				'condition' => [
					'show_excerpt' => 'yes',
				]
			]
		);

		$this->add_control(
			'title_word',
			[
				'label'   => __( 'Title Word Count', 'themedraft-core' ),
				'type'    => Controls_Manager::NUMBER,
				'min'     => 0,
				'max'     => '',
				'step'    => 1,
				'default' => 8,
			]
		);

		$this->add_control(
			'show_rm_button',
			[
				'label'   => __( 'Read More Button', 'themedraft-core' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'show_date',
			[
				'label'   => __( 'Show Date', 'themedraft-core' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'show_category',
			[
				'label'   => __( 'Show First Category Name', 'themedraft-core' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'show_comment_count',
			[
				'label'   => __( 'Show Comment Count', 'themedraft-core' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'recent_post_slide_options_settings',
			[
				'label' => esc_html__( 'Slider Options', 'themedraft-core' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'enable_slider',
			[
				'label'   => __( 'Enable Post Slider', 'themedraft-core' ),
				'type'    => Controls_Manager::SWITCHER,
				'default' => 'yes',
			]
		);

		$this->add_control(
			'desktop_count',
			[
				'label'       => __( 'Item Show On Desktop', 'themedraft-core' ),
				'type'        => Controls_Manager::SELECT,
				'show_label'  => true,
				'label_block' => false,
				'options'     => [
					1 => __( '1 Column', 'themedraft-core' ),
					2 => __( '2 Column', 'themedraft-core' ),
					3 => __( '3 Column', 'themedraft-core' ),
					4 => __( '4 Column', 'themedraft-core' ),
				],
				'default'     => 3,
				'condition'   => [
					'enable_slider' => 'yes',
				],
			]
		);

		$this->add_control(
			'tab_count',
			[
				'label'       => __( 'Item Show On Tablet', 'themedraft-core' ),
				'type'        => Controls_Manager::SELECT,
				'show_label'  => true,
				'label_block' => false,
				'options'     => [
					1 => __( '1 Column', 'themedraft-core' ),
					2 => __( '2 Column', 'themedraft-core' ),
					3 => __( '3 Column', 'themedraft-core' ),
					4 => __( '4 Column', 'themedraft-core' ),
				],
				'default'     => 2,
				'condition'   => [
					'enable_slider' => 'yes',
				],
			]
		);

		$this->add_control(
			'autoplay',
			[
				'label'       => __( 'Autoplay', 'themedraft-core' ),
				'type'        => Controls_Manager::SWITCHER,
				'show_label'  => true,
				'label_block' => false,
				'default'     => 'yes',
				'condition'   => [
					'enable_slider' => 'yes',
				],
			]
		);

		$this->add_control(
			'autoplay_interval',
			[
				'label'       => __( 'Autoplay Interval', 'themedraft-core' ),
				'type'        => Controls_Manager::SELECT,
				'show_label'  => true,
				'label_block' => false,
				'options'     => [
					'2000'  => __( '2 seconds', 'themedraft-core' ),
					'3000'  => __( '3 seconds', 'themedraft-core' ),
					'4000'  => __( '4 seconds', 'themedraft-core' ),
					'5000'  => __( '5 seconds', 'themedraft-core' ),
					'6000'  => __( '6 seconds', 'themedraft-core' ),
					'7000'  => __( '7 seconds', 'themedraft-core' ),
					'8000'  => __( '8 seconds', 'themedraft-core' ),
					'9000'  => __( '9 seconds', 'themedraft-core' ),
					'10000' => __( '10 seconds', 'themedraft-core' ),
				],
				'default'     => '4000',
				'conditions'  => [
					'terms' => [
						[
							'name'     => 'enable_slider',
							'operator' => '==',
							'value'    => 'yes',
						],
						[
							'name'     => 'autoplay',
							'operator' => '==',
							'value'    => 'yes',
						]

					],
				],
			]
		);

		$this->add_control(
			'infinity_loop',
			[
				'label'       => __( 'Loop', 'themedraft-core' ),
				'type'        => Controls_Manager::SWITCHER,
				'show_label'  => true,
				'label_block' => false,
				'default'     => 'yes',
				'condition'   => [
					'enable_slider' => 'yes',
				],
			]
		);

		$this->add_control(
			'dots',
			[
				'label'       => __( 'Navigation Dots', 'themedraft-core' ),
				'type'        => Controls_Manager::SWITCHER,
				'show_label'  => true,
				'label_block' => false,
				'default'     => 'no',
				'condition'   => [
					'enable_slider' => 'yes',
				],
			]
		);

		$this->add_control(
			'nav_arrow',
			[
				'label'       => __( 'Navigation Arrow', 'themedraft-core' ),
				'type'        => Controls_Manager::SWITCHER,
				'show_label'  => true,
				'label_block' => false,
				'default'     => 'yes',
				'condition'   => [
					'enable_slider' => 'yes',
				],
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
		    'post_styles',
		    [
		        'label' => esc_html__( 'Post Style', 'themedraft-core' ),
		        'tab'   => Controls_Manager::TAB_STYLE,
		    ]
		);

		$this->add_responsive_control(
			'thumbnail_height',
			[
				'label' => __( 'Thumbnail Height', 'themedraft-core' ),
				'type' => Controls_Manager::SLIDER,
				'size_units' => ['px'],
				'range' => [
					'px' => [
						'min' => 100,
						'max' => 500,
					],
				],
				'devices' => [ 'desktop', 'tablet', 'mobile' ],

				'selectors' => [
					'{{WRAPPER}} .td-recent-post-el-widget .td-post-thumbnail' => 'height: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$this->add_control(
			'date_color',
			[
				'label'       => esc_html__('Date Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-recent-post-el-widget .posted-on a' => 'color: {{VALUE}};',
				],

                'condition' => [
                    'show_date' => 'yes',
                ],
			]
		);

		$this->add_control(
		    'category_comment_count_color',
		    [
		        'label'       => esc_html__('Category & Comment Count Color', 'themedraft-core'),
		        'type'        => Controls_Manager::COLOR,
		        'selectors'   => [
		            '{{WRAPPER}} .td-recent-post-el-widget .td-post-meta li a,{{WRAPPER}} .td-recent-post-el-widget .td-post-meta li > span:before' => 'color: {{VALUE}};',
		        ],
		    ]
		);

		$this->add_control(
			'title_hover_color',
			[
				'label'       => esc_html__('Title Hover Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-recent-post-el-widget .post-title:hover' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'button_bg',
			[
				'label'       => esc_html__('Button Background Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-button' => 'background-color: {{VALUE}};',
				],
                'condition' => [
                    'show_rm_button' => 'yes',
                ],
			]
		);

		$this->add_control(
			'button_hover_bg',
			[
				'label'       => esc_html__('Button Background Color ON Hover', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-button:hover' => 'background-color: {{VALUE}};',
				],
				'condition' => [
					'show_rm_button' => 'yes',
				],
			]
		);

		$this->add_control(
			'arrow_dot_bg',
			[
				'label'       => esc_html__('Arrow & Dots Background Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-recent-post-el-widget .slick-arrow,{{WRAPPER}} .slick-dots button' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'arrow_dot_bg_hover',
			[
				'label'       => esc_html__('Arrow & Dots Hover Background Color', 'themedraft-core'),
				'type'        => Controls_Manager::COLOR,
				'selectors'   => [
					'{{WRAPPER}} .td-recent-post-el-widget .slick-arrow:hover,{{WRAPPER}} .td-recent-post-el-widget .slick-dots button:hover,{{WRAPPER}} .td-recent-post-el-widget .slick-dots .slick-active button' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name' => 'item_box_shadow',
				'label' => __( 'Box Shadow', 'themedraft-core' ),
				'selector' => '{{WRAPPER}} .td-recent-post-el-widget .td-single-post-content',
			]
		);

		$this->end_controls_section();
	}


	//Render In HTML
	protected function render() {
		$settings = $this->get_settings_for_display();
		if($settings['entrance_animation']){
			$td_animation = 'wow'.' ' . $settings['entrance_animation'];
			$td_animation_duration = $settings['post_animation_duration'];
		}else{
			$td_animation ='';
			$td_animation_duration ='';
		}

		if(is_rtl()){
			$slide_dir = 'dir="ltr"';
			$btn_icon = 'fas fa-arrow-left';
		}else{
			$slide_dir = '';
			$btn_icon = 'fas fa-arrow-right';
		}
		?>

		<div class="td-recent-post-el-widget <?php echo $td_animation;?>" <?php echo $td_animation_duration;?>>
			<?php if ( $settings['enable_slider'] == 'yes' ) : ?>
			<div class="container td-recent-post-slider-container">
				<?php endif; ?>
				<div <?php echo $slide_dir;?> class="row td-all-posts-wrapper <?php if ( $settings['enable_slider'] == 'yes' ) {
					echo 'td-resent-post-slider';
				} ?>">

					<?php
					if ( ! empty( $settings['category'] ) ) {
						$post_query = new WP_Query( array(
							'post_type'           => 'post',
							'posts_per_page'      => $settings['post_count'],
							'ignore_sticky_posts' => 1,
							'offset'              => $settings['post_offset'],
							'orderby'             => $settings['post_orderby'],
							'order'               => $settings['post_order'],
							'post__not_in'        => $settings['exclude_post'],
							'tax_query'           => array(
								array(
									'taxonomy' => 'category',
									'terms'    => $settings['category'],
									'field'    => 'slug',
								)
							)
						) );
					} else {

						$post_query = new WP_Query(
							array(
								'posts_per_page'      => $settings['post_count'],
								'post_type'           => 'post',
								'ignore_sticky_posts' => 1,
								'offset'              => $settings['post_offset'],
								'orderby'             => $settings['post_orderby'],
								'order'               => $settings['post_order'],
								'post__not_in'        => $settings['exclude_post'],
							)
						);
					}
					while ( $post_query->have_posts() ) : $post_query->the_post();
						?>

						<div class="td-single-post-item <?php echo esc_attr( $settings['columns_desktop'] . ' ' . $settings['columns_tab'] ) ?>">
							<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
								<div class="td-single-post-wrap">
									<div class="td-post-thumbnail td-cover-bg"
									     style="background-image: url(<?php echo the_post_thumbnail_url( 'full' ) ?>)">
										<?php if($settings['show_date'] == 'yes') : ?>
                                        <?php induxter_posted_on(); ?>
										<?php endif;?>
                                    </div>

									<div class="td-single-post-content">

										<div class="td-post-meta">
                                            <ul class="td-list-style td-list-inline">
	                                            <?php if($settings['show_category'] == 'yes') : ?>
											    <li><?php induxter_post_first_category(); ?></li>
	                                            <?php endif;?>

	                                            <?php if($settings['show_comment_count'] == 'yes') : ?>
                                                <li><?php induxter_comment_count(); ?></li>
	                                            <?php endif;?>
                                            </ul>
										</div>


                                        <a href="<?php echo esc_url( get_the_permalink() );?>">
                                            <h4 class="post-title"><?php echo wp_trim_words(get_the_title(), $settings['title_word'], ' ...');?></h4>
                                        </a>

										<?php if ( $settings['show_excerpt'] === 'yes' ) : ?>
											<div class="td-post-excerpt">
												<p><?php echo themedraft_get_excerpt( get_the_ID(), $settings['excerpt_lenth'] ); ?></p>
											</div>
										<?php endif; ?>

										<?php if ( $settings['show_rm_button'] == 'yes' ): ?>
											<div class="td-post-read-more">
												<a class="td-button"
												   href="<?php echo esc_url( get_the_permalink() ) ?>">
                                                    <?php if(is_rtl()) : ?>
                                                    <i class="fas fa-arrow-left"></i>
                                                    <?php endif;?>
													<?php echo esc_html__( $settings['button_text'] ); ?>
													<?php if(!is_rtl()) : ?>
                                                    <i class="fas fa-arrow-right"></i>
													<?php endif;?>
												</a>
											</div>

										<?php endif; ?>
									</div>
								</div>
							</article><!-- #post-<?php the_ID(); ?> -->
						</div>
					<?php
					endwhile;
					wp_reset_query();
					?>
				</div>
				<?php if ( $settings['enable_slider'] == 'yes' ) : ?>
					<script>
                        (function ($) {
                            "use strict";
                            $(document).ready(function () {
                                $(".td-resent-post-slider").slick({
                                    slidesToShow: <?php echo json_encode( $settings['desktop_count'] )?>,
                                    autoplay: <?php echo json_encode( $settings['autoplay'] == 'yes' ? true : false ); ?>,
                                    autoplaySpeed: <?php echo json_encode( $settings['autoplay_interval'] )?>,
                                    speed: 1500, // slide speed
                                    dots: <?php echo json_encode( $settings['dots'] == 'yes' ? true : false ); ?>,
                                    arrows: <?php echo json_encode( $settings['nav_arrow'] == 'yes' ? true : false ); ?>,
                                    prevArrow: '<i class="slick-arrow slick-prev fas fa-arrow-left"></i>',
                                    nextArrow: '<i class="slick-arrow slick-next fas fa-arrow-right"></i>',
                                    infinite: <?php echo json_encode( $settings['infinity_loop'] == 'yes' ? true : false ); ?>,
                                    pauseOnHover: false,
                                    centerMode: false,
                                    centerPadding: 30,
                                    responsive: [
                                        {
                                            breakpoint: 992,
                                            settings: {
                                                slidesToShow: <?php echo json_encode( $settings['tab_count'] )?>, //768-991
                                            }
                                        },
                                        {
                                            breakpoint: 768,
                                            settings: {
                                                slidesToShow: 1, // 0 -767
                                            }
                                        }
                                    ]
                                });
                            });
                        })(jQuery);
					</script>
				<?php endif; ?>
				<?php if ( $settings['enable_slider'] == 'yes' ) : ?>
			</div>
		<?php endif; ?>
		</div>
		<?php

	}
}

Plugin::instance()->widgets_manager->register( new ThemeDraft_Recent_Post_Widget );