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

class Sifency_Addons_Widget_Stack extends Widget_Base {
	use Sifency_Addons_Animation_Controls;
	use Sifency_Addons_Slider_Controls;
	use Sifency_Addons_Query_Arg_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Stack_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-stacks',
			'title'          => 'Sifency Stack',
			'icon'           => 'eicon-gallery-grid',
			'category'       => 'sifency-advance-widgets',
			'keywords'       => array( 'stacks', 'logo', 'companies', 'partners' ),
			'style_depends'  => array( 'sifency-typo-style', 'swiper', 'sifency-frontend-style', 'sifency-stacks-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_stacks_controls();
		$this->add_sifency_addons_slider_controls( $carousel_condition );
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'label'    => esc_html__( 'Stack Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-stack-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'condition'      => array( 'show_title' => 'yes' ),
				'selector'       => '.sifency-heading-style',
				'hover_selector' => 'sifency-stack-item:hover .sifency-heading-style',
				'exclude'        => array( 'bg', 'radius', 'shadow' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'  => '.sifency-stack-item img',
				'selectors' => array(
					'image'      => '.sifency-stack-item img',
					'container'  => '.sifency-stack-item img',
					'dimensions' => '.sifency-stack-item img',
				),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'excerpt_style',
				'label'          => 'Excerpt Style',
				'selector'       => '.stack-excerpt',
				'color_only'     => true,
				'exclude'        => array( 'shadow', 'transition', 'tabs' ),
				'show_max_width' => true,
				'condition'      => array( 'show_excerpt' => 'yes' ),
			)
		);
		Sifency_Addons_Parity_Style_Controls::register( $this );
		$this->add_Sifency_Addons_Animation_Controls();
	}

	protected function render() {
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$is_carousel       = ( 'carousel' === $settings['layout_style'] );

		$wrapper_classes = array( 'sifency-stacks-wrapper' );

		if ( $is_carousel ) {
			$wrapper_classes[] = 'sifency-carousel-wrapper';
			$wrapper_classes[] = 'swiper-container';

			$slide_count = ( 'posts' === $settings['source'] )
				? ( new \WP_Query( array_merge( $this->get_sifency_addons_query_args() ?? array(), array( 'post_type' => $settings['query_post_type'] ?? 'stack' ) ) ) )->post_count
				: count( $settings['stack_list'] ?? array() );

			$slider_options = $this->get_sifency_swiper_options( $settings, $slide_count );
			$this->add_render_attribute( 'wrapper', 'data-swiper-options', wp_json_encode( $slider_options ) );
		} else {
			$wrapper_classes[] = 'sifency-stacks--grid';
		}

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

			<?php
			if ( 'posts' === $settings['source'] ) {
				$this->render_posts_content( $settings, $animation_manager );
			} else {
				$this->render_custom_content( $settings, $animation_manager );
			}
			?>

			<?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
		// End main wrapper
	}

	private function render_custom_content( $settings, Sifency_Addons_Animation_Manager $animation_manager ) {
		if ( empty( $settings['stack_list'] ) ) {
			return; }

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

		foreach ( $settings['stack_list'] as $index => $item ) {
			$tag        = 'span';
			$item_attrs = array();

			if ( ! empty( $item['link']['url'] ) ) {
				$tag                = 'a';
				$item_attrs['href'] = esc_url( $item['link']['url'] );
				if ( ! empty( $item['link']['is_external'] ) ) {
					$item_attrs['target'] = '_blank';
				}
				if ( ! empty( $item['link']['nofollow'] ) ) {
					$item_attrs['rel'] = 'nofollow';
				}
			}

			$base_item_classes = array( 'sifency-stack-item' );
			if ( ( $index + 1 ) % 2 !== 0 ) {
				$base_item_classes[] = 'odd-item';
			} else {
				$base_item_classes[] = 'even-item';
			}

			if ( $is_carousel ) {
				$slide_attrs = $animation_manager->get_item_attributes( array( 'swiper-slide' ) );

				echo wp_kses_post( '<div ' . $slide_attrs . '>' );

				$this->add_render_attribute( 'inner_item', array( 'class' => $base_item_classes ) + $item_attrs, true );
				?>
				<<?php echo esc_html( $tag ); ?> <?php echo wp_kses_post( $this->get_render_attribute_string( 'inner_item' ) ); ?>>
					<?php if ( ! empty( $item['stack_logo']['url'] ) ) : ?>
						<img src="<?php echo esc_url( $item['stack_logo']['url'] ); ?>" alt="<?php echo esc_attr( $item['stack_name'] ); ?>" />
					<?php endif; ?>
				</<?php echo esc_html( $tag ); ?>>
				<?php
				echo '</div>'; // End .swiper-slide
			} else {
				$final_item_attrs = $animation_manager->get_item_attributes( $base_item_classes );

				$final_tag_attrs = $final_item_attrs;
				if ( 'a' === $tag ) {
					$link_attrs_string = '';
					foreach ( $item_attrs as $key => $value ) {
						$link_attrs_string .= esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
					}
					$final_tag_attrs = $link_attrs_string . $final_item_attrs;
				}
				?>
				
				<<?php echo esc_html( $tag ); ?> <?php echo wp_kses_post( $final_tag_attrs ); ?>>
					<?php if ( ! empty( $item['stack_logo']['url'] ) ) : ?>
						<img src="<?php echo esc_url( $item['stack_logo']['url'] ); ?>" alt="<?php echo esc_attr( $item['stack_name'] ); ?>" />
					<?php endif; ?>
					
					<?php if ( 'yes' === $settings['show_title'] && ! empty( $item['stack_name'] ) ) : ?>
						<h3 class="stack-title sifency-heading-style"><?php echo esc_html( $item['stack_name'] ); ?></h3>
					<?php endif; ?>
				</
				<?php
				echo esc_html( $tag );
				?>
				>

				<?php
			}
		}
	}

	private function render_posts_content( $settings, Sifency_Addons_Animation_Manager $animation_manager ) {
		$query_args              = method_exists( $this, 'get_sifency_addons_query_args' ) ? $this->get_sifency_addons_query_args() : array();
		$query_args['post_type'] = $settings['query_post_type'] ?? 'stack';
		$query                   = new \WP_Query( $query_args );
		$show_text_wrap          = ( 'yes' === $settings['show_title'] || 'yes' === $settings['show_excerpt'] );

		if ( ! $query->have_posts() ) {
			return; }

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

		while ( $query->have_posts() ) :
			$query->the_post();
			$stack_link = get_post_meta( get_the_ID(), 'stack_link', true );
			$tag        = empty( $stack_link ) ? 'span' : 'a';
			$item_attrs = array();

			if ( 'a' === $tag ) {
				$item_attrs['href'] = esc_url( $stack_link );
				$item_attrs['rel']  = 'noopener';
			}

			$base_item_classes = array( 'sifency-stack-item' );
			if ( ( $query->current_post + 1 ) % 2 !== 0 ) {
				$base_item_classes[] = 'odd-item';
			} else {
				$base_item_classes[] = 'even-item';
			}

			if ( $is_carousel ) {
				$slide_attrs = $animation_manager->get_item_attributes( array( 'swiper-slide' ) );

				echo wp_kses_post( '<div ' . $slide_attrs . '>' );
				$this->add_render_attribute( 'inner_post_item', array( 'class' => $base_item_classes ) + $item_attrs, true );
				?>
				<<?php echo esc_html( $tag ); ?> <?php echo wp_kses_post( $this->get_render_attribute_string( 'inner_post_item' ) ); ?>>
					<?php
					if ( has_post_thumbnail() ) {
						the_post_thumbnail( 'large' ); }
					?>
				</<?php echo esc_html( $tag ); ?>>
				<?php
				echo '</div>'; // End .swiper-slide
			} else {
				$final_item_attrs = $animation_manager->get_item_attributes( $base_item_classes );

				$final_tag_attrs = $final_item_attrs;
				if ( 'a' === $tag ) {
					$link_attrs_string = '';
					foreach ( $item_attrs as $key => $value ) {
						$link_attrs_string .= esc_attr( $key ) . '="' . esc_attr( $value ) . '" ';
					}
					$final_tag_attrs = $link_attrs_string . $final_item_attrs;
				}
				?>
				<<?php echo esc_html( $tag ); ?> <?php echo wp_kses_post( $final_tag_attrs ); ?>>
					<?php
					if ( has_post_thumbnail() ) {
						the_post_thumbnail( 'large' ); }
					?>
					<?php if ( $show_text_wrap ) : ?>
					<div class="sifency-stack-text-wrap">
						<?php if ( 'yes' === $settings['show_title'] ) : ?>
						<h3 class="stack-title sifency-heading-style"><?php the_title(); ?></h3>
					<?php endif; ?>
						<?php if ( 'yes' === $settings['show_excerpt'] ) : ?>
						<div class="stack-excerpt sifency-content-style">
							<?php echo esc_html( wp_trim_words( get_the_excerpt(), 15, '...' ) ); ?>
						</div>
					<?php endif; ?>
					</div>
				<?php endif; ?>
				</<?php echo esc_html( $tag ); ?>>

				<?php
			}
		endwhile;
		$this->render_sifency_addons_numeric_pagination( $query, $settings, 'query_pagination_' );
		wp_reset_postdata();
	}
}
