<?php

class Thememakers_Entity_Game {

    public function register() {

        $args = array(
            'labels' => array(
                'name' => __('Games', THEMEMAKERS_THEME_FOLDER_NAME),
                'singular_name' => __('Games', THEMEMAKERS_THEME_FOLDER_NAME),
                'add_new' => __('Add New', THEMEMAKERS_THEME_FOLDER_NAME),
                'add_new_item' => __('Add New Game', THEMEMAKERS_THEME_FOLDER_NAME),
                'edit_item' => __('Edit Game', THEMEMAKERS_THEME_FOLDER_NAME),
                'new_item' => __('New Game', THEMEMAKERS_THEME_FOLDER_NAME),
                'view_item' => __('View Game', THEMEMAKERS_THEME_FOLDER_NAME),
                'search_items' => __('Search Games', THEMEMAKERS_THEME_FOLDER_NAME),
                'not_found' => __('No Games found', THEMEMAKERS_THEME_FOLDER_NAME),
                'not_found_in_trash' => __('No Games found in Trash', THEMEMAKERS_THEME_FOLDER_NAME),
                'parent_item_colon' => ''
            ),
            'public' => true,
            //'menu_icon' => THEMEMAKERS_THEME_URI . '/images/icons/games.png',
            'exclude_from_search' => false,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'query_var' => true,
            'capability_type' => 'post',
            'hierarchical' => true,
            'menu_position' => null,
            'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
            'rewrite' => array('slug' => 'games', 'with_front' => false),
            'show_in_admin_bar' => true,
            'has_archive' => true
        );
        register_post_type('games', $args);
        flush_rewrite_rules(false);

        //*** taxonomies ****
        register_taxonomy("cons", array("games", "review", "release", "pre-view", "gall", "post", "page"), array(
            "hierarchical" => true,
            "labels" => array(
                'name' => __('Consoles', THEMEMAKERS_THEME_FOLDER_NAME),
                'singular_name' => __('Console', THEMEMAKERS_THEME_FOLDER_NAME),
                'add_new' => __('Add New', THEMEMAKERS_THEME_FOLDER_NAME),
                'add_new_item' => __('Add New Console', THEMEMAKERS_THEME_FOLDER_NAME),
                'edit_item' => __('Edit Console', THEMEMAKERS_THEME_FOLDER_NAME),
                'new_item' => __('New Console', THEMEMAKERS_THEME_FOLDER_NAME),
                'view_item' => __('View Console', THEMEMAKERS_THEME_FOLDER_NAME),
                'search_items' => __('Search Consoles', THEMEMAKERS_THEME_FOLDER_NAME),
                'not_found' => __('No Consoles found', THEMEMAKERS_THEME_FOLDER_NAME),
                'not_found_in_trash' => __('No Consoles found in Trash', THEMEMAKERS_THEME_FOLDER_NAME),
                'parent_item_colon' => ''
            ),
            "singular_label" => __("console", THEMEMAKERS_THEME_FOLDER_NAME),
            "rewrite" => true,
            'show_in_nav_menus' => true,
            'capabilities' => array('edit_themes'),
            'show_ui' => true
        ));


        register_taxonomy("gamecat", array("games", "gall"), array(
            "hierarchical" => true,
            "labels" => array(
                'name' => __('Games categories', THEMEMAKERS_THEME_FOLDER_NAME),
                'singular_name' => __('Game category', THEMEMAKERS_THEME_FOLDER_NAME),
                'add_new' => __('Add New', THEMEMAKERS_THEME_FOLDER_NAME),
                'add_new_item' => __('Add New Game category', THEMEMAKERS_THEME_FOLDER_NAME),
                'edit_item' => __('Edit Game category', THEMEMAKERS_THEME_FOLDER_NAME),
                'new_item' => __('New Game category', THEMEMAKERS_THEME_FOLDER_NAME),
                'view_item' => __('View Game category', THEMEMAKERS_THEME_FOLDER_NAME),
                'search_items' => __('Search Game categories', THEMEMAKERS_THEME_FOLDER_NAME),
                'not_found' => __('No Game categories found', THEMEMAKERS_THEME_FOLDER_NAME),
                'not_found_in_trash' => __('No Game categories found in Trash', THEMEMAKERS_THEME_FOLDER_NAME),
                'parent_item_colon' => ''
            ),
            "singular_label" => __("gamecat", THEMEMAKERS_THEME_FOLDER_NAME),
            "show_tagcloud" => true,
            'query_var' => true,
            'rewrite' => true,
            'show_in_nav_menus' => true,
            'capabilities' => array('edit_themes'),
            'show_ui' => true
        ));


        add_filter("manage_games_posts_columns", array("Thememakers_Entity_Game", "show_edit_columns"));
        add_action("manage_games_posts_custom_column", array("Thememakers_Entity_Game", "show_edit_columns_content"));

        
    }

   

    public static function features() {
        $data = array();
        $data['featured_games'] = self::get_featured_games();
        echo ThemeMakersThemeView::draw_page('game/features', $data);
    }

    public static function search_games() {
        global $wpdb;
        $posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_title LIKE '%{$_REQUEST['game_name']}%' AND post_type= 'games' AND post_status='publish'");

        if (isset($_REQUEST['search_mode'])) {
            if ($_REQUEST['search_mode'] == 'user_profile') {
                echo ThemeMakersThemeView::draw_html('game/draw_searched_games', array("posts" => $posts));
                exit();
            }
        }
        echo ThemeMakersThemeView::draw_html('game/draw_searched_features', array("posts" => $posts));
        exit();
    }

    public static function save_featured_games() {
        $data = array();
        parse_str($_REQUEST['values'], $data);
        ThemeMakersThemeModel::update_option('featured_games', $data);
        _e('Options have been updated.', THEMEMAKERS_THEME_FOLDER_NAME);
        exit;
    }

    public static function get_featured_games() {
        $data = ThemeMakersThemeModel::get_option("featured_games");
        if (isset($data['featured_games'])) {
            return $data['featured_games'];
        }
        return array();
    }

    public function settings_meta() {
        global $post;
        $data = array();
        $custom = get_post_custom($post->ID);
        $data['release_date'] = @$custom["release_date"][0];
        echo ThemeMakersThemeView::draw_html('game/settings_meta', $data);
    }

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

    public static function init_meta_boxes() {
        add_meta_box("game_settings_meta", __("Game settings", THEMEMAKERS_THEME_FOLDER_NAME), array('Thememakers_Entity_Game', 'settings_meta'), "games", "normal", "low");
    }

    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 "gamecat":
                echo get_the_term_list($post->ID, 'gamecat', '', ', ', '');
                break;

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

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

            case "release_date":
                echo get_post_meta($post->ID, "release_date", true);
                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" => __("Game Title", THEMEMAKERS_THEME_FOLDER_NAME),
            "image" => __("Featured image", THEMEMAKERS_THEME_FOLDER_NAME),
            "gamecat" => __("Categories", THEMEMAKERS_THEME_FOLDER_NAME),
            "consoles" => __("Consoles", THEMEMAKERS_THEME_FOLDER_NAME),
            "rating" => __("Rating", THEMEMAKERS_THEME_FOLDER_NAME),
            "release_date" => __("Release date", THEMEMAKERS_THEME_FOLDER_NAME),
            "date" => __("Date", THEMEMAKERS_THEME_FOLDER_NAME)
        );

        return $columns;
    }

    public static function get_games() {
        $args = array(
            'orderby' => 'post_title',
            'order' => 'ASC',
            'post_type' => 'games',
            'post_status' => 'publish',
			'numberposts'     => -1,
            'suppress_filters' => true
			);
        return get_posts($args);
    }

    public static function get_game_term_list($post_id, $term) {
        $saved_games = array();
        switch ($term) {
            case 'gall':
                $saved_games = Thememakers_Entity_Gallery::get_gallery_games($post_id);
                break;
            case 'release':
                $saved_games = Thememakers_Entity_Release::get_release_games($post_id);
                break;
            case 'review':
                $saved_games = Thememakers_Entity_Review::get_review_games($post_id);
                break;
            case 'pre-view':
                $saved_games = Thememakers_Entity_Preview::get_preview_games($post_id);
                break;

            default:
                break;
        }



        $games = Thememakers_Entity_Game::get_games();
        $result = array();
        if (!empty($saved_games)) {
            if (!empty($games)) {
                foreach ($games as $key => $game) {
                    if (in_array($game->ID, $saved_games)) {
                        $result[] = '<a rel="tag" href="' . get_permalink($game->ID) . '">' . $game->post_title . '</a>';
                    }
                }
            }

            return implode(',', $result);
        }

        return "";
    }

    public static function get_users_play_this_game($post_id) {
        $users_ids = ThemeMakersUser::get_game_using_stat($post_id);
        global $wpdb;
        $users = array();
        if (!empty($users_ids)) {
            $users_ids = implode(",", $users_ids);
            $users = $wpdb->get_results("SELECT ID, display_name FROM $wpdb->users WHERE ID IN($users_ids)");
        }
        return $users;
    }

}
