<?php
/**
 * The template for displaying featured content
 *
 * @package Catch_Store
 */

$enable_content = get_theme_mod( 'signify_collection_option', 'disabled' );
$classes = array();

if ( ! signify_check_section( $enable_content ) ) {
	// Bail if featured content is disabled.
	return;
}

$type = get_theme_mod( 'signify_collection_type', 'category' );

if ( 'custom' !== $type ) {
	$type     = get_theme_mod( 'signify_collection_type', 'category' );
	$number   = get_theme_mod( 'signify_collection_number', 6 );
	
	$post_list  = array();
	$no_of_post = 0;

	$args = array(
		'post_type'           => 'post',
		'ignore_sticky_posts' => 1, // ignore sticky posts.
	);

	// Get valid number of posts.
	if ( 'post' === $type || 'page' === $type || 'product' === $type ) {
		$args['post_type'] = $type;

		for ( $i = 1; $i <= $number; $i++ ) {
			$post_id = '';

			if ( 'post' === $type ) {
				$post_id = get_theme_mod( 'signify_collection_post_' . $i );
			} elseif ( 'page' === $type ) {
				$post_id = get_theme_mod( 'signify_collection_page_' . $i );
			} elseif ( 'product' === $type ) {
				$post_id = get_theme_mod( 'signify_collection_product_' . $i );
			}

			if ( $post_id && '' !== $post_id ) {
				$post_list = array_merge( $post_list, array( $post_id ) );

				$no_of_post++;
			}
		}

		$args['post__in'] = $post_list;
		$args['orderby']  = 'post__in';
	} elseif ( 'category' === $type ) {
		if ( $cat = get_theme_mod( 'signify_collection_select_category' ) ) {
			$args['category__in'] = $cat;
		}

		$no_of_post = $number;
	}

	$args['posts_per_page'] = $no_of_post;

	if( ! $no_of_post ) {
		return;
	}

	$collection_posts = get_posts( $args );

	if ( empty( $collection_posts ) ) {
		return;
	}
}

$signify_sub_title   = get_theme_mod( 'signify_collection_sub_title' );
$signify_title       = get_theme_mod( 'signify_collection_title' );
$signify_description = get_theme_mod( 'signify_collection_description' );

$layout = get_theme_mod( 'signify_collection_layout', 'layout-three' );

if ( 'product' === $type ) {
	$classes[] = 'woocommerce';
}

$classes[] = 'section';

if ( ! $signify_sub_title && ! $signify_title && ! $signify_description ) {
	$classes[] = 'no-section-heading';
}
?>

<div id="collection-section" class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
	<div class="wrapper">
		<?php signify_section_header( $signify_sub_title, $signify_title, $signify_description ); ?>

		<div class="section-content-wrapper collection-wrapper <?php echo esc_attr( $layout ); ?>">

			<?php
			if ( 'custom' === $type ) {
				get_template_part( 'template-parts/collection/custom-collection' );
			} else {
				$i =1;
				foreach ( $collection_posts as $post ) {
					setup_postdata( $post );

					$show_content = get_theme_mod( 'signify_collection_show', 'hide-content' );
					$show_meta    = get_theme_mod( 'signify_collection_meta_show', 'show-meta' );

					$post_class = 'hentry';

					$thumbnail = 'signify-portfolio';

					if ( 'layout-one' == $layout ) {
						$thumbnail = 'signify-slider';
					} elseif ( 'layout-two' == $layout ) {
						if ( 1 === $i % 4 || 0 === $i % 4 ) {
							$post_class = 'featured hentry';
							$thumbnail  = 'signify-collection-large-layout-two';
						}
					} elseif ( 'layout-three' == $layout ) {
						if ( 1 === $i % 4 ) {
							$post_class = 'featured hentry';
							$thumbnail  = 'signify-collection-large-layout-three';
						}
					}

					if ( has_post_thumbnail() ) {
						$thumb_url = get_the_post_thumbnail_url( get_the_ID(), $thumbnail );
					} else {
						$thumb_url = signify_get_no_thumb_image( $thumbnail, 'src' );
					}
					?>
					<article id="post-<?php the_ID(); ?>" <?php post_class( $post_class ); ?>>
						<div class="hentry-inner">
							<div class="post-thumbnail" style="background-image: url( '<?php echo esc_url( $thumb_url ); ?>' )">
								<a class="cover-link" href="<?php the_permalink(); ?>">
								</a>
							</div>

							<?php 
							if ( signify_is_woocommerce_activated() &&  'product' === $type ) {
								$title = 'woocommerce-loop-product__title';
							} else {
								$title = 'entry-title';
							}
							 ?>
							<div class="entry-container product-container">
								<header class="entry-header">
									<?php 
										if ( 'show-meta' === $show_meta  && ( 'post' === $type || 'category' === $type || 'product' === $type ) ) {
											if ( signify_is_woocommerce_activated() &&  'product' === $type ) {
												woocommerce_template_single_meta();
											} 
											else {
												echo signify_entry_header();
											}
										}
									?>

									<?php the_title( '<h2 class="' . esc_attr( $title ) . '"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">','</a></h2>' ); ?>
								</header>

								<?php if ( signify_is_woocommerce_activated() &&  'product' === $type ) { ?>
									<div class="product-meta">
										<?php woocommerce_template_single_rating(); ?>
										<?php woocommerce_template_single_price(); ?>
									</div>
								<?php } ?>

								<?php
								if ( 'excerpt' === $show_content ) {
									$excerpt = get_the_excerpt();

									echo '<div class="entry-summary"><p>' . $excerpt . '</p></div><!-- .entry-summary -->';
								} elseif ( 'full-content' === $show_content ) {
									$content = apply_filters( 'the_content', get_the_content() );
									$content = str_replace( ']]>', ']]&gt;', $content );
									echo '<div class="entry-content">' . wp_kses_post( $content ) . '</div><!-- .entry-content -->';
								} ?>

								<?php if ( signify_is_woocommerce_activated() &&  'product' === $type ) { ?>
									<div class="product-footer-meta">
										<?php woocommerce_template_loop_add_to_cart(); ?>
									</div>
								<?php } ?>
							</div><!-- .entry-container -->
						</div><!-- .hentry-inner -->
					</article>
					<?php
					
					if ( $i > 8 ) {
						$i = 0;
					}
					
					$i++;
				}

				wp_reset_postdata();
			}
			?>
		</div><!-- .collection-wrapper -->
	</div><!-- .wrapper -->
</div><!-- #collection-section -->
