<?php

class Thememakers_Entity_Preview {

    public function register() {

        $args = array(
            'labels' => array(
                'name' => __('Previews', THEMEMAKERS_THEME_FOLDER_NAME),
                'singular_name' => __('Preview', THEMEMAKERS_THEME_FOLDER_NAME),
                'add_new' => __('Add New', THEMEMAKERS_THEME_FOLDER_NAME),
                'add_new_item' => __('Add New Preview', THEMEMAKERS_THEME_FOLDER_NAME),
                'edit_item' => __('Edit Preview', THEMEMAKERS_THEME_FOLDER_NAME),
                'new_item' => __('New Preview', THEMEMAKERS_THEME_FOLDER_NAME),
                'view_item' => __('View Preview', THEMEMAKERS_THEME_FOLDER_NAME),
                'search_items' => __('Search Preview', THEMEMAKERS_THEME_FOLDER_NAME),
                'not_found' => __('No Previews found', THEMEMAKERS_THEME_FOLDER_NAME),
                'not_found_in_trash' => __('No Previews found in Trash', THEMEMAKERS_THEME_FOLDER_NAME),
                'parent_item_colon' => ''
            ),
            'public' => true,
            'exclude_from_search' => false,
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'capability_type' => 'page',
            'has_archive' => true,
            'hierarchical' => true,
            'menu_position' => null,
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
            'rewrite' => array('slug' => 'pre-view'),
            'show_in_admin_bar' => true
        );
        register_post_type('pre-view', $args);
        flush_rewrite_rules(false);

        add_filter("manage_pre-view_posts_columns", array("Thememakers_Entity_Preview", "show_edit_columns"));
        add_action("manage_pre-view_posts_custom_column", array("Thememakers_Entity_Preview", "show_edit_columns_content"));
    }

    public static function init_meta_boxes() {
        add_meta_box("preview_settings_meta", __("Games", THEMEMAKERS_THEME_FOLDER_NAME), array('Thememakers_Entity_Preview', 'settings_meta'), "pre-view", "side", "default");
        add_meta_box("preview_videos", __("Videos", THEMEMAKERS_THEME_FOLDER_NAME), array('Thememakers_Entity_Preview', 'videos_meta'), "pre-view", "normal", "high");
    }

    public function settings_meta() {
        global $post;
        $data = array();
        $data['games'] = Thememakers_Entity_Game::get_games();
        $data['games_previews'] = self::get_preview_games($post->ID);
        echo ThemeMakersThemeView::draw_html('preview/settings_meta', $data);
    }

    public function videos_meta() {
        global $post;
        $data = array();
        $data['videos'] = self::get_preview_videos($post->ID);
        echo ThemeMakersThemeView::draw_html('preview/videos_meta', $data);
    }

    public static function save($post_id) {
        update_post_meta($post_id, "games_previews", $_POST["games_previews"]);
        update_post_meta($post_id, "preview_videos", $_POST["preview_videos"]);
    }

    public function show_edit_columns_content($column) {
        global $post;

        switch ($column) {
            case "image":
                if (has_post_thumbnail($post->ID)) {
                    echo '<img alt="" src="' . ThemeMakersHelper::get_post_featured_image($post->ID, 'gallery_thumb') . '"/>';
                }
                break;

            case "game":
                echo Thememakers_Entity_Game::get_game_term_list($post->ID, 'pre-view');
                break;

            case "consoles":
                echo get_the_term_list($post->ID, 'cons', '', ', ', '');
                break;

            case "rating":
                echo Thememakers_Entity_Rating::draw_rating($post->ID);
                break;

            case "date":
                $date = get_post_modified_time("U", false, $post->ID);
                echo $date;
                break;
        }
    }

    public function show_edit_columns($columns) {
        $columns = array(
            "cb" => "<input type=\"checkbox\" />",
            "title" => __("Title", THEMEMAKERS_THEME_FOLDER_NAME),
            "image" => __("Featured image", THEMEMAKERS_THEME_FOLDER_NAME),
            "game" => __("Games", THEMEMAKERS_THEME_FOLDER_NAME),
            "consoles" => __("Consoles", THEMEMAKERS_THEME_FOLDER_NAME),
            "rating" => __("Rating", THEMEMAKERS_THEME_FOLDER_NAME),
            "date" => __("Date", THEMEMAKERS_THEME_FOLDER_NAME)
        );

        return $columns;
    }

    public static function get_preview_games($post_id) {
        return get_post_meta($post_id, "games_previews", true);
    }

    public static function get_preview_videos($post_id) {
        return get_post_meta($post_id, "preview_videos", true);
    }

    public static function get_latest_previews($count) {
        $args = array();
        $args['numberposts'] = $count;
        $args['post_type'] = 'pre-view';
        return get_posts($args);
    }

}

