<?php
/**
 * Event slider shortcode.
 *
 * @package EventimUSEventListings
 */

defined( 'ABSPATH' ) || exit;

global $evuselConfig;
add_shortcode( $evuselConfig::SHORTCODE_SLIDER, 'evusel_slider_shortcode' );
/**
 * Render event slider shortcode.
 *
 * @return string Shortcode output.
 */
function evusel_slider_shortcode() {

	global $evuselConfig;

	$hideRepeating = $evuselConfig->get( 'show_repeating_events' ) === false;

	$args = array(
		'numberposts'   => true,
		'post_type'     => 'seetickets-event',
		'post_status'   => array( 'publish' ),
		'meta_query'    => array( 'compare' => 'AND' ),
		'orderby'       => 'meta_value',
		'order'         => 'ASC',
		'meta_key'      => 'event_time',
		'meta_type'     => 'datetime',
		'no_found_rows' => true,
		'post_parent'   => '',
	);

	evusel_add_passed_meta_criteria( $args );

	$allEvents = get_posts( $args );

	// Filter multi-day events to show only parent events.
	if ( ! $hideRepeating ) {
		$allEvents = evusel_filter_multi_day_events_results( $allEvents );
	}

	$amountSelectedEventsSlider = 0;
	$sliderSelectedEvents       = array();
	foreach ( $allEvents as $event ) {

		$includeEventInSlider = get_post_meta( $event->ID, 'include_in_event_slider', true );

		if ( isset( $includeEventInSlider[0] ) && $includeEventInSlider[0] == 'Show this field on event cards' ) {
			++$amountSelectedEventsSlider;
			array_push( $sliderSelectedEvents, $event );
		}
	}

	if ( $amountSelectedEventsSlider == 0 ) {
		$args = array(
			'numberposts'   => $evuselConfig->get( 'shortcodes.slider.no_of_slides' ),
			'post_type'     => 'seetickets-event',
			'post_status'   => array( 'publish' ),
			'meta_query'    => array( 'compare' => 'AND' ),
			'orderby'       => 'meta_value',
			'order'         => 'ASC',
			'meta_key'      => 'event_time',
			'meta_type'     => 'datetime',
			'no_found_rows' => true,
		);

		$args['post_parent'] = 0;

		evusel_add_passed_meta_criteria( $args );

		$sliderSelectedEvents = get_posts( $args );

		// Filter multi-day events to show only parent events..
		if ( ! $hideRepeating ) {
			$sliderSelectedEvents = evusel_filter_multi_day_events_results( $sliderSelectedEvents );
		}
	}

	$string  = '<div class="see-slider swiper mySwiper">';
	$string .= '<div class="swiper-wrapper">';

	$navType = $evuselConfig->get( 'shortcodes.slider.navigation' );
	$layout  = $evuselConfig->get( 'shortcodes.slider.layout' );

	foreach ( $sliderSelectedEvents as $event ) {

		// if ($hideRepeating && has_post_parent($event)) {

		// $parent = get_post_parent($event);
		// $repeatingDates = $parent->repeating_dates;
		// $lastRepeatingDate = end($repeatingDates);
		// $sortDateTime = DateTime::createFromFormat('Ymd', $event->sort_date);

		// if ($lastRepeatingDate != $sortDateTime->format('m/d/Y')) {
		// continue; // Skip all repeating event,s except for the last one.
		// }
		// }

		$eventDateTime = evusel_get_event_display_date_time( $event->ID );

		$eventTitle = get_post_meta( $event->ID, 'event_title', true );

		$btnText          = evusel_get_ticket_status_button_text( $event );
		$ariaLabel        = evusel_get_ticket_button_aria_label( false, $eventTitle, $eventDateTime );
		$ticketButtonLink = evusel_get_ticket_link( $event, true );

		$eventImage = evusel_get_event_image( $event->ID );

		$classes               = evusel_get_ticket_status_button_classes( $event );
		$openEventPageToNewTab = $evuselConfig->get( 'open_event_page_to_new_tab' );
		$iamgeClasses          = $evuselConfig->get( 'shortcodes.slider.fullwidth' ) === 'on' ? 'fullwidth' : '';
		$overlayClass          = $layout === 'image_only' ? '' : 'overlay';

		$seeticketEventButtonTarget = '';
		if ( $openEventPageToNewTab == 'on' ) {
			$seeticketEventButtonTarget = "target='_blank'";
		}

		$seetiketEventButton = "<a href='" . esc_url( $ticketButtonLink ) . "' " . esc_attr( $seeticketEventButtonTarget ) . " aria-label='" . esc_attr( $ariaLabel ) . "' class='seetickets-buy-btn fs-16 " . esc_attr( $classes ) . "'>" . esc_html( $btnText ) . '</a>';

		$string .= '<div class="swiper-slide position-relative">';
		$string .= '<div class="' . esc_attr( $overlayClass ) . ' position-absolute"></div>';

		$string .= '<div class="slider-img-container position-absolute">';
		$string .= '<img src="' . esc_url( $eventImage ) . '" class="' . esc_attr( $iamgeClasses ) . '">';
		$string .= '</div>';

		if ( $layout === 'full_width' ) {
			$string .= '<div class="display-flex slider-content align-end justify-center text-center">';
			$string .= '<div class="mb-35 z-index-1">';
			$string .= '<p class="mb-0 fs-48 font-medium slider-event-title">' . evusel_anchorize( $eventTitle, $event ) . '</p>';
			$string .= ! empty( $eventDateTime ) ? '<p class="fs-20">' . $eventDateTime->format( 'D M j' ) . '</p>' : '';
			$string .= '<div>';
			$string .= $seetiketEventButton;
			$string .= '</div>';
			$string .= '</div>';
		}

		if ( $layout === 'image_only' ) {
			$string .= '<div class="display-flex slider-content align-end justify-center text-center">';
			$string .= '<div class="mb-35 z-index-1">';
			$string .= '<p class="mb-0 fs-48 font-medium slider-event-title"></p>';
			$string .= '<div>';
			$string .= '</div>';
			$string .= '</div>';
		}

		$string .= '</div>';
		$string .= '</div>';
	}

	$string .= '</div>';
	if ( $navType === 'circles' ) {
		$string .= '<div class="swiper-pagination"></div>';
	}
	if ( $navType === 'arrows' ) {
		$string .= ' <div class="swiper-button-next"></div><div class="swiper-button-prev"></div>';
	}
	$string .= '</div>';

	return $string;
}
