<?php

namespace Dispatch\PostTypes;

use Timber\Timber;
use Timber\Post as TimberPost;
use WP_Query;

class Updates extends Post {

    protected static $this_post_type = 'updates';

    static $today;

    static $progress = array(
            'key' => 'progress',
            'name' => 'In Progress'
        );

    static $scheduled = array(
            'key' => 'scheduled',
            'name' => 'Scheduled'
        );

    static $complete = array(
            'key' => 'complete',
            'name' => 'Completed'
        );

    static $unknown = null;

    public static function get_by_date_range( $posts_per_page_min ) {

        static::$today = current_time('Ymd');

        $progress = static::get_progress() ?: array();
        $scheduled = static::get_scheduled() ?: array();

        $updates = array_merge( $progress, $scheduled );

        if ( count( $updates ) < $posts_per_page_min ) {
            $complete = static::get_complete( $posts_per_page_min - count( $updates ) ) ?: array();
            $updates = array_merge( $updates, $complete );
        }

        if ( count( $updates ) < $posts_per_page_min ) {
            $exclude = array_map( function( $post ) { return $post->ID; }, $updates );
            $unknown = static::get_unknown( $posts_per_page_min - count( $updates ), $exclude ) ?: array();
            $updates = array_merge( $updates, $unknown );
        }

        return $updates;

    }

    public static function get_status( $post_id = null ) {

        if ( !$post_id ) $post_id = get_the_ID();

        $today = current_time('Ymd');

        $event_start_date = get_field( 'event_start_date', $post_id, false ) ?: null;
        $event_end_date = get_field( 'event_end_date', $post_id, false ) ?: null;

        if( $event_end_date == '' ) $event_end_date = null;
        if( $event_start_date == '' ) $event_end_date = null;

        if ( $event_start_date <= $today && $event_end_date >= $today ) {
            return static::$progress;
        } else if ( $event_start_date > $today ) {
            return static::$scheduled;
        } else if ( isset($event_end_date) && $event_end_date < $today ) {
            return static::$complete;
        } else {
            return static::$unknown;
        }

    }

    public static function format_update( $posts, $status ) {

        foreach ($posts as $post) {
            $post->status = $status;
        }

        return $posts;

    }

    public static function get_progress( $posts_per_page = -1 ) {

        $args = array (
            'post_type' => static::$this_post_type,
            'posts_per_page' => $posts_per_page,
            'meta_query' => array(
                array(
                    'key'       => 'event_start_date',
                    'compare'   => '<=',
                    'value'     => static::$today,
                ),
                 array(
                    'key'       => 'event_end_date',
                    'compare'   => '>=',
                    'value'     => static::$today,
                )
            ),
        );

        $posts = \Timber::get_posts($args);

        return static::format_update( $posts, static::$progress );

    }

    public static function get_scheduled( $posts_per_page = -1 ) {

        $args = array (
            'post_type' => static::$this_post_type,
            'posts_per_page' => $posts_per_page,
            'meta_query' => array(
                array(
                    'key'       => 'event_start_date',
                    'compare'   => '>',
                    'value'     => static::$today,
                )
            )
        );

        $posts = \Timber::get_posts($args);

        return static::format_update( $posts, static::$scheduled );

    }

    public static function get_complete( $posts_per_page = -1 ) {

        $args = array (
            'post_type' => static::$this_post_type,
            'posts_per_page' => $posts_per_page,
            'meta_query' => array(
                 array(
                    'key'       => 'event_end_date',
                    'compare'   => '<',
                    'value'     => static::$today,
                ),
                array(
                    'key'       => 'event_end_date',
                    'compare'   => '!=',
                    'value'     => '',
                )
            )
        );

        $posts = \Timber::get_posts($args);

        return static::format_update( $posts, static::$complete );

    }

    public static function get_unknown( $posts_per_page = -1, $exclude = array() ) {

        $args = array (
            'post_type' => static::$this_post_type,
            'posts_per_page' => $posts_per_page,
            // 'exclude ' => $exclude,
            'post__not_in' => $exclude
        );

        $posts = \Timber::get_posts($args);

        return static::format_update( $posts, static::$unknown );

    }

}
