<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Image_Size;
use Elementor\Repeater;
use Elementor\Icons_Manager;
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_Master_Typography;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Animation_Manager;
use Sifency\Traits\Sifency_Event_Controls;
use Sifency\Traits\Sifency_Addons_Image_Style_Controls;
use Sifency\Traits\Sifency_Addons_Icon_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_Widget_Event 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_Event_Controls;
	use Sifency_Addons_Image_Style_Controls;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Section_Header;
	use Sifency_Addons_Help_Controls;
	use Sifency_Addons_Custom_CSS_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency-addons-event',
			'title'          => 'Sifency Event',
			'icon'           => 'eicon-calendar',
			'category'       => 'sifency-advance-widgets',
			'keywords'       => array( 'event', 'calendar', 'date', 'schedule' ),
			'style_depends'  => array( 'sifency-typo-style', 'sifency-frontend-style', 'sifency-icon', 'sifency-events-style' ),
			'script_depends' => array( 'sifency-frontend-script' ),
		);}
	protected function register_controls() {
		$this->add_sifency_addons_section_header_controls();
		$this->add_sifency_event_controls();
		$this->add_sifency_addons_help_controls();
		$this->add_sifency_addons_item_style_controls(
			array(
				'label'    => esc_html__( 'Event Item Wrapper', 'sifency-addons' ),
				'selector' => '.sifency-event-item',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'heading_style_',
				'label'          => 'Event Heading',
				'selector'       => '.sifency-heading-style',
				'exclude'        => array( 'bg', 'radius' ),
				'show_min_width' => true,
				'show_max_width' => true,
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'content_style_',
				'label'          => 'Event Content',
				'selector'       => '.sifency-content-style',
				'exclude'        => array( 'transition', 'tabs', 'shadow', 'stroke', 'bg', 'radius' ),
				'color_only'     => true,
				'show_max_width' => true,
				'show_min_width' => true,
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'         => 'meta_style_',
				'label'          => 'Event Meta',
				'selector'       => '.sifency-meta-style',
				'hover_selector' => 'sifency-event-item:hover .sifency-meta-style',
				'exclude'        => array( 'transition', 'alignment', 'shadow', 'stroke', 'tabs' ),
				'color_only'     => true,
				'show_min_width' => true,
				'show_max_width' => true,
			)
		);
		$this->add_Sifency_Addons_Button_Style_Controls(
			array(
				'section_label'  => esc_html__( 'Button Style', 'sifency-addons' ),
				'selector'       => '.sifency-event-item__button',
				'hover_selector' => '.sifency-event-item:hover .sifency-event-item__button',
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'selector' => '.sifency-meta-style i, .sifency-meta-style svg',
				'label'    => 'Meta Icon Style',
			)
		);
		$this->add_sifency_addons_image_style_controls(
			array(
				'selectors' => array(
					'image'      => '.sifency-event-img img',
					'container'  => '.sifency-event-img img',
					'dimensions' => '.sifency-event-img img',
				),
			)
		);
		$this->add_Sifency_Addons_Animation_Controls();
		Sifency_Addons_Parity_Style_Controls::register(
			$this,
			array(
				'selectors' => array(
					'odd_item'  => '{{WRAPPER}} .odd-item',
					'even_item' => '{{WRAPPER}} .even-item',
					'title'     => '.sifency-event-item__title',
				),
			)
		);
		$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-event-wrapper',
			'sifency-event-layout-' . $settings['layout_style'],
		);

		$wrapper_attrs = $animation_manager->get_wrapper_attributes( $wrapper_classes );
		?>
		<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_single_item( $settings, $data, $link_key, $animation_manager, $index ) {
		$oi           = $index + 1;
		$parity_class = ( $oi % 2 !== 0 ) ? 'odd-item' : 'even-item';

		$tag      = 'div';
		$link_url = $data['link']['url'] ?? '';

		if ( ! empty( $link_url ) ) {
			$this->add_link_attributes( $link_key, $data['link'] );
			if ( 'yes' !== $settings['show_button'] ) {
				$tag = 'a';
			}
		}

		$item_classes = array( 'sifency-event-item', $parity_class );
		$item_attrs   = $animation_manager->get_item_attributes( $item_classes );
		?>
		<?php $this->render_sifency_section_header_elements( $settings ); ?>
		<<?php echo esc_html( $tag ); ?> <?php echo wp_kses_post( $this->get_render_attribute_string( $link_key ) . ' ' . $item_attrs ); ?>>
			
			<?php if ( 'yes' === $settings['show_image'] && ! empty( $data['image_html'] ) ) : ?>
				<div class="sifency-event-img">
					<?php
					if ( ! empty( $link_url ) && 'div' === $tag ) :
						?>
						<a <?php echo wp_kses_post( $this->get_render_attribute_string( $link_key ) ); ?>><?php endif; ?>
					<?php echo wp_kses_post( $data['image_html'] ); ?>
					<?php
					if ( ! empty( $link_url ) && 'div' === $tag ) :
						?>
						</a><?php endif; ?>
					
					<?php if ( $settings['layout_style'] === 'style-5' ) : ?>
						<div class="sifency-overlay-gradient"></div>
					<?php endif; ?>
				</div>
			<?php endif; ?>

			<div class="sifency-event-content-wrap">
				
				<div class="sifency-event-info">
					<h3 class="sifency-event-item__title sifency-heading-style">
						<?php
						if ( ! empty( $link_url ) && 'div' === $tag ) :
							?>
							<a <?php echo wp_kses_post( $this->get_render_attribute_string( $link_key ) ); ?>><?php endif; ?>
						<?php echo wp_kses_post( $data['title'] ); ?>
						<?php
						if ( ! empty( $link_url ) && 'div' === $tag ) :
							?>
							</a><?php endif; ?>
					</h3>
					
					<?php
					if (
						( $data['date'] && 'yes' === $settings['show_date'] ) ||
						( $data['location'] && 'yes' === $settings['show_location'] )
					) :
						?>
						
						<div class="sifency-event-item__meta">
							
							<?php if ( $data['date'] && 'yes' === $settings['show_date'] ) : ?>
								<span class="sifency-event-item__date sifency-meta-style">
									<?php Icons_Manager::render_icon( $settings['date_icon'], array( 'aria-hidden' => 'true' ) ); ?>
									<?php echo wp_kses_post( $data['date'] ); ?>
								</span>
							<?php endif; ?>
							
							<?php if ( $data['location'] && 'yes' === $settings['show_location'] ) : ?>
								<span class="sifency-event-item__location sifency-meta-style">
									<?php Icons_Manager::render_icon( $settings['location_icon'], array( 'aria-hidden' => 'true' ) ); ?>
									<?php echo wp_kses_post( $data['location'] ); ?>
								</span>
							<?php endif; ?>
							
						</div>

					<?php endif; ?>

					<?php if ( $data['excerpt'] && 'yes' === $settings['show_excerpt'] ) : ?>
						<div class="sifency-event-description sifency-content-style">
							<?php echo wp_kses_post( $data['excerpt'] ); ?>
						</div>
					<?php endif; ?>
				</div>

				<?php if ( 'yes' === $settings['show_button'] ) : ?>
				<div class="sifency-event-item__button-wrap">
					<?php
					$button_text = ! empty( $data['read_more_text'] ) ? $data['read_more_text'] : ( ! empty( $settings['read_more_text'] ) ? $settings['read_more_text'] : esc_html__( 'Read More', 'sifency-addons' ) );
					?>
					<<?php echo tag_escape( ! empty( $link_url ) && 'div' === $tag ? 'a' : 'span' ); ?> <?php echo ! empty( $link_url ) && 'div' === $tag ? wp_kses_post( $this->get_render_attribute_string( $link_key ) ) : ''; ?> class="sifency-event-item__button sifency-button-style">
						<?php echo esc_html( $button_text ); ?>
						<?php Icons_Manager::render_icon( $settings['button_icon'], array( 'aria-hidden' => 'true' ) ); ?>
					</<?php echo tag_escape( ! empty( $link_url ) && 'div' === $tag ? 'a' : 'span' ); ?>>
				</div>
				<?php endif; ?>
				
			</div>
		</<?php echo esc_html( $tag ); ?>>
		<?php
	}

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

		foreach ( $settings['event_list'] as $index => $item ) {
			$link_key = 'link_' . $index;
			$this->remove_render_attribute( $link_key );

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

			$data = array(
				'title'          => $item['title'],
				'date'           => $item['date'],
				'location'       => $item['location'],
				'excerpt'        => $item['excerpt'],
				'link'           => $item['link'],
				'read_more_text' => $item['read_more_text'] ?? '',
				'image_html'     => $image_html,
			);

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

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

		$index = 0;
		while ( $query->have_posts() ) :
			$query->the_post();
			$meta = get_post_meta( get_the_ID(), 'event_single_metabox', true ) ?: array();

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

			$data = array(
				'title'      => get_the_title(),
				'date'       => $meta['event_start_date'] ?? get_the_date(),
				'location'   => $event_location = sifency_get_field( 'event_location' ),
				'excerpt'    => get_the_excerpt(),
				'link'       => array(
					'url'         => get_permalink(),
					'is_external' => false,
				),
				'image_html' => $image_html,
			);

			$link_key = 'post_link_' . $index;
			$this->remove_render_attribute( $link_key );

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