<?php
/**
 * The template used for displaying promotion headline
 *
 * @package Signify
 */

$signify_type = get_theme_mod( 'signify_promo_head_type', 'page' );

if ( 'page' === $signify_type && $signify_id = get_theme_mod( 'signify_promotion_headline' ) ) {
	$signify_args['page_id'] = absint( $signify_id );
} elseif ( 'post' === $signify_type && $signify_id = get_theme_mod( 'signify_promo_head_post' ) ) {
	$signify_args['p'] = absint( $signify_id );
} elseif ( 'product' === $signify_type && $signify_id = get_theme_mod( 'signify_promo_head_product' ) ) {
	$signify_args['post_type'] = 'product';
	$signify_args['p'] = absint( $signify_id );
} elseif ( 'category' === $signify_type && $signify_cat = get_theme_mod( 'signify_promo_head_category' ) ) {
	$signify_args['cat']            = absint( $signify_cat );
	$signify_args['posts_per_page'] = 1;
}

// If $signify_args is empty return false
if ( empty( $signify_args ) ) {
	return;
}

// Create a new WP_Query using the argument previously created
$promotion_headline_query = new WP_Query( $signify_args );
if ( $promotion_headline_query->have_posts() ) :
	while ( $promotion_headline_query->have_posts() ) :
		$promotion_headline_query->the_post();

		$signify_classes[] = 'section';
		$signify_classes[] = 'promotion-section';
		$signify_classes[] = get_theme_mod( 'signify_promo_head_image_position', 'content-align-left' );
		$signify_classes[] = get_theme_mod( 'signify_promo_head_text_alignment', 'text-align-center' );

		$signify_sub_title   = get_theme_mod( 'signify_promotion_headline_sub_title' );
		$signify_title       = get_theme_mod( 'signify_promotion_headline_title' );
		$signify_description = get_theme_mod( 'signify_promotion_headline_description' );

		if ( get_theme_mod( 'signify_promo_head_text_color', 0 ) ) {
			$signify_classes[] = 'content-color-white';
		}

		if( get_theme_mod( 'signify_promo_head_content_frame', 1 ) ) {
			$signify_classes[] = 'content-frame';
		}

		$signify_classes[] = 'promotion-headline-one';

		$signify_link = get_theme_mod( 'signify_featured_video_link' );

		if( $signify_link ) {
			$signify_classes[] = 'play-video';
		}
		?>

		<div id="promotion-section" class="<?php echo esc_attr( implode( ' ', $signify_classes ) ); ?>">
			<div class="wrapper">
				<div class="section-content-wrapper">
					<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
						<div class="hentry-inner">
							<?php signify_post_thumbnail( 'signify-promotion-headline', 'html-with-bg' ); // signify_post_thumbnail( $image_size, $signify_type = 'html', $echo = true, $no_thumb = false ). ?>

							<div class="content-wrapper">
								<div class="entry-container">
									<div class="entry-container-frame">
										<?php
										$signify_sub_title   = get_theme_mod( 'signify_promotion_headline_sub_title' );
										$signify_title       = get_theme_mod( 'signify_display_promotion_headline_title', 1 );
										$signify_description = get_theme_mod( 'signify_promotion_headline_description' );

										 if ( $signify_title || $signify_sub_title || $signify_link ) : ?>
											<header class="entry-header section-title-wrapper">
												<?php if ( $signify_sub_title ) : ?>
													<div class="section-subtitle">
													<?php
														echo esc_html( $signify_sub_title );
													?>
													</div><!-- .section-description-wrapper -->
												<?php endif; ?>

												<?php if ( $signify_title ) : ?>
													<?php the_title( '<h2 class="section-title">', '</h2>' ); ?>
												<?php endif; ?>

												<?php if ( $signify_description ) : ?>
													<div class="section-description">
														<p><?php echo wp_kses_post( $signify_description ); ?></p>
													</div><!-- .section-description-wrapper -->
												<?php endif; ?>

												<?php $image = get_theme_mod( 'signify_promo_head_logo_image' );
												if ( $image ) : ?>
													<div class="promo-logo">
														<img src="<?php echo esc_url( $image ); ?>">
													</div><!-- .post-thumbnail-->
												<?php endif; ?>

												<?php
													$embed_code = wp_oembed_get( esc_url( $signify_link ) );

													if ( $embed_code ) {
														echo '<div class="play-video"><a class="mixed button play-button" data-flashy-type="video" href="' . esc_url( $signify_link ) . '"> <span class="screen-reader-text">Play Video </span></a>
															</div>';
													}
												?>
										<?php endif; ?>
											</header><!-- .entry-header -->

										<?php
										$show_content = get_theme_mod( 'signify_promo_head_show', 'excerpt' );

										if ( 'full-content' === $show_content ) { ?>
											<div class="entry-content">
												<?php
												the_content();

												wp_link_pages( array(
													'before'      => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'signify-pro' ) . '</span>',
													'after'       => '</div>',
													'link_before' => '<span class="page-number">',
													'link_after'  => '</span>',
													'pagelink'    => '<span class="screen-reader-text">' . __( 'Page', 'signify-pro' ) . ' </span>%',
													'separator'   => '<span class="screen-reader-text">, </span>',
												) );
												?>
											</div><!-- .entry-content -->
										<?php
										}
										elseif ( 'excerpt' === $show_content ) { ?>
											<div class="entry-summary">
												<?php the_excerpt(); ?>
											</div><!-- .entry-summary -->
										<?php
										} ?>

									</div><!-- .entry-container-frame -->
								</div><!-- .entry-container -->
							</div><!-- .content-wrapper -->
						</div><!-- .hentry-inner -->
					</article><!-- #post-## -->
				</div><!-- .section-content-wrapper -->
			</div><!-- .wrapper -->
		</div><!-- .section -->
	<?php
	endwhile;
	wp_reset_postdata();
endif;
