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

if ( ! class_exists( 'amy_movie_Genre' ) ) {
	class amy_movie_Genre {
		function get_genre_args( $args ) {
			$genres = get_terms( $args );

			if ( ! empty( $genres ) ) {
				foreach ( $genres as $genre ) {
					$options[ $genre->term_id ] = $genre->name;
				}
			}

			return $options;
		}
	}
}

class Amy_Movie_Widget_List extends AmyFramework_Widget {

	function __construct() {
		$widget_ops		= array(
			'classname'		=> 'amy-widget-list',
			'description'	=> esc_html__( 'Show list post/movie into Widget.', 'amy-movie' ),
		);

		$control_ops	= array(
			'width'			=> 400,
			'height'		=> 350,
		);

		parent::__construct( 'amy_movie_widget_list', esc_html__( '+ Amy Movie List', 'amy-movie' ), $widget_ops, $control_ops );
	}

	function widget( $args, $instance ) {
		extract( $args );

		$title			= apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		$post_type 		= empty( $instance['post_type'] ) ? '' : $instance['post_type'];
		$category 		= empty( $instance['category'] ) ? '' : $instance['category'];
		$genre 			= empty( $instance['genre'] ) ? '' : $instance['genre'];
		$post_number 	= empty( $instance['post_number'] ) ? '' : $instance['post_number'];
		$post_orderby 	= empty( $instance['post_orderby'] ) ? '' : $instance['post_orderby'];
		$movie_orderby 	= empty( $instance['movie_orderby'] ) ? '' : $instance['movie_orderby'];
		$post_order 	= empty( $instance['post_order'] ) ? '' : $instance['post_order'];
		$post_date 		= empty( $instance['post_date'] ) ? '' : $instance['post_date'];
		$class 			= empty( $instance['class'] ) ? '' : $instance['class'];

		echo $before_widget;

		// Custom query
		$params 	= array();
		$img_size	= array( '118', '159' );

		$default_image_size	= amy_movie_default_image_size();

		if ( ! empty( $default_image_size ) ) {
			foreach ( $default_image_size as $size ) {
				if ( $size['img_size_name'] == 'Movie Widget List' && $post_type == 'movie' ) {
					$img_size = array( $size['img_size_width'], $size['img_size_height'] );
				} else if ( $size['img_size_name'] == 'Blog Widget List' && $post_type == 'post' ) {
					$img_size = array( $size['img_size_width'], $size['img_size_height'] );
				}
			}
		}

		if ( $genre != '' ) {
			$genre = implode( ',', $genre );
		}

		$params['movie_date']		= $post_date;
		$params['movie_genre']		= $genre;
		$params['movie_orderby']	= $movie_orderby;
		$params['movie_order']		= $post_order;
		$params['movie_per_page']	= $post_number;

		$loop = '';

		if ( $post_type == 'movie' ) {
			$arpg = amy_movie_query( $params );

			$the_query 	= new WP_Query( $arpg );
			$data		= $the_query->posts;

			if ( ! empty( $data ) ) {
				foreach ( $data as $i => $item ) {
					$mdetails = amy_movie_get_details( $item->ID );
					$mrating = amy_movie_get_rating( $item->ID );

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

					$loop .= '<div class="entry-item">';

					$loop .= '<div class="entry-thumb">';
					$loop .= amy_movie_get_poster( $item->ID, $img_size );
					$loop .= '</div>';

					$loop .= '<div class="entry-content">';
					$loop .= '<h2 class="entry-title"><a href="' . get_permalink( $item->ID ) . '">' . $item->post_title . '</a></h2>';

					if ( $mdetails['movie_duration'] != '' ) {
						$loop .= '<div><span class="duration"><i class="fa fa-clock-o"></i>' . amy_movie_converttime( $mdetails['movie_duration'] ) . '</span></div>';
					}

					$loop .= '<div class="genre"><span>' . amy_movie_get_movie_taxomony( $item->ID, 'amy_genre' ) . '</span></div>';

					if ( $m_point != 0 && $m_turn != 0 ) {
						$loop .= '<div class="mrate">';
						$loop .= '<ul class="mv-rating-stars">';
						$loop .= '<li class="mv-current-rating" data-point="' . round( $m_ave / 5, 2 ) * 100 . '%' . '"></li>';
						$loop .= '</ul>';
						// $loop .= '<span class="mcount">' . $m_turn . esc_html__(' votes', 'amy-movie') . '</span>';
						$loop .= '<span class="rate">' . $m_ave . '</span>';
						$loop .= '</div>';
					}

					$loop .= '</div>';
					$loop .= '<div class="clearfix"></div>';
					$loop .= '</div>';
				}
			}
		} else if ( $post_type == 'post' ) {
			if ( $category != '' ) {
				$cat_query = array(
					'taxonomy'	=> 'category',
					'field'		=> 'term_id',
					'terms'		=> $category,
				);
			} else {
				$cat_query = '';
			}

			$query_date = array();

			if ( $post_date == 'day' ) {
				$query_date = array(
					array(
						'after'	=> '24 hours ago',
					),
				);
			} else if ( $post_date == 'week' ) {
				$query_date = array(
					array(
						'after'	=> '1 week ago',
					),
				);
			} else if ( $post_date == 'month' ) {
				$query_date = array(
					array(
						'after'	=> '1 month ago',
					),
				);
			}

			$arpg = array(
				'tax_query'	=> array(
					$cat_query
				),
				'date_query'		=> $query_date,
				'posts_per_page'	=> $post_number,
				'orderby'			=> $post_orderby,
				'order'				=> $post_order,
			);

			query_posts( $arpg );

			if ( have_posts() ) :
				while ( have_posts() ) :
					the_post();

					global $post;

					$loop .= '<div class="entry-item">';
					$loop .= '<div class="entry-thumb">' . get_the_post_thumbnail( $post->ID, $img_size ) . '</div>';
					$loop .= '<div class="entry-content">';
					$loop .= '<h2 class="entry-title"><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h2>';
					$loop .= '<div class="entry-meta">';
					$loop .= '<span class="entry-date">' . get_the_date() . '</span>';
					$loop .= '<span> / </span>';
					$loop .= '<span class="entry-comment">' . get_comments_number( $post->ID ) . esc_html__( ' Comments', 'amy-movie' ) . '</span>';
					$loop .= '</div>';
					$loop .= '</div>';
					$loop .= '<div class="clearfix"></div>';
					$loop .= '</div>';

				endwhile;
			endif;

			wp_reset_query();
		}

		$output = '<div class="amy-widget amy-widget-list list-' . $post_type . ' ' . $class . '">';

		if ( ! empty( $title ) ) {
			$output .= '<h4 class="amy-title amy-widget-title">' . esc_html( $title ) . '</h4>';
		}

		$output .= $loop;

		$output .= '</div>';

		echo $output;

		echo $after_widget;
	}

	function get_options() {
		$genre = new amy_movie_Genre();

		return array(
			array(
				'id'				=> 'title',
				'type'				=> 'text',
				'title'				=> esc_html__( 'Title', 'amy-movie' ),
			),

			array(
				'id'		=> 'post_type',
				'type'		=> 'select',
				'class'		=> 'horizontal',
				'title'		=> esc_html__( 'Post Type', 'amy-movie' ),
				'options'	=> array(
					'post'		=> esc_html__( 'Post', 'amy-movie' ),
					'movie'		=> esc_html__( 'Movie', 'amy-movie' ),
				),
				'default'	=> 'amy_movie',
			),

			array(
				'id'		=> 'category',
				'type'		=> 'select',
				'title'		=> esc_html__( 'Categories', 'amy-movie' ),
				'options'	=> 'categories',
				'class'		=> 'chosen',
				'attributes'	=> array(
					'data-placeholder'	=> 'Select category',
					'multiple'	=> 'only-key',
					'style'		=> 'width: 200px',
				),
				'dependency'	=> array( 'post_type', '==', 'post' ),
			),

			array(
				'id'		=> 'genre',
				'type'		=> 'select',
				'title'		=> esc_html__( 'Genres', 'amy-movie' ),
				'options'	=> amy_element_values('callback', array(
					'function'	=> array( $genre, 'get_genre_args' ),
					'args'		=> array(
						'taxonomy' => 'amy_genre',
					),
				)),
				'class'		=> 'chosen',
				'attributes'	=> array(
					'data-placeholder'	=> 'Select Genre',
					'multiple'	=> 'only-key',
					'style'		=> 'width: 200px',
				),
				'dependency'	=> array( 'post_type', '==', 'movie' ),
			),

			array(
				'id'		=> 'post_number',
				'type'		=> 'text',
				'title'		=> esc_html__( 'Number of Posts/Movies', 'amy-movie' ),
				'default'	=> '4',
			),

			array(
				'id'		=> 'post_orderby',
				'type'		=> 'select',
				'title'		=> esc_html__( 'Order By', 'amy-movie' ),
				'options'	=> array(
					'ID'		=> esc_html__( 'Post ID', 'amy-movie' ),
					'author'	=> esc_html__( 'Author', 'amy-movie' ),
					'title'		=> esc_html__( 'Title', 'amy-movie' ),
					'date'		=> esc_html__( 'Date', 'amy-movie' ),
					'rand'		=> esc_html__( 'Random Order', 'amy-movie' ),
					'comment_count'	=> esc_html__( 'Comment Count', 'amy-movie' ),
				),
				'default'	=> 'date',
				'dependency'	=> array( 'post_type', '==', 'post' ),
			),

			array(
				'id'		=> 'movie_orderby',
				'type'		=> 'select',
				'title'		=> esc_html__( 'Order By', 'amy-movie' ),
				'options'	=> array(
					'_amy_movie_rate'		=> esc_html__( 'Rate', 'amy-movie' ),
					'_amy_movie_release' 	=> esc_html__( 'Release Date', 'amy-movie' ),
					'_amy_post_views_count'	=> esc_html__( 'Post Views', 'amy-movie' ),
					'ID'					=> esc_html__( 'Post ID', 'amy-movie' ),
					'author'				=> esc_html__( 'Author', 'amy-movie' ),
					'title'					=> esc_html__( 'Title', 'amy-movie' ),
					'date'					=> esc_html__( 'Date', 'amy-movie' ),
					'rand'					=> esc_html__( 'Random Order', 'amy-movie' ),
					'comment_count'			=> esc_html__( 'Comment Count', 'amy-movie' ),
				),
				'default'	=> 'date',
				'dependency'	=> array( 'post_type', '==', 'movie' ),
			),

			array(
				'id'		=> 'post_order',
				'type'		=> 'select',
				'title'		=> esc_html__( 'Sort order', 'amy-movie' ),
				'options'	=> array(
					'DESC'	=> esc_html__( 'Descending', 'amy-movie' ),
					'ASC'	=> esc_html__( 'Ascending', 'amy-movie' ),
				),
				'defalut'	=> 'DESC',
			),

			array(
				'id'		=> 'post_date',
				'type'		=> 'select',
				'title'		=> esc_html__( 'Date From', 'amy-movie' ),
				'options'	=> array(
					'day'		=> esc_html__( 'Last 1 day', 'amy-movie' ),
					'week'		=> esc_html__( 'Last 7 days', 'amy-movie' ),
					'month'		=> esc_html__( 'Last 30 days', 'amy-movie' ),
					'alltime'	=> esc_html__( 'All time', 'amy-movie' ),
				),
				'default'	=> 'alltime',
			),

			array(
				'id'		=> 'class',
				'type'		=> 'text',
				'title'		=> esc_html__( 'Extra Class', 'amy-movie' ),
			),
		);
	}
}

register_widget( 'Amy_Movie_Widget_List' );
