<?php
/**
 * Class api for working with fields of custom match posts.
 *
 * @package cyberpress
 */

/**
 * Class CyberPress_Match_API
 */
class CyberPress_Match_API extends CyberPress_Custom_Post_API {

    /**
     * CyberPress_Match_API constructor.
     *
     * @param null   $id - post id. If null or not exist - we use global $post->ID.
     * @param string $post_type - post type.
     */
    public function __construct( $id = null, $post_type = 'match' ) {
        global $post;
        if ( isset( $id ) && ! empty( $id ) ) {
            $this->set_id( $id );
        } else {
            $this->set_id( $post->ID );
        }
        $this->set_post_type( $post_type );
    }

    /**
     * Display the classes for the post div.
     *
     * @param string $class - One or more classes to add to the class list.
     * @param null   $post_id - Post ID or post object. Defaults to the global `$post`.
     */
    public function match_classes( $class = '', $post_id = null ) {
        post_class( $class, $post_id );
    }

    /**
     * Get Participants of Teams.
     *
     * @return mixed
     */
    public function get_participants() {
        $result = array();
        $teams  = (array) carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_match_participants_team' );

        foreach ( $teams as $team ) {
            $players = array();

            foreach ( (array) $team['players'] as $player ) {
                $player_object = cyberpress()->get_the_player( (int) $player['player'] );
                $players[]     = $player_object;
            }

            $team_object = cyberpress()->get_the_team( (int) $team['team_name'] );

            $result[] = array(
                'team'    => $team_object,
                'points'  => (int) $team['points'],
                'players' => $players,
            );
        }

        return apply_filters( 'cyberpress_match_get_participants', $result );
    }

    /**
     * Get Match Screenshots.
     *
     * @return mixed
     */
    public function get_screenshots() {
        return apply_filters( 'cyberpress_match_get_screenshots', carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_match_screenshots' ) );
    }

    /**
     * Get Match Videos.
     *
     * @return mixed
     */
    public function get_videos() {
        $result = array();
        $videos = carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_match_videos' );

        foreach ( $videos as $video ) {
            $result[] .= $video['video'];
        }

        return apply_filters( 'cyberpress_match_get_videos', $result );
    }

    /**
     * The Match Game.
     *
     * @return mixed
     */
    public function get_game() {
        $game_id = carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_match_game' );

        // team game data.
        if ( $game_id ) {
            $game = cyberpress()->get_the_game( $game_id );

            if ( $game->get_url() ) {
                return apply_filters( 'cyberpress_match_get_game', $game );
            }
        }

        return false;
    }

    /**
     * Get Match Time Start.
     *
     * @return mixed
     */
    public function get_time_start() {
        return apply_filters( 'cyberpress_match_get_time_start', carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_match_time_start' ) );
    }

    /**
     * Get Match Time End.
     *
     * @return mixed
     */
    public function get_time_end() {
        return apply_filters( 'cyberpress_match_get_time_end', carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_match_time_end' ) );
    }

    /**
     * Get Match Time Start.
     *
     * @return mixed
     */
    public function get_time_start_formatted() {
        $time = $this->get_time_start();
        return apply_filters( 'cyberpress_match_get_time_start_formatted', date_i18n( get_option( 'date_format' ), strtotime( $time ) ) . ' ' . date_i18n( get_option( 'time_format' ), strtotime( $time ) ) );
    }

    /**
     * Get Match Time End.
     *
     * @return mixed
     */
    public function get_time_end_formatted() {
        $time = $this->get_time_end();
        return apply_filters( 'cyberpress_match_get_time_end_formatted', date_i18n( get_option( 'date_format' ), strtotime( $time ) ) . ' ' . date_i18n( get_option( 'time_format' ), strtotime( $time ) ) );
    }

    /**
     * Get Match Tournament.
     *
     * @return mixed
     */
    public function get_tournament() {
        $tournament_id = carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_match_tournament' );
        $tournament    = cyberpress()->get_the_tournament( $tournament_id );
        return apply_filters( 'cyberpress_match_get_tournament', $tournament );
    }

    /**
     * Get Match Tournament Stage.
     *
     * @return bool|mixed
     */
    public function get_tournament_stage() {
        $match_tournament_stage = carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_match_tournament_stage' );
        $tournament_stages      = $this->get_tournament()->get_tournament_stages();

        if ( isset( $tournament_stages ) && is_array( $tournament_stages ) && is_int( $match_tournament_stage ) && $match_tournament_stage > 0 && ! empty( $tournament_stages ) ) {
            return apply_filters( 'cyberpress_match_get_tournament_stage', $tournament_stages[ $match_tournament_stage - 1 ] );
        }

        return false;
    }
}
