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

/**
 * Class CyberPress_Team_API
 */
class CyberPress_Team_API extends CyberPress_Custom_Post_API {

    /**
     * CyberPress_Team_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 = 'team' ) {
        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 team_classes( $class = '', $post_id = null ) {
        post_class( $class, $post_id );
    }

    /**
     * Get Country of Foundation team.
     *
     * @return mixed
     */
    public function get_country_of_foundation() {
        $country_code = carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_team_country' );
        if ( isset( $country_code ) && ! empty( $country_code ) ) {
            $country = CyberPress_Country::country( $country_code );
            $flag    = CyberPress_Country::get_flag( $country_code );

            return apply_filters(
                'cyberpress_team_get_country_of_foundation',
                array(
                    'name'          => $country->getName(),
                    'official_name' => $country->getOfficialName(),
                    'flag'          => $flag,
                )
            );
        }
        return false;
    }

    /**
     * Get Date of Foundation team.
     *
     * @return mixed
     */
    public function get_date_of_foundation() {
        return apply_filters( 'cyberpress_team_get_date_of_foundation', carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_team_date_of_foundation' ) );
    }

    /**
     * Get Formatted Date of Foundation team.
     *
     * @return mixed
     */
    public function get_date_of_foundation_formatted() {
        $date = $this->get_date_of_foundation();

        if ( $date ) {
            return apply_filters( 'cyberpress_team_get_date_of_foundation_formatted', date_i18n( get_option( 'date_format' ), strtotime( $date ) ) );
        } else {
            return false;
        }
    }

    /**
     * The Game in which this team plays.
     *
     * @return mixed
     */
    public function get_game() {
        $game_id = carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_team_game' );

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

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

        return false;
    }

    /**
     * Get team Players.
     *
     * @return mixed
     */
    public function get_players() {
        $result             = array();
        $players            = carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_team_players' );
        $teams_player_roles = json_decode( CyberPress_Settings::get_option( 'teams_player_roles', 'cyberpress_teams' ), true );

        if ( $players && ! empty( $players ) ) {
            foreach ( $players as $player ) {
                $player_obj = cyberpress()->get_the_player( $player['player'] );
                $role_name  = $player['role'];

                foreach ( $teams_player_roles['slug'] as $key => $player_roles_slug ) {
                    if ( $player_roles_slug === $player['role'] ) {
                        $role_name = $teams_player_roles['name'][ $key ];
                    }
                }

                $result[] = array(
                    'role'      => $role_name,
                    'role_slug' => $player['role'],
                    'data'      => $player_obj,
                );
            }
        }

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

    /**
     * Get team Site Url.
     *
     * @return string
     */
    public function get_site_url() {
        return apply_filters( 'cyberpress_team_get_site_url', carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_team_site_url' ) );
    }

    /**
     * Get team Twitch Channel Url.
     *
     * @return mixed
     */
    public function get_twitch_channel_url() {
        $channel = carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_team_twitch' );
        return apply_filters( 'cyberpress_team_get_twitch_channel_url', $channel ? ( 'https://www.twitch.tv/' . esc_attr( $channel ) ) : false );
    }

    /**
     * Get team Twitch iframe oEmbed.
     *
     * @return string
     */
    public function get_twitch_player_oembed() {
        $channel = carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_team_twitch' );
        return apply_filters( 'cyberpress_team_get_twitch_player_oembed', CyberPress_Twitch::embed( $channel ) );
    }

    /**
     * Fallback for Twitch Chat.
     *
     * @return string
     */
    public function get_twitch_chat_oembed() {
        return '';
    }

    /**
     * Get team YouTube Channel Url.
     *
     * @return mixed
     */
    public function get_youtube_channel_url() {
        $channel = carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_team_youtube' );
        return apply_filters( 'cyberpress_team_get_youtube_channel_url', $channel ? ( 'https://www.youtube.com/user/' . esc_attr( $channel ) ) : false );
    }

    /**
     * Get team Social Links.
     *
     * @return mixed
     */
    public function get_social_links() {
        return apply_filters( 'cyberpress_team_get_social_links', carbon_get_post_meta( $this->get_id(), $this->get_global_prefix() . '_team_social_links' ) );
    }

    /**
     * Get Child Teams by Parent
     *
     * @return array|bool - child teams list or false if not exist or if it's parent.
     */
    public function get_child_teams() {
        $id = $this->get_id();
        if ( isset( $id ) && ! empty( $id ) ) {
            $child_teams_list = array();
            $child_teams      = get_children(
                array(
                    'post_parent' => $id,
                    'post_type'   => 'team',
                    // @codingStandardsIgnoreLine
                    'numberposts' => -1,
                    'post_status' => 'any',
                )
            );

            if ( isset( $child_teams ) && ! empty( $child_teams ) && is_array( $child_teams ) ) {
                foreach ( $child_teams as $child_team ) {
                    $team               = cyberpress()->get_the_team( $child_team->ID );
                    $child_teams_list[] = $team;
                }
                return apply_filters( 'cyberpress_team_get_childs', $child_teams_list );
            }
        }
        return false;
    }

    /**
     * Get Parent Team
     *
     * @return mixed|bool - parent team object or false if not exist or if it's parent.
     */
    public function get_parent_team() {
        $id = $this->get_id();
        if ( isset( $id ) && ! empty( $id ) ) {
            $parent_post = wp_get_post_parent_id( $id );
            if ( isset( $parent_post ) && ! empty( $parent_post ) ) {
                $parent_team = cyberpress()->get_the_team( $parent_post->ID );
                return apply_filters( 'cyberpress_team_get_parent', $parent_team );
            }
        }
        return false;
    }
}
