<?php
/**
 * Kopa Widget Video
 *
 * @author 		Kopatheme
 * @category 	Widgets
 * @package 	KopaFramework/Widgets
 * @since    	1.0.0
 * @extends 	Kopa_Widget
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class Kopa_Widget_Video extends Kopa_Widget {
	/**
	 * Constructor
	 */
	public function __construct() {

        //Tags Array
        $tags = get_tags();
        $arr_tags = array(
            '' => __('----SELECT----', 'kopa-framework')
            );
        foreach ($tags as $category) 
            $arr_tags[$category->term_id] = $category->name.' ('.$category->count.')';

        //Categories Array
        $categories = get_categories();
        $arr_cats = array(
            '' => __('----SELECT----', 'kopa-framework')
            );
        foreach ($categories as $category)
            $arr_cats[$category->term_id] = $category->name.' ('.$category->count.')';

		$this->widget_cssclass    = 'kopa-widget-video';
		$this->widget_description = __( 'Display article list with type video.', 'newmoment' );
		$this->widget_id          = 'kopa-widget-video';
		$this->widget_name        = __( 'Kopa Widget Video', 'newmoment' );
		// $this->widget_width       = 400;
		$this->settings           = array(
			'title'  => array(
				'type'  => 'text',
				'std'   => __( 'Kopa Widget Video', 'newmoment' ),
				'label' => __( 'Title:', 'newmoment' )
			),
            'categories' => array(
                'type'    => 'multiselect',
                'std'     => null,
                'label'   => __( 'Categories:', 'newmoment' ),
                'options' => $arr_cats,
            ),
            'relation' => array(
                'type'    => 'select',
                'label'   => __( 'Relation:', 'newmoment' ),
                'options' => array(
                    'OR'  => __( 'OR', 'newmoment' ),
                    'AND' => __( 'AND', 'newmoment' ),
                )
            ),
            'tags' => array(
                'type'    => 'multiselect',
                'std'     => null,
                'label'   => __( 'Tags:', 'newmoment' ),
                'options' => $arr_tags,
            ),
            'orderby' => array(
                'type'    => 'select',
                'std'     => '',
                'label'   => __( 'Orderby:', 'newmoment' ),
                'options' => array(
                    'latest'       => __('Latest', 'newmoment'),
                    'popular'      => __('Popular by view count', 'newmoment'),
                    'most_comment' => __('Popular by comment count', 'newmoment'),
                    'random'       => __('Random', 'newmoment')
                )
            ),
            'timestamp' => array(
                'type'  => 'select',
                'std'   => '',
                'label' => __( 'Timestamp (ago):', 'newmoment' ),
                'options' => array(
                    ''          => __('-- Select --', 'newmoment'),
                    '-1 week'   => __('1 week', 'newmoment'),
                    '-2 week'   => __('2 weeks', 'newmoment'),
                    '-3 week'   => __('3 weeks', 'newmoment'),
                    '-1 month'  => __('1 months', 'newmoment'),
                    '-2 month'  => __('2 months', 'newmoment'),
                    '-3 month'  => __('3 months', 'newmoment'),
                    '-4 month'  => __('4 months', 'newmoment'),
                    '-5 month'  => __('5 months', 'newmoment'),
                    '-6 month'  => __('6 months', 'newmoment'),
                    '-7 month'  => __('7 months', 'newmoment'),
                    '-8 month'  => __('8 months', 'newmoment'),
                    '-9 month'  => __('9 months', 'newmoment'),
                    '-10 month' => __('10 months', 'newmoment'),
                    '-11 month' => __('11 months', 'newmoment'),
                    '-1 year'   => __('1 year', 'newmoment'),
                    '-2 year'   => __('2 years', 'newmoment'),
                    '-3 year'   => __('3 years', 'newmoment'),
                    '-4 year'   => __('4 years', 'newmoment'),
                    '-5 year'   => __('5 years', 'newmoment'),
                    '-6 year'   => __('6 years', 'newmoment'),
                    '-7 year'   => __('7 years', 'newmoment'),
                    '-8 year'   => __('8 years', 'newmoment'),
                    '-9 year'   => __('9 years', 'newmoment'),
                    '-10 year'  => __('10 years', 'newmoment'),
                )
            ),
			'posts_per_page' => array(
				'type'  => 'number',
				'step'  => 1,
				'min'   => 1,
				'max'   => '',
				'std'   => 5,
				'label' => __( 'Number of items:', 'newmoment' )
			)
		);
		parent::__construct();
	}

	/**
	 * widget function.
	 *
	 * @see WP_Widget
	 * @access public
	 * @param array $args
	 * @param array $instance
	 * @return void
	 */
	public function widget( $args, $instance ) {

		if ( $this->get_cached_widget( $args ) )
			return;

		ob_start();
		extract( $args );

		$title       = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );

        $query_args['categories'] = $instance['categories'];
        $query_args['relation'] = esc_attr($instance['relation']);
        $query_args['tags'] = $instance['tags'];
        $query_args['posts_per_page'] = (int) $instance['posts_per_page'];

        if (isset($instance['timestamp'])){
            $query_args['date_query'] = $instance['timestamp'];
        }

        $query_args['orderby'] = $instance['orderby'];
        $number_of_post = (int) $instance['posts_per_page'];
        $query_args['post_format'] = 'post-format-video';

        $status = get_theme_mod( 'kopa_status_default_thumbnail', true );
        if($status == 'enable') {
            $status = true;
        }else {
            $status = false;
        }

        $posts = kopa_widget_posttype_build_query($query_args);
        ?>
        <div class="widget kopa-video-widget clearfix">
            <?php if($title) { ?>
                <h3 class="widget-title"><?php echo $title; ?>
                    <span class="left-corner"></span>
                    <span class="right-corner"></span>
                </h3>
            <?php } ?>
            <?php $count = 0; $next = true; ?>
            <?php while($posts->have_posts()) : $posts->the_post(); ?>
            <?php if($count == 0) { ?>
            <article class="last-item">
                <?php if(has_post_thumbnail() || $status == true) : ?>
                    <div class="entry-thumb">
                        <a href="<?php the_permalink(); ?>">
                            <?php the_post_thumbnail('kopa-size-12'); ?>
                        </a>
                        <a class="entry-icon fa fa-play" href="<?php the_permalink(); ?>"></a>
                        <div class="meta-box clearfix">                                        
                            <span class="entry-time"><i class="fa fa-clock-o"></i><span><?php echo get_post_time('g:i A') ;?></span></span>
                            <span class="entry-meta">|</span>
                            <span class="entry-date"><i class="fa fa-calendar"></i><span><?php the_date(); ?></span></span>
                        </div>
                        <!-- meta-box -->
                    </div>
                <?php endif; ?>
                <div class="entry-content">
                    <h6 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h6>
                </div>
            </article>
            <?php } else {
                    if($next) {
                        echo '<ul class="older-post clearfix">';
                        $next = false;
                    }
                ?>
                
                <li>
                    <h6 class="entry-title"><a href="<?php the_permalink(); ?>"><i class="fa fa-film"></i><?php the_title(); ?></a></h6>
                </li>
                <?php if($count == $number_of_post) {
                    echo '</ul>';
                    }
                }
                $count++; 
                ?>
            <?php endwhile; ?>
            </ul>
        </div>
		<?php
		wp_reset_postdata();

        $content = ob_get_clean();

        echo $content;

        $this->cache_widget( $args, $content );
	}
}

register_widget( 'Kopa_Widget_Video' );