<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Repeater;
use Elementor\Group_Control_Image_Size;
use Sifency\Traits\Sifency_Addons_Query_Arg_Controls;
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_Animation_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Award_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Section_Header;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;
use Sifency\Classes\Sifency_Addons_Widget_Base;
class sifency_addons_Awards_Widget extends Sifency_Addons_Widget_Base {
	use Sifency_Addons_Animation_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_Award_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Custom_CSS_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-awards',
			'title'          => 'Sifency Awards',
			'icon'           => 'eicon-price-table',
			'category'       => 'sifency-advance-widgets',
			'keywords'       => array( 'awards', 'achievements', 'recognitions', 'honors' ),
			'style_depends'  => array( 'sifency-typo-style', 'sifency-frontend-style', 'sifency-awards-style', 'sifency-icon' ),
			'script_depends' => array( 'sifency-awards-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->add_sifency_award_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'exclude'  => array( 'transition' ),
				'label'    => esc_html__( 'Award Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-award-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'heading_style',
				'label'          => 'Award Title',
				'selector'       => '.sifency-heading-style',
				'show_min_width' => true,
				'show_max_width' => true,
				'condition'      => array( 'show_title' => 'yes' ),
				'exclude'        => array( 'transition', 'bg', 'radius' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'meta_style',
				'label'          => 'Award Date (Meta)',
				'selector'       => '.sifency-meta-style',
				'show_min_width' => true,
				'show_max_width' => true,
				'condition'      => array( 'show_date' => 'yes' ),
				'exclude'        => array( 'transition', 'tabs', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'badge_',
				'label'          => 'Award Badge',
				'selector'       => '.sifency-award-level-badge',
				'show_min_width' => true,
				'show_max_width' => true,
				'exclude'        => array( 'transition', 'tabs', 'alignment' ),
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'content_style',
				'label'          => 'Award Content',
				'selector'       => '.sifency-content-style',
				'color_only'     => true,
				'show_max_width' => true,
				'show_min_width' => true,
				'condition'      => array( 'show_excerpt' => 'yes' ),
				'exclude'        => array( 'box_model', 'transition', 'shadow' ),
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'selector'       => '.sa-award-btn',
				'hover_selector' => '.sifency-award-item:hover .sa-award-btn',
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'label'     => esc_html__( 'Image Style', 'sifency-addons' ),
				'selector'  => '.sifency-award-img-wrapper img, .sifency-award-hover-img',
				'selectors' => array(
					'image'     => '.sifency-award-img-wrapper img, .sifency-award-hover-img',
					'container' => '.sifency-award-img-wrapper img, .sifency-award-hover-img',
				),
				'condition' => array( 'show_image' => 'yes' ),
				'exclude'   => array( 'opacity', 'filters', 'tabs', 'transition' ),
			)
		);
		Sifency_Addons_Parity_Style_Controls::register( $this );
		$this->add_Sifency_Addons_Animation_Controls();
		$this->add_Sifency_Addons_Custom_CSS_Controls();
	}

	protected function render_dom( array $context ) {
		$wrapper_id        = esc_attr( $context['dom_id'] );
		$settings          = $this->get_settings_for_display();
		$animation_manager = new Sifency_Addons_Animation_Manager( $settings );
		$this->sifency_addons_render_custom_css();

		$wrapper_classes = array(
			'sifency-awards-wrapper',
			'sifency-awards--' . $settings['layout_style'],
		);

		if ( 'yes' === $settings['enable_hover_image'] && $settings['layout_style'] !== 'grid' ) {
			$wrapper_classes[] = 'sifency-hover-reveal-active';
		}

		$wrapper_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<div id="<?php echo esc_attr( $wrapper_id ); ?>" <?php echo wp_kses_post( $wrapper_attrs ); ?>>
			<?php
			if ( 'posts' === $settings['source'] ) {
				$this->render_posts_content( $settings, $animation_manager );
			} else {
				$this->render_custom_content( $settings, $animation_manager );
			}
			?>
		</div>
		<?php
	}

	private function render_item_content( $settings, $item, $link_key, $animation_manager, $index ) {
		$oi             = $index + 1;
		$parity_class   = 'odd-item';
		$parity_pattern = $settings['parity_pattern'] ?? 'standard';

		if ( 'every_third' === $parity_pattern ) {
			$parity_class = ( $oi % 3 === 0 ) ? 'even-item' : 'odd-item'; } elseif ( 'custom' === $parity_pattern ) {
			$even_positions = array_map( 'intval', explode( ',', $settings['custom_even_positions'] ?? '' ) );
			$parity_class   = in_array( $oi, $even_positions ) ? 'even-item' : 'odd-item'; } else {
				$parity_class = ( $oi % 2 === 0 ) ? 'even-item' : 'odd-item'; }

			$item_classes = array( 'sifency-award-item', $parity_class );
			$item_attrs   = $animation_manager->get_item_attributes( $item_classes );

			$has_image    = ! empty( $item['image_html'] );
			$hover_effect = ( 'yes' === $settings['enable_hover_image'] && $settings['layout_style'] !== 'grid' );

			?>
		<div <?php echo wp_kses_post( $item_attrs ); ?>>
			<div class="sifency-award-content-wrap">
				<div class="sifency-award-left">
					<?php if ( $settings['show_image'] === 'yes' && $has_image && ! $hover_effect ) : ?>
						<div class="sifency-award-img-wrapper static-img">
							<?php echo wp_kses_post( $item['image_html'] ); ?>
						</div>
					<?php endif; ?>
					
					<div class="sifency-award-text award-content-wrap">
						<?php
						if ( ! empty( $item['year'] ) && 'yes' === $settings['show_date'] ) :
							?>
							<span class="sifency-award-year sifency-meta-style"><?php echo esc_html( $item['year'] ); ?></span> <?php endif; ?>
							
						<?php if ( ! empty( $item['award_level'] ) && 'yes' === $settings['show_award_level'] ) : ?> 
							<span class="sifency-award-level-badge"><?php echo esc_html( $item['award_level'] ); ?></span> 
						<?php endif; ?>
						
						<div class="award-title-and-content">
						<?php if ( ! empty( $item['title'] ) && 'yes' === $settings['show_title'] ) : ?> 
								<?php if ( $settings['layout_style'] === 'list-modern' ) : ?>
									<a <?php echo wp_kses_post( $this->get_render_attribute_string( $link_key ) ); ?> class="sifency-award-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></a>
								<?php else : ?>
									<h3 class="sifency-award-title sifency-heading-style"><?php echo esc_html( $item['title'] ); ?></h3>
								<?php endif; ?>
						<?php endif; ?>
						
						<?php
						if ( ! empty( $item['body'] ) && 'yes' === $settings['show_excerpt'] ) :
							?>
							<div class="sifency-award-body sifency-content-style"><?php echo wp_kses_post( $item['body'] ); ?></div> <?php endif; ?>
						</div>
					</div>
				</div>
				<?php if ( ! empty( $settings['read_more_text'] ) && $settings['layout_style'] === 'list-modern' && 'yes' === $settings['show_button'] ) : ?> 
					<div class="sifency-award-right">
						<div class="sifency-award-item-link">
							<a <?php echo wp_kses_post( $this->get_render_attribute_string( $link_key ) ); ?> class="sa-award-btn">
								<?php echo esc_html( $settings['read_more_text'] ); ?>
									<?php
									$next_icon = ! empty( $settings['arrow_next_icon']['value'] )
										? $settings['arrow_next_icon']
										: array(
											'value'   => 'icon-right-open-big',
											'library' => 'sifency-icons',
										);
									\Elementor\Icons_Manager::render_icon(
										$next_icon,
										array(
											'aria-hidden' => 'true',
										)
									);
									?>

							</a>
						</div>
					</div>
				<?php endif; ?>
			</div>
			<?php if ( ! empty( $settings['read_more_text'] ) && $settings['layout_style'] !== 'list-modern' && 'yes' === $settings['show_button'] ) : ?> 
				<div class="sifency-award-item-link">
					<a <?php echo wp_kses_post( $this->get_render_attribute_string( $link_key ) ); ?> class="sa-award-btn">
						<?php echo esc_html( $settings['read_more_text'] ); ?>
						<?php
							$next_icon = ! empty( $settings['arrow_next_icon']['value'] ) ? $settings['arrow_next_icon'] : '';
							\Elementor\Icons_Manager::render_icon(
								$next_icon,
								array(
									'aria-hidden' => 'true',
								)
							);
						?>
					</a>
				</div>
			<?php endif; ?>
			<?php if ( $settings['show_image'] === 'yes' && $has_image && $hover_effect ) : ?>
				<div class="sifency-award-hover-img">
					<?php echo wp_kses_post( $item['image_html'] ); ?>
				</div>
			<?php endif; ?>

		</div>
		<?php
	}

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

		foreach ( $settings['awards_list'] as $index => $item ) {
			$link_key = 'link_' . $index;
			$this->remove_render_attribute( $link_key );
			if ( ! empty( $item['link']['url'] ) ) {
				$this->add_link_attributes( $link_key, $item['link'] );
			} else {
				$this->add_render_attribute( $link_key, 'href', '#' );
			}

			$image_html = '';
			if ( ! empty( $item['award_image']['id'] ) ) {
				$image_html = Group_Control_Image_Size::get_attachment_image_html( $item, 'award_image' );
			}

			$data = array(
				'year'        => $item['award_year'],
				'title'       => $item['award_title'],
				'body'        => $item['awarding_body'],
				'award_level' => $item['award_level'],
				'image_html'  => $image_html,
			);

			$this->render_item_content( $settings, $data, $link_key, $animation_manager, $index );
		}
	}

	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'] ?? 'award';
		$query                   = new \WP_Query( $query_args );

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

		$index = 0;
		while ( $query->have_posts() ) :
			$query->the_post();

			$link_key = 'post_link_' . $index;
			$this->add_render_attribute( $link_key, 'href', sifency_get_field( 'project_url' ) );

			$image_html = '';
			if ( has_post_thumbnail() ) {
				$image_html = get_the_post_thumbnail( get_the_ID() );
			}

			$data = array(
				'year'          => sifency_get_field( 'date_awarded' ),
				'awarding_body' => sifency_get_field( 'awarding_body' ),
				'award_level'   => sifency_get_field( 'award_level' ),
				'title'         => get_the_title(),
				'body'          => get_the_excerpt(),
				'image_html'    => $image_html,
			);

			$this->render_item_content( $settings, $data, $link_key, $animation_manager, $index );
			++$index;
		endwhile;
		$this->render_sifency_addons_numeric_pagination( $query, $settings, 'query_pagination_' );
		wp_reset_postdata();
	}
}
