<?php
/**
 * Front Page
 *
 * @package far_loop
 */

get_header();

$fields = array_fill_keys( array(
	'dates',
	'subtitle',
	'hero_style',
	'featured_video',
	'custom_video',
	'custom_video_caption',
	'linked_image',
	'linked_image_link',
	'linked_image_caption',
	'sections',
), null );

foreach ( $fields as $field => $value ) {
	$fields[ $field ] = get_field( $field );
}

switch ( $fields['hero_style'] ) {
	case 'vimeo-id':
		$vimeo_id = $fields['custom_video'];
		$caption = $fields['custom_video_caption'];
		break;
	case 'linked-image':
		$linked_image = $fields['linked_image'];
		$linked_image_src = ( 'gif' == pathinfo( $linked_image['url'], PATHINFO_EXTENSION ) ) ? $linked_image['sizes']['large'] : $linked_image['url'];
		$caption = sprintf( '<a href="%1$s">%2$s</a>', $fields['linked_image_link'], $fields['linked_image_caption'] );
		break;
	case 'artist-video':
	default:
		$vimeo_id = get_field( 'vimeo_id', $fields['featured_video'] );
		$authors = get_field( 'author', $fields['featured_video'] );
		$caption = sprintf(
			'<a href="%1$s" class="home-hero__highlight-caption-link"><em>%2$s</em>%3$s</a>',
			get_permalink( $fields['featured_video'] ),
			$fields['featured_video']->post_title,
			get_authors_string( $authors )
		);
		break;
}

?>

<div class="u_pt-3 u_pt-md-6">
	<div class="home-hero">
		<div class="grid home-hero__grid">
			<div class="col--tb--four--ninth col--push--tb--five--ninth home-hero__column">
				<div class="home-hero__site-title-container">
					<h1 class="home-hero__site-title">Loop<br>Barcelona</h1>
					<table class="home-hero__dates">
						<tbody>
							<?php 
							$dates_fair = get_field('date_fair'); 
							if (!empty($dates_fair)): ?>
							<tr class="-fair">
								<th><span>Fair</span></th>
								<td><?php echo esc_html($dates_fair); ?></td>
							</tr>
							<?php endif; ?>

							<?php 
							$dates_festival = get_field('date_festival'); 
							if (!empty($dates_festival)): ?>
							<tr>
								<th><span>Festival</span></th>
								<td><?php echo esc_html($dates_festival); ?></td>
							</tr>
							<?php endif; ?>

							<?php 
							$dates_symposium = get_field('dates_symposium'); 
							if (!empty($dates_symposium)): ?>
							<tr>
								<th><span>Symposium</span></th>
								<td><?php echo esc_html($dates_symposium); ?></td>
							</tr>
							<?php endif; ?>
						</tbody>

					</table>
					<div class="home-hero__site-title-data">
						<h2 class="home-hero__baseline"><?php echo nl2br( esc_html( $fields['subtitle'] ) ); ?></h2>
					</div>
				</div>
			</div>
			<div class="col--tb--five--ninth col--pull--tb--four--ninth home-hero__column">
				<div class="home-hero__highlight-container">
					<div class="home-hero__highlight-video-container">
						<?php

						if ( ! empty( $vimeo_id ) ) {

							video_player_module( $vimeo_id, true );

						} elseif ( ! empty( $linked_image ) ) {

							printf(
								'<a href="%1$s"><img src="%2$s" alt="%3$s"/></a>',
								esc_url( $fields['linked_image_link'] ),
								esc_url( $linked_image_src ),
								esc_html( $fields['subtitle'] )
							);

						}
						?>
					</div>
					<div class="home-hero__highlight-caption"><?php echo $caption?></div>
				</div>
			</div>
		</div>
	</div>
	<div class="l_container u_pt-6">

		<?php get_template_part( 'template-parts/front-page-featured' ); ?>

		<?php $page_for_posts = get_option( 'page_for_posts' ); ?>

		<div class="u_mt-1 u_text-right">
			<a href="<?php echo esc_url( get_permalink( $page_for_posts ) ); ?>" class="home-pillar__cta">
				<span class="home-pillar__cta-text"><?php _e('View all news', 'far_loop');?></span>
			</a>
		</div>

		<!-- About sections-->
		<div class="u_mt-7">
			<!--<h3 class="section-heading delta"><span class="u_text-underline">About LOOP Barcelona</span></h3>-->

			<?php
			if ( ! empty( $fields['sections'] ) ) :
				foreach ( $fields['sections'] as $section ) :
				?>
			

			
			
				<!-- section-heading module-->

				<div class="home-pillar">
					<div class="grid u_mh-min1">
						<div class="col--sm--one--half col--vab u_ph-1">
							<h2 class="home-pillar__heading"><?php echo $section['section_title']; ?></h2>
							<figure class="home-pillar__image"><img src="<?php echo esc_url( $section['image_1']['url'] ); ?>" alt="<?php echo esc_html( $section['image_1']['caption'] ); ?>">
								<figcaption class="home-pillar__image-caption"><?php echo esc_html( $section['image_1']['caption'] ); ?></figcaption>
							</figure>
						</div>
						<div class="col--sm--one--half col--vab u_ph-1">
							<figure class="home-pillar__image"><img src="<?php echo esc_url( $section['image_2']['url'] ); ?>" alt="<?php echo esc_html( $section['image_2']['caption'] ); ?>">
								<figcaption class="home-pillar__image-caption"><?php echo esc_html( $section['image_2']['caption'] ); ?></figcaption>
							</figure>
							<p class="home-pillar__description"><?php echo $section['description']; ?></p>
						</div>
					</div>
					<div class="u_mt-2 u_text-right"><a href="<?php echo esc_url( get_permalink( $section['link_url'] ) ); ?>" class="home-pillar__cta"><span class="home-pillar__cta-text"><?php echo esc_html( $section['link_text'] ); ?></span></a></div>
				</div>

				<?php
				endforeach;
			endif;
			?>
		</div>
	</div>
</div>

<?php

get_footer();