<?php
/**
 * Template Name: Archive
 *
 * @package far_loop
 */

get_header();

$sections = [];

if ( empty( $_GET ) ) :

	$sections = array(
		array(
			'section_title' => __( 'Artists videos', 'far_loop' ),
			'query_args' => array(
				'posts_per_page' => 6,
				'post_type' => 'artist-video',
				'meta_query' => array(
					'relation' => 'AND',
					array(
						'relation' => 'OR',
						array(
							'key' => 'video_location',
							'value' => array( 'both', 'loop-only' ),
							'compare' => 'IN'
						),
						array(
							'key' => 'video_location',
							'compare' => 'NOT EXISTS',
						),
					),
				),
			),
		),
		array(
			'section_title' => __( 'Artists', 'far_loop' ),
			'query_args' => array(
				'posts_per_page' => 6,
				'post_type' => 'profile',
				'tax_query' => array(
					array(
						'taxonomy' => 'profile-type',
						'field'    => 'slug',
						'terms'    => 'artist',
					),
				),
			),
		),
		array(
			'section_title' => __( 'Galleries', 'far_loop' ),
			'query_args' => array(
				'posts_per_page' => 6,
				'post_type' => 'gallery',
			),
		),
		array(
			'section_title' => __( 'Speakers', 'far_loop' ),
			'query_args' => array(
				'posts_per_page' => 6,
				'post_type' => 'profile',
				'tax_query' => array(
					array(
						'taxonomy' => 'profile-type',
						'field'    => 'slug',
						'terms'    => 'speaker',
					),
				),
			),
		),
	);

else :

	$paged = get_query_var( 'paged' ) ?: 1;

	$sections = array(
		array(
			'section_title' => '',
			'query_args' => array(
				'paged' => $paged,
				'posts_per_page' => 12,
				'post_type' => 'any',
				'tax_query' => array( 'relation' => 'AND' ),
				'meta_query' => array(
					'relation' => 'AND',
					array(
						'relation' => 'OR',
						array(
							'key' => 'video_location',
							'value' => array( 'both', 'loop-only' ),
							'compare' => 'IN'
						),
						array(
							'key' => 'video_location',
							'compare' => 'NOT EXISTS',
						),
					),
				),
			),
		),
	);

	$query_args = &$sections[0]['query_args'];

	if ( ! empty( $_GET['type'] ) ) {
		$query_args['post_type'] = explode( ' ', sanitize_text_field( $_GET['type'] ) );
	}

	if ( ! empty( $_GET['activity-type'] ) ) {
		$query_args['tax_query'][] = array(
			'taxonomy' => 'activity-type',
			'field'    => 'slug',
			'terms'    => sanitize_text_field( $_GET['activity-type'] ),
		);
	}

	if ( ! empty( $_GET['profile-type'] ) ) {
		$query_args['tax_query'][] = array(
			'taxonomy' => 'profile-type',
			'field'    => 'slug',
			'terms'    => sanitize_text_field( $_GET['profile-type'] ),
		);
	}

	if ( ! empty( $_GET['edition'] ) ) {
		$query_args['tax_query'][] = array(
			'taxonomy' => 'edition',
			'field'    => 'slug',
			'terms'    => sanitize_text_field( $_GET['edition'] ),
		);
	}

	if ( ! empty( $_GET['id'] ) ) {
		$query_args['p'] = absint( $_GET['id'] );
	}

	if ( ! empty( $_GET['search'] ) ) {
		$query_args['s'] = sanitize_text_field( $_GET['search'] );
	}

endif; // empty $_GET
?>

<div class="u_pt-3 u_pt-md-6">
	<div class="l_container l_container--wider u_mv-5">
		<div class="page-title u_mb-3 u_mb-md-4">
			<h1 class="page-title__apex-title u_mb-0p5"><?php the_title(); ?></h1>
		</div>
		<div class="grid u_mh-min1p5">
			<?php get_sidebar( 'archive' ); ?>
			<div class="col--sm--three--fifth col--md--three--quarter u_ph-1p5">
				<?php foreach ( $sections as $key => $section ) :
					$args  = array_merge( array( 'orderby' => 'title', 'order' => 'ASC' ), $section['query_args'] );
					$query = new WP_Query( $args );

					if ( ! empty( $_GET ) ) {
						$section['section_title'] = get_results_string( $query );
					}
					?>
					<div class="archive-result-section">
						<h3 class="section-heading delta"><span class="u_text-underline"><?php echo esc_html( $section['section_title'] ); ?></span></h3>
						<?php if ( $query->have_posts() ) :
							while ( $query->have_posts() ) : $query->the_post();
								get_template_part( 'template-parts/content-archive' );
							endwhile;
						else :
							echo '<h2 class="u_mb-1 alpha u_text-zero-kerning">' . esc_html__( 'Sorry, no results matched your search.', 'far_loop' ) . '</h2>';
							echo '<a href="' . esc_url( get_permalink( get_queried_object_id() ) ) . '" class="button button--text">' . esc_html__( 'Return to archive index', 'far_loop' ) . '</a>';
						endif;

						if ( empty( $_GET ) ) :
							switch ( $key ) {
								case 0: $url = esc_url( add_query_arg( 'type', 'artist-video' ) ); break;
								case 1: $url = esc_url( add_query_arg( 'profile-type', 'artist' ) ); break;
								case 2: $url = esc_url( add_query_arg( 'type', 'gallery' ) ); break;
								case 3: $url = esc_url( add_query_arg( 'profile-type', 'speaker' ) ); break;
								default: $url = '#'; break;
							}
							?>
							<div class="u_mt-min2">
								<a href="<?php echo $url; ?>" class="button">
									<?php printf( esc_html__( 'View all %s', 'far_loop' ), strtolower( $section['section_title'] ) ); ?>
								</a>
							</div>
						<?php else :
							pagination_module( $query );
						endif;

						wp_reset_postdata(); ?>
					</div>
				<?php endforeach; ?>
			</div>
		</div>
	</div>
</div>

<?php get_footer(); ?>
