<?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;

$mdetails 	= amy_movie_get_details( $post->ID );
$mrating  	= amy_movie_get_rating( $post->ID );
$cinemas	= amy_movie_cinema_in_movie( $post->ID );

$movie_meta		= get_post_meta( $post->ID, '_movie_sidebar', true );

// rate
if ( isset( $mrating['movie_rating_turn'] ) ) {
	$m_turn = $mrating['movie_rating_turn'];
} else {
	$m_turn = 0;
}

if ( isset( $mrating['movie_rating_points'] ) ) {
	$m_point = $mrating['movie_rating_points'];
} else {
	$m_point = 0;
}

if ( $m_point != 0 && $m_turn != 0 ) {
	$m_ave	= round( $m_point / $m_turn, 1 );
} else {
	$m_ave = 0;
}

// media
$media		= array();
$gallery	= array();

if ( isset( $mdetails['movie_gallery'] ) ) {
	$gallery 	= $mdetails['movie_gallery'];
	$gallery 	= explode( ',', $gallery );
}

if ( ! empty( $gallery ) ) {
	foreach ( $gallery as $g ) {
		$media[] = $g;
	}
}

$tralier = array();

if ( isset( $mdetails['movie_tralier'] ) ) {
	$tralier = $mdetails['movie_tralier'];
}

if ( ! empty( $tralier ) ) {
	foreach ( $tralier as $t ) {
		if ( isset( $t['movie_tralier_link'] ) && $t['movie_tralier_link'] != '' ) {
			$media[] = $t['movie_tralier_link'];
		}

		if ( isset( $t['movie_tralier_upload'] ) && $t['movie_tralier_upload'] != '' ) {
			$media[] = $t['movie_tralier_upload'];
		}
	}
}

// Carousel
$slick  = '{';
$slick .= '"slidesToShow":4,"slidesToScroll":4,';
$slick .= '"autoplay":true,';
$slick .= '"autoplaySpeed":3000,';
$slick .= '"arrows":true,';
$slick .= '"infinite":true,';
$slick .= '"dots":false';
$slick .= '}';

$rateclass = (amy_get_option( 'enable_movie_rating' ) == true) ? 'user-rate' : '';

?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?><?php amy_movie_semantics( 'post' ); ?>>
	<?php if ( $movie_meta['movie_sidebar'] == 'full' ) : ?>
		<div class="row amy-single-movie">
			<div class="col-md-4 col-sm-4">
				<div class="entry-thumb">
					<?php echo amy_movie_get_poster( $post->ID, array( '360', '618' ) ); ?>
				</div>
			</div>
			<div class="col-md-8 col-sm-8">
				<div class="entry-info">
					<?php
					if ( is_single() ) {
						the_title( '<h1 class="entry-title p-name" itemprop="name headline"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark" class="u-url url" itemprop="url">', '</a></h1>' );
					}
					?>
					<div class="entry-pg">
						<span class="pg"><?php echo esc_attr( $mdetails['movie_mpaa'] ); ?></span>
				<span class="duration">
					<i class="fa fa-clock-o"></i>
					<?php echo amy_movie_converttime( $mdetails['movie_duration'] ); ?>
				</span>
					</div>
					<ul class="info-list">
						<li>
							<label><?php echo esc_html__( 'Actor:', 'amy-movie' ); ?></label>
							<span><?php echo amy_movie_get_movie_taxomony( $post->ID, 'amy_actor' ); ?></span>
						</li>
						<li>
							<label><?php echo esc_html__( 'Director:', 'amy-movie' ); ?></label>
							<span><?php echo amy_movie_get_movie_taxomony( $post->ID, 'amy_director' ); ?></span>
						</li>
						<li>
							<label><?php echo esc_html__( 'Genre:', 'amy-movie' ); ?></label>
							<span><?php echo amy_movie_get_movie_taxomony( $post->ID, 'amy_genre' ); ?></span>
						</li>
						<li>
							<label><?php echo esc_html__( 'Release:', 'amy-movie' ); ?></label>
							<span><?php echo esc_attr( date( get_option( 'date_format' ), strtotime( $mdetails['movie_release'] ) ) ); ?></span>
						</li>
						<li>
							<label><?php echo esc_html__( 'Language:', 'amy-movie' ); ?></label>
							<span><?php echo esc_attr( $mdetails['movie_language'] ); ?></span>
						</li>
						<?php if ( amy_get_option( 'enable_m_cinema', true ) == true ) : ?>
						<li>
							<label><?php echo esc_html__( 'Cinema:', 'amy-movie' ); ?></label>
							<?php
							if ( ! empty( $cinemas ) ) {
								$numItems 	= count( $cinemas );
								$i 			= 0;

								foreach ( $cinemas as $cinema ) {
									if ( ++$i === $numItems ) {
										$space = '';
									} else {
										$space = ', ';
									}

									?>
									<a href="<?php echo get_permalink( $cinema ); ?>"><?php echo get_the_title( $cinema ); ?></a>
									<?php echo esc_attr( $space ); ?>
								<?php
								}
							}
							?>
						</li>
						<?php endif; ?>
					</ul>

				</div>
				<div class="entry-action">
					<?php
					if ( $m_point != 0 && $m_turn != 0 ) {
						$classe = 'has-rate';
					} else {
						$classe = 'no-rate';
					}
					?>
					<div class="mrate <?php echo esc_attr( $rateclass ); ?> <?php echo esc_attr( $classe ); ?>">
						<?php if ( amy_get_option( 'enable_movie_rating' ) == true ) : ?>
							<?php echo amy_movie_show_star( $post->ID ); ?>
						<?php endif; ?>
						<?php if ( $m_point != 0 && $m_turn != 0 ) { ?>
						<ul class="mv-rating-stars">
							<li class="mv-current-rating user-rating" data-point="<?php echo round( $m_ave / 5, 2 ) * 100; ?>%"></li>
						</ul>
						<span class="mcount"><?php echo esc_attr( $m_turn ) . esc_html__( ' votes', 'amy-movie' ); ?></span>
						<span class="rate"><?php echo esc_attr( $m_ave ); ?></span>
						<?php } ?>
					</div>
					<div class="entry-share">
						<label><?php echo esc_html__( 'Share:', 'amy-movie' ); ?></label>
						<?php echo amy_movie_post_social_links(); ?>
					</div>
					<div class="clearfix"></div>
				</div>

				<div class="entry-content e-content" itemprop="description articleBody">
					<h3 class="info-name amy-title"><?php echo esc_html__( 'Synopsis', 'amy-movie' ); ?></h3>
					<?php the_content( esc_html__( 'Read More', 'amy-movie' ) ); ?>
					<?php
					wp_link_pages(
						array(
							'before' 		=> '<nav class="amy-page-break amy-pagination">',
							'after'  		=> '</nav>',
							'link_before'	=> '<span class="current">',
							'link_after'	=> '</span>',
						)
					);
					?>
				</div>
			</div>
		</div>
	<?php else : ?>

		<div class="entry-top">
			<div class="entry-poster">
				<?php echo amy_movie_get_poster( $post->ID, array( '204', '350' ) ); ?>
			</div>
			<div class="entry-info">
				<?php
				if ( is_single() ) {
					the_title( '<h1 class="entry-title p-name" itemprop="name headline"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark" class="u-url url" itemprop="url">', '</a></h1>' );
				}
				?>
				<div class="entry-pg">
					<span class="pg"><?php echo esc_attr( $mdetails['movie_mpaa'] ); ?></span>
				<span class="duration">
					<i class="fa fa-clock-o"></i>
					<?php echo amy_movie_converttime( $mdetails['movie_duration'] ); ?>
				</span>
				</div>
				<ul class="info-list">
					<li>
						<label><?php echo esc_html__( 'Actor:', 'amy-movie' ); ?></label>
						<span><?php echo amy_movie_get_movie_taxomony( $post->ID, 'amy_actor' ); ?></span>
					</li>
					<li>
						<label><?php echo esc_html__( 'Director:', 'amy-movie' ); ?></label>
						<span><?php echo amy_movie_get_movie_taxomony( $post->ID, 'amy_director' ); ?></span>
					</li>
					<li>
						<label><?php echo esc_html__( 'Genre:', 'amy-movie' ); ?></label>
						<span><?php echo amy_movie_get_movie_taxomony( $post->ID, 'amy_genre' ); ?></span>
					</li>
					<li>
						<label><?php echo esc_html__( 'Release:', 'amy-movie' ); ?></label>
						<span><?php echo esc_attr( $mdetails['movie_release'] ); ?></span>
					</li>
					<li>
						<label><?php echo esc_html__( 'Language:', 'amy-movie' ); ?></label>
						<span><?php echo esc_attr( $mdetails['movie_language'] ); ?></span>
					</li>
					<?php if ( amy_get_option( 'enable_m_cinema', true ) == true ) : ?>
					<li>
						<label><?php echo esc_html__( 'Cinema:', 'amy-movie' ); ?></label>
						<?php
						if ( ! empty( $cinemas ) ) {
							$numItems 	= count( $cinemas );
							$i 			= 0;

							foreach ( $cinemas as $cinema ) {
								if ( ++$i === $numItems ) {
									$space = '';
								} else {
									$space = ', ';
								}

								?>
								<a href="<?php echo get_permalink( $cinema ); ?>"><?php echo get_the_title( $cinema ); ?></a>
								<?php echo esc_attr( $space ); ?>
							<?php
							}
						}
						?>
					</li>
					<?php endif; ?>
				</ul>
				<div class="entry-action">
				<?php
				if ( $m_point != 0 && $m_turn != 0 ) {
					$classe = 'has-rate';
				} else {
					$classe = 'no-rate';
				}
				?>
					<div class="mrate <?php echo esc_attr( $rateclass ); ?> <?php echo esc_attr( $classe ); ?>">
						<?php if ( amy_get_option( 'enable_movie_rating' ) == true ) : ?>
							<?php echo amy_movie_show_star( $post->ID ); ?>
						<?php endif; ?>
						<?php if ( $m_point != 0 && $m_turn != 0 ) { ?>
						<ul class="mv-rating-stars">
							<li class="mv-current-rating user-rating" data-point="<?php echo round( $m_ave / 5, 2 ) * 100; ?>%"></li>
						</ul>
						<span class="mcount"><?php echo esc_attr( $m_turn ) . esc_html__( ' votes', 'amy-movie' ); ?></span>
						<span class="rate"><?php echo esc_attr( $m_ave ); ?></span>
						<?php } ?>
					</div>
					<div class="entry-share">
						<label><?php echo esc_html__( 'Share:', 'amy-movie' ); ?></label>
						<?php echo amy_movie_post_social_links(); ?>
					</div>
					<div class="clearfix"></div>
				</div>
			</div>
		</div>
		<div class="clearfix"></div>
		<div class="entry-content e-content" itemprop="description articleBody">
			<h3 class="info-name amy-title"><?php echo esc_html__( 'Synopsis', 'amy-movie' ); ?></h3>
			<?php the_content( esc_html__( 'Read More', 'amy-movie' ) ); ?>
			<?php
			wp_link_pages(
				array(
					'before' 		=> '<nav class="amy-page-break amy-pagination">',
					'after'  		=> '</nav>',
					'link_before'	=> '<span class="current">',
					'link_after'	=> '</span>',
				)
			);
			?>
		</div>
	<?php endif; ?>


	<div class="entry-media">
		<h3 class="info-name amy-title"><?php echo esc_html__( 'Video & Photo', 'amy-movie' ); ?></h3>
		<div class="number-media">
			<?php if ( count( $tralier ) > 0 ) : ?>
				<span class="video"><i></i><?php echo count( $tralier ) . esc_html__( ' videos', 'amy-movie' ); ?></span>
			<?php endif; ?>
			<?php if ( count( $gallery ) > 0 ) : ?>
				<span class="gallery"><i></i><?php echo count( $gallery ) . esc_html__( ' photos', 'amy-movie' ); ?></span>
			<?php endif; ?>
		</div>
		<div class="media-carousel">
			<div class="amy-slick" data-slick='<?php echo $slick; ?>'>
				<?php if ( ! empty( $media ) ) : ?>
					<?php foreach ( $media as $i => $m ) : ?>
						<div class="media-item">
						<?php
						if ( is_numeric( $m ) ) { ?>
							<?php $src = wp_get_attachment_image_src( $m, 'full' )[0]; ?>
							<a href="<?php echo esc_url( $src ); ?>" class="amy-fancybox" rel="movie-gallery">
								<?php echo wp_get_attachment_image( $m, array( '200', '150' ) ); ?>
							</a>
						<?php } else { ?>
							<?php
							if ( strpos( $m, 'youtube' ) == true ) {
								parse_str( parse_url( $m, PHP_URL_QUERY ), $youtube );
								echo '<a href="https://www.youtube.com/embed/' . $youtube['v'] . '" class="fancybox.iframe amy-fancybox">';
								echo '<img src="http://img.youtube.com/vi/' . $youtube['v'] . '/hqdefault.jpg"	/>';
							} else if ( strpos( $m, 'vimeo.com' ) == true ) {
								global $wp_filesystem;

								if ( empty( $wp_filesystem ) ) {
									require_once( ABSPATH . '/wp-admin/includes/file.php' );
									WP_Filesystem();
								}

								$id		= (int) substr( parse_url( $m, PHP_URL_PATH ), 1 );
								$hash 	= unserialize( $wp_filesystem->get_contents( "http://vimeo.com/api/v2/video/$id.php" ) );

								echo '<a href="https://player.vimeo.com/video/' . $id . '" class="fancybox.iframe amy-fancybox">';
								echo '<img src="' . $hash[0]['thumbnail_large'] . '" />';
							} else if ( strpos( $m, 'dailymotion.com' ) == true ) {
								$m_id = strtok( basename( $m ), '_' );
								echo '<a href="http://www.dailymotion.com/embed/video/' . $m_id . '" class="fancybox.iframe amy-fancybox">';
								echo '<img src="http://www.dailymotion.com/thumbnail/video/' . $m_id . '" />';
							}
							?>
							<i class="fa fa-play"></i>
							</a>
						<?php } ?>
						</div>
					<?php endforeach; ?>
				<?php endif; ?>
			</div>
		</div>
	</div>

	<?php if ( ( ! empty( $cinemas ) && amy_get_option( 'enable_m_cinema', true ) == true) && amy_get_option( 'movie_showtime', true ) == true ) : ?>
		<div class="entry-showtime">
			<div class="clearfix">
				<h3 class="info-name amy-title"><?php echo esc_html__( 'Showtime', 'amy-movie' ); ?></h3>
			</div>
			<div class="select-cinema">
				<h4><?php echo esc_html__( 'Select a cinema', 'amy-movie' ); ?></h4>
				<ul>
					<?php if ( ! empty( $cinemas ) ) : ?>
						<?php foreach ( $cinemas as $i => $cinema ) : ?>
							<li data-cinema="<?php echo $cinema; ?>" data-movie="<?php echo $post->ID; ?>"><?php echo get_the_title( $cinema ); ?></li>
						<?php endforeach; ?>
					<?php endif; ?>
				</ul>
			</div>
			<div class="showtime">

			</div>
			<div class="clearfix"></div>
		</div>
	<?php endif; ?>

	<?php if ( amy_get_option( 'movie_comment' ) == true ) : ?>
		<div class="entry-comment">
			<?php
			comments_template( '', true );
			?>
		</div>
	<?php endif; ?>
</article>
