<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Sifency\Traits\Sifency_Addons_Query_Arg_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Addons_Parity_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Slider_Controls;
use Sifency\Traits\Sifency_Blog_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;

class Sifency_Addons_Widget_Blog extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Slider_Controls;
	use Sifency_Addons_Query_Arg_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Button_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Blog_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	use \Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-blog-post',
			'title'          => 'Sifency Blog',
			'icon'           => 'eicon-posts-grid',
			'category'       => 'sifency-advance-widgets',
			'keywords'       => array( 'blog', 'posts', 'grid', 'loop' ),
			'style_depends'  => array( 'sifency-blog-style', 'sifency-typo-style', 'sifency-button-style', 'swiper', 'sifency-frontend-style', 'sifency-icon' ),
			'script_depends' => array( 'swiper', 'sifency-frontend-script', 'sifency-carousel-handler' ),
		);}
	protected function register_controls() {
		$carousel_condition = array(
			'condition' => array(
				'layout_style' => 'carousel',
			),
		);
		$this->add_sifency_addons_section_header_controls();
		$this->add_sifency_blog_controls();
		$this->add_sifency_arg_controls();
		$this->add_sifency_addons_slider_controls( $carousel_condition );
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'condition' => array(
					'skin_layout!' => 'overlay',
				),
				'exclude'   => array( 'transition' ),
				'selector'  => '.sifency-addons-blog-item',
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'    => 'overlay_item',
				'label'     => esc_html__( 'Overlay Box Style', 'sifency-addons' ),
				'selector'  => '.sifency-skin-overlay .sifency-blog-inner-wrapper',
				'condition' => array(
					'skin_layout' => 'overlay',
				),
				'exclude'   => array( 'transition' ),
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'content_wrapper_',
				'label'    => esc_html__( 'Content Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-addons-blog-content',
				'exclude'  => array( 'transition', 'dimensions', 'tabs' ),
			)
		);
		$this->start_controls_section(
			'align_section',
			array(
				'label'     => esc_html__( 'Alignement', 'sifency-addons' ),
				'tab'       => \Elementor\Controls_Manager::TAB_CONTENT,
				'condition' => array(
					'skin_layout' => 'overlay',
				),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'    => 'content_flex_',
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'selector'  => '.skin-overlay .sifency-blog-inner-wrapper',
				'condition' => array(
					'skin_layout' => 'overlay',
				),
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'heading_style',
				'label'    => 'Blog Title',
				'selector' => '.sifency-heading-style',
				'exclude'  => array( 'transition' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'meta_style',
				'label'      => 'Meta Data',
				'selector'   => '.sifency-meta-style',
				'color_only' => true,
				'exclude'    => array( 'shadow', 'stroke', 'alignment' ),
				'condition'  => array( 'show_date' => 'yes' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'category_tags_style_',
				'label'      => 'Catergory & Tags',
				'selector'   => '.sifency-meta-cat, .sifency-meta-tags',
				'color_only' => true,
				'exclude'    => array( 'shadow', 'stroke', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'     => 'content_style',
				'label'      => 'Blog Excerpt',
				'selector'   => '.sifency-content-style',
				'color_only' => true,
				'exclude'    => array( 'transition', 'tabs', 'bg', 'radius', 'shadow', 'stroke' ),
				'condition'  => array( 'show_excerpt' => 'yes' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'condition' => array(
					'show_read_more'  => 'yes',
					'read_more_text!' => '',
				),
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'selector' => '.sifency-icon-for-blog',
				'label'    => 'Meta Icons Style',
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selectors'    => array(
					'image'      => '.sifency-addons-blog-image img',
					'container'  => '.sifency-addons-blog-image',
					'dimensions' => '.sifency-addons-blog-image img',
				),
				'show_overlay' => false,
				'condition'    => array( 'show_excerpt' => 'yes' ),
			)
		);
		Sifency_Addons_Parity_Style_Controls::register( $this );
		$this->add_Sifency_Addons_Animation_Controls();
		$this->add_Sifency_Addons_Custom_CSS_Controls();
	}

	protected function print_post_image( $settings ) {
		if ( ! has_post_thumbnail() || 'yes' !== $settings['show_thumbnail'] ) {
			return;
		}
		?>
		<div class="sifency-addons-blog-image sifency-image-container">
			<a href="<?php the_permalink(); ?>" class="sifency-image img">
				<?php the_post_thumbnail( 'medium_large' ); ?>
			</a>
			<div class="sifency-image-overlay"></div>

			<?php
			if ( 'floating' === $settings['skin_layout'] && 'yes' === $settings['show_date'] ) :
				?>
				<div class="sifency-floating-date sifency-meta-style">
					<span class="day"><?php echo get_the_date( 'd' ); ?></span>
					<span class="month"><?php echo get_the_date( 'M' ); ?></span>
				</div>
			<?php endif; ?>
		</div>
		<?php
	}

	protected function print_post_meta_date( $settings ) {
		if ( 'yes' !== $settings['show_date'] ) {
			return;
		}

		if ( 'floating' === $settings['skin_layout'] ) {
			return;
		}

		$date_string = ( 'custom' === $settings['date_format'] ) ? get_the_date( $settings['custom_date_format'] ) : get_the_date();
		?>
		<div class="sifency-addons-blog-meta sifency-meta-style date-meta">
			<?php if ( ! empty( $settings['date_icon']['value'] ) ) : ?>
				<span class="sifency-icon-for-blog">
					<?php \Elementor\Icons_Manager::render_icon( $settings['date_icon'], array( 'aria-hidden' => 'true' ) ); ?>
				</span>
			<?php endif; ?>
			<?php echo esc_html( $date_string ); ?>
		</div>
		<?php
	}

	protected function print_post_meta_cats_tags( $settings ) {
		if ( 'yes' !== $settings['show_category'] && 'yes' !== $settings['show_tags'] ) {
			return;
		}

		if ( 'yes' === $settings['show_category'] ) {
			$categories = get_the_category();
			if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) {

				if ( ! empty( $settings['category_limit'] ) ) {
					$categories = array_slice( $categories, 0, $settings['category_limit'] );
				}

				$cat_links = array();
				foreach ( $categories as $cat ) {
					$cat_links[] = '<a href="' . esc_url( get_category_link( $cat->term_id ) ) . '">' . esc_html( $cat->name ) . '</a>';
				}
				$categories_output = implode( ', ', $cat_links );

				?>
					<span class="sifency-meta-cat sifency-meta-style">
					<?php if ( ! empty( $settings['category_icon']['value'] ) ) : ?>
							<span class="sifency-icon-for-blog">
								<?php \Elementor\Icons_Manager::render_icon( $settings['category_icon'], array( 'aria-hidden' => 'true' ) ); ?>
							</span>
						<?php endif; ?>
					<?php echo $categories_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					</span>
					<?php
			}
		}

		if ( 'yes' === $settings['show_tags'] ) {
			$tags = get_the_tags();
			if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) {

				if ( ! empty( $settings['tag_limit'] ) ) {
					$tags = array_slice( $tags, 0, $settings['tag_limit'] );
				}

				$tag_links = array();
				foreach ( $tags as $tag ) {
					$tag_links[] = '<a href="' . esc_url( get_tag_link( $tag->term_id ) ) . '">' . esc_html( $tag->name ) . '</a>';
				}
				$tags_output = implode( ', ', $tag_links );

				?>
					<span class="sifency-meta-tag sifency-meta-style">
					<?php if ( ! empty( $settings['tag_icon']['value'] ) ) : ?>
							<span class="sifency-icon-for-blog">
								<?php \Elementor\Icons_Manager::render_icon( $settings['tag_icon'], array( 'aria-hidden' => 'true' ) ); ?>
							</span>
						<?php endif; ?>
					<?php echo $tags_output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					</span>
				<?php
			}
		}
	}
	protected function print_post_title( $settings ) {
		?>
		<a href="<?php the_permalink(); ?>" class="sifency-heading-style"><?php the_title(); ?></a>
		<?php
	}

	protected function print_post_excerpt( $settings ) {
		if ( 'yes' !== $settings['show_excerpt'] ) {
			return;
		}

		$excerpt_length = ! empty( $settings['excerpt_length'] ) ? $settings['excerpt_length'] : 20;
		$excerpt        = wp_trim_words( get_the_excerpt(), $excerpt_length, '...' );
		?>
		<div class="sifency-addons-blog-excerpt sifency-content-style">
			<?php echo wp_kses_post( $excerpt ); ?>
		</div>
		<?php
	}

	protected function print_read_more( $settings ) {
		if ( 'yes' !== $settings['show_read_more'] || empty( $settings['read_more_text'] ) ) {
			return;
		}
		?>
		<div class="sifency-read-more-wrap">
			<a href="<?php the_permalink(); ?>" class="sifency-button-style sifency-addons-primary-button">
				<?php echo esc_html( $settings['read_more_text'] ); ?> 
				
				<?php if ( ! empty( $settings['read_more_icon']['value'] ) ) : ?>
					<?php \Elementor\Icons_Manager::render_icon( $settings['read_more_icon'], array( 'aria-hidden' => 'true' ) ); ?>
				<?php endif; ?>
			</a>
		</div>
		<?php
	}

	/**
	 * Renders the blog post item with a floating skin layout
	 *
	 * @param array $settings The blog settings
	 * @since 1.0.0
	 */
	protected function render_skin_floating( $settings ) {
		?>
		<div class="sifency-blog-inner-wrapper">
			<?php $this->print_post_image( $settings ); ?>
			
			<div class="sifency-addons-blog-content">
				<?php $this->print_post_title( $settings ); ?>
				<?php $this->print_post_meta_cats_tags( $settings ); ?>
				<?php $this->print_post_excerpt( $settings ); ?>
				<?php $this->print_read_more( $settings ); ?>
			</div>
		</div>
		<?php
	}

	protected function render_skin_overlay( $settings ) {
		?>
		<div class="sifency-blog-inner-wrapper">
			<?php $this->print_post_image( $settings ); ?>
			
			<div class="sifency-addons-blog-content">
				<div class="sifency-post-meta-data top-meta">
					<?php $this->print_post_meta_date( $settings ); ?>
				</div>
				
				<?php $this->print_post_title( $settings ); ?>
				<?php $this->print_post_excerpt( $settings ); ?>
				
				<div class="sifency-overlay-footer">
					<?php $this->print_post_meta_cats_tags( $settings ); ?>
					<?php $this->print_read_more( $settings ); ?>
				</div>
			</div>
		</div>
		<?php
	}

	protected function render_skin_standard( $settings ) {
		?>
		<div class="sifency-blog-inner-wrapper">
			<?php $this->print_post_image( $settings ); ?>
			
			<div class="sifency-addons-blog-content">
				<div class="sifency-post-meta-data top-meta">
					<?php $this->print_post_meta_date( $settings ); ?>
				</div>
				<?php $this->print_post_meta_cats_tags( $settings ); ?>
				<?php $this->print_post_title( $settings ); ?>
				<?php $this->print_post_excerpt( $settings ); ?>
				<?php $this->print_read_more( $settings ); ?>
			</div>
		</div>
		<?php
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$this->sifency_addons_render_custom_css();

		$query_args              = $this->get_sifency_addons_query_args();
		$query_args['post_type'] = $settings['query_post_type'] ?? 'post';

		$query = new \WP_Query( $query_args );
		if ( ! $query->have_posts() ) {
			return;
		}
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );

		$wrapper_classes   = array( 'sifency-addons-blog-wrapper' );
		$wrapper_classes[] = 'skin-' . $settings['skin_layout'];

		$is_carousel = 'carousel' === $settings['layout_style'];

		if ( $is_carousel ) {
			$wrapper_classes[] = 'sifency-addons-blog--carousel';
			$wrapper_classes[] = 'swiper-container';
			$post_count        = $query->post_count;
			$slider_options    = $this->get_sifency_swiper_options( $settings, $post_count );
			$this->add_render_attribute( 'wrapper', 'data-swiper-options', wp_json_encode( $slider_options ) );
		} else {
			$wrapper_classes[] = 'sifency-addons-blog-grid';
		}

		$animation_wrapper_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div <?php echo wp_kses_post( $this->get_render_attribute_string( 'wrapper' ) . ' ' . $animation_wrapper_attrs ); ?>>
			<?php if ( $is_carousel ) : ?>
				<div class="swiper-wrapper">
			<?php endif; ?>

			<?php
			$post_index = 0;
			while ( $query->have_posts() ) :
				$query->the_post();
				++$post_index;
				$parity_class         = ( $post_index % 2 !== 0 ) ? 'odd-item' : 'even-item';
				$item_classes         = array( 'sifency-addons-blog-item', $parity_class );
				$animation_item_attrs = $animation_manager->get_item_attributes( $item_classes );
				?>
				<?php if ( $is_carousel ) : ?>
					<div class="swiper-slide">
				<?php endif; ?>
				
					<article <?php echo wp_kses_post( $animation_item_attrs ); ?>>
						
						<?php

						switch ( $settings['skin_layout'] ) {
							case 'floating':
								$this->render_skin_floating( $settings );
								break;
							case 'overlay':
								$this->render_skin_overlay( $settings );
								break;
							case 'classic':
							case 'minimal':
							case 'list':
							default:
								$this->render_skin_standard( $settings );
								break;
						}
						?>

					</article>

				<?php if ( $is_carousel ) : ?>
					</div>
				<?php endif; ?>
			<?php endwhile; ?>

			<?php if ( $is_carousel ) : ?>
					</div>
					<?php $this->render_sifency_addons_slider_nav( $settings ); ?>
					<?php $this->render_sifency_addons_slider_pagination( $settings ); ?>
			<?php endif; ?>
		</div>
		<?php
		$this->render_sifency_addons_numeric_pagination( $query, $settings, 'query_pagination_' );
		wp_reset_postdata();
	}
}
