<?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_Clients_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_Clients 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_Clients_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-clients',
			'title'          => 'Sifency Clients',
			'icon'           => 'eicon-gallery-grid',
			'category'       => 'sifency-advance-widgets',
			'keywords'       => array( 'clients', 'logo', 'companies', 'partners' ),
			'style_depends'  => array( 'sifency-typo-style', 'swiper', 'sifency-frontend-style', 'sifency-clients-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_clients_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__( 'Client Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-client-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'condition'      => array( 'show_title' => 'yes' ),
				'selector'       => '.sifency-heading-style',
				'hover_selector' => 'sifency-client-item:hover .sifency-heading-style',
				'exclude'        => array( 'bg', 'radius', 'shadow' ),
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selector'  => '.sifency-client-item img',
				'selectors' => array(
					'image'      => '.sifency-client-item img',
					'container'  => '.sifency-client-item img',
					'dimensions' => '.sifency-client-item img',
				),
			)
		);
		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'] );

		$this->add_render_attribute( 'wrapper', 'class', 'sifency-clients-wrapper' );

		$animation_attrs = $animation_manager->get_wrapper_attributes();
		if ( ! empty( $animation_attrs ) ) {
			preg_match( '/class="([^"]+)"/', $animation_attrs, $class_matches );
			if ( isset( $class_matches[1] ) ) {
				$this->add_render_attribute( 'wrapper', 'class', $class_matches[1] );
			}
		}
		if ( $is_carousel ) {
			$this->add_render_attribute( 'wrapper', 'class', array( 'sifency-carousel-wrapper', 'swiper', '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'] ?? 'client' ) ) ) )->post_count
				: count( $settings['client_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 {
			$this->add_render_attribute( 'wrapper', 'class', 'sifency-clients--grid' );
		}

		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div <?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['client_list'] ) ) {
			return; }

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

		foreach ( $settings['client_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-client-item' );
			if ( ( $index + 1 ) % 2 !== 0 ) {
				$base_item_classes[] = 'odd-item';
			} else {
				$base_item_classes[] = 'even-item';
			}

			if ( $is_carousel ) {
				$item_id = 'client_item_' . $index;
				$this->add_render_attribute( $item_id, $item_attrs, true );
				$slide_attributes = $animation_manager->get_item_attributes( array_merge( array( 'swiper-slide' ), $base_item_classes ) );
				?>
				<<?php echo esc_html( $tag ); ?> <?php echo wp_kses_post( $slide_attributes ); ?> <?php echo wp_kses_post( $this->get_render_attribute_string( $item_id ) ); ?>>
					<?php if ( ! empty( $item['client_logo']['url'] ) ) : ?>
						<img src="<?php echo esc_url( $item['client_logo']['url'] ); ?>" alt="<?php echo esc_attr( $item['client_name'] ); ?>" />
					<?php endif; ?>
				</<?php echo esc_html( $tag ); ?>>
				<?php
			} 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['client_logo']['url'] ) ) : ?>
						<img src="<?php echo esc_url( $item['client_logo']['url'] ); ?>" alt="<?php echo esc_attr( $item['client_name'] ); ?>" />
					<?php endif; ?>
					
					<?php if ( 'yes' === $settings['show_title'] && ! empty( $item['client_name'] ) ) : ?>
						<h3 class="client-title sifency-heading-style"><?php echo esc_html( $item['client_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'] ?? 'client';
		$query                   = new \WP_Query( $query_args );

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

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

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

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

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

			if ( $is_carousel ) {
				$item_id = 'post_item_' . $query->current_post;
				$this->add_render_attribute( $item_id, $item_attrs, true );
				$slide_attributes = $animation_manager->get_item_attributes( array_merge( array( 'swiper-slide' ), $base_item_classes ) );
				?>
				<<?php echo esc_html( $tag ); ?> <?php echo wp_kses_post( $slide_attributes ); ?> <?php echo wp_kses_post( $this->get_render_attribute_string( $item_id ) ); ?>>
					<?php
					if ( has_post_thumbnail() ) {
						the_post_thumbnail( 'large' ); }
					?>
				</<?php echo esc_html( $tag ); ?>>
				<?php
			} 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 ( 'yes' === $settings['show_title'] ) : ?>
						<h3 class="client-title"><?php the_title(); ?></h3>
					<?php endif; ?>
				</<?php echo esc_html( $tag ); ?>>

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