<?php
/**
 * @copyright Copyright (c) 2016 AmyTheme (http://www.amytheme.com). All rights reserved.
 * @license  http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

defined( 'ABSPATH' ) or die;

get_header();

get_template_part( 'partials/page-header' );

$amy_type = isset( $_GET['amy_type'] ) ? $_GET['amy_type'] : '';

if ( $amy_type != '' ) {
	switch ( $amy_type ) {
		case 'amy_director':
			$args = array(
				'taxonomy' 	=> 'amy_director',
				'name'		=> $s,
			);

			$data = get_terms( $args );

			break;

		case 'amy_actor':
			$args = array(
				'taxonomy' 	=> 'amy_actor',
				'name'		=> $s,
			);

			$data = get_terms( $args );

			break;

		case 'amy_cinema':
			$args = array(
				's'	=> $s,
				'post_type'			=> 'amy_cinema',
				'posts_per_page'	=> amy_get_option( 'search_number' ),
			);

			$the_query 	= new WP_Query( $args );
			$max   		= intval( $the_query->max_num_pages );
			$data		= $the_query->posts;

			break;
	}
} else {
	$args = array(
		's'	=> $s,
		'post_type'			=> 'amy_movie',
		'posts_per_page'	=> amy_get_option( 'search_number' ),
	);

	$the_query 	= new WP_Query( $args );
	$max   		= intval( $the_query->max_num_pages );
	$data		= $the_query->posts;

	$img_size 	= array( '205', '347' );

	$custom_image_sizes	= amy_get_option( 'custom_image_sizes' );

	if ( ! empty( $custom_image_sizes ) ) {
		foreach ( $custom_image_sizes as $size ) {
			if ( $size['img_size_name'] == 'Movie List Size' ) {
				$img_size = array( $size['img_size_width'], $size['img_size_height'] );
			}
		}
	}

	$options	 = array(
		'img_size'		=> $img_size,
		'max_pages'		=> $max,
		'pagination'	=> true,
	);
}

?>
<section class="amy-main-content movie-search">
	<div class="container">
		<?php
		if ( $amy_type != '' ) {
			switch ( $amy_type ) {
				case 'amy_director':
					if ( ! empty( $data ) ) {
						?>
						<div class="director-search">
							<?php foreach ( $data as $i => $item ) : ?>
								<?php if ( ! is_wp_error( $item ) ) : ?>
									<article>
										<h1 class="entry-title"><a href="<?php echo get_term_link( $item, 'amy_director' ); ?>"><?php echo esc_attr( $item->name ); ?></a></h1>
										<div class="entry-summary"><?php echo $item->description; ?></div>
									</article>
								<?php endif; ?>
							<?php endforeach; ?>
						</div>
						<?php
					} else {
						echo esc_html__( 'No Director', 'amy-movie' );
					}

					break;

				case 'amy_actor':
					if ( ! empty( $data ) ) {
						?>
						<div class="actor-search">
							<?php foreach ( $data as $i => $item ) : ?>
								<?php if ( ! is_wp_error( $item ) ) : ?>
									<article>
										<h1 class="entry-title"><a href="<?php echo get_term_link( $item, 'amy_actor' ); ?>"><?php echo esc_attr( $item->name ); ?></a></h1>
										<div class="entry-summary"><?php echo $item->description; ?></div>
									</article>
								<?php endif; ?>
							<?php endforeach; ?>
						</div>
						<?php
					} else {
						echo esc_html__( 'No Actor', 'amy-movie' );
					}

					break;

				case 'amy_cinema':
					if ( ! empty( $data ) ) {
						?>
						<div class="row cinema-search">
							<?php foreach ( $data as $i => $item ) :
								$details 	= get_post_meta( $item->ID, '_cinema_block_options', true );
							?>
								<div class="col-md-4">
									<?php if ( isset( $details['cinema_banner'] ) ) : ?>
										<div class="entry-thumb"><?php echo wp_get_attachment_image( $details['cinema_banner'], 'full' ); ?></div>
									<?php endif; ?>
									<h1 class="entry-title"><a href="<?php echo get_permalink( $item->ID ); ?>"><?php echo esc_attr( $item->post_title ); ?></a></h1>
								</div>
							<?php endforeach; ?>
						</div>
						<?php
					} else {
						echo esc_html__( 'No Cinema', 'amy-movie' );
					}

					break;
			}
		} else { ?>
			<div class="amy-shortcode amy-mv-list">
			<?php if ( ! empty( $data ) ) {
				echo amy_movie_listlayout( $data, $options );
} else {
	echo esc_html__( 'No Movie', 'amy-movie' );
}
			?>
			</div>
		<?php
		}
		?>
	</div>
</section>


<?php
get_footer();
