<?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_Comingsoon extends AmyFramework_Widget {

	function __construct() {
		$widget_ops		= array(
			'classname'		=> 'amy-widget-ads',
			'description'	=> esc_html__( 'Show Movie Coming Soon into Widget.', 'amy-movie' ),
		);

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

		parent::__construct( 'amy_movie_widget_comingsoon', esc_html__( '+ Amy Movie Coming Soon', '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 );
		$genre 			= empty( $instance['genre'] ) ? '' : $instance['genre'];
		$post_number 	= empty( $instance['post_number'] ) ? '' : $instance['post_number'];
		$class 			= empty( $instance['class'] ) ? '' : $instance['class'];
		$datenow		= strtotime( current_time( 'y-m-d' ) );

		echo $before_widget;

		if ( $genre != '' ) {
			$tax_query_genre = array(
				'taxonomy'	=> 'amy_genre',
				'field'		=> 'term_id',
				'terms'		=> implode( ',', $genre ),
			);
		} else {
			$tax_query_genre = '';
		}

		$args = array(
			'post_type'	=> 'amy_movie',
			'tax_query'	=> array(
				$tax_query_genre
			),
			'meta_query'	=> array(
				array(
					'key'		=> '_amy_movie_release',
					'value'		=> $datenow,
					'compare' 	=> '<',
				),
			),
			'posts_per_page'	=> $post_number,
			'meta_key'			=> '_amy_movie_release',
			'orderby'			=> 'meta_value',
			'order'				=> 'DESC',
		);

		$output = '<div class="amy-widget amy-widget-comingsoon ' . esc_attr( $class ) . '">';

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

		$output .= '<ul>';
		query_posts( $args );

		$i = 1;

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

				$output .= '<li><a href="' . get_the_permalink() . '"><span>' . $i . '.</span>' . get_the_title() . '</a></li>';

				$i++;
			endwhile;
		endif;

		wp_reset_query();

		$output .= '</ul></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'		=> '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',
				),
			),

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

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

register_widget( 'Amy_Movie_Widget_Comingsoon' );
