<?php
/**
 * Esport Posts Settings.
 *
 * @package cyberpress
 */

use Carbon_Fields\Container;
use Carbon_Fields\Field;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * CyberPress Settings Class
 */
class CyberPress_Posts_Settings {
    /**
     * Flag to prevent recursive field registration.
     *
     * @var bool
     */
    private static $is_registering_fields = false;

    /**
     * CyberPress_Posts_Settings constructor.
     */
    public function __construct() {
        $this->init_actions();
    }

    /**
     * Init actions
     */
    public function init_actions() {
        add_action( 'carbon_fields_register_fields', array( $this, 'cyberpress_carbon_fields_register_fields' ), 100 );
        add_action( 'wp_ajax_cyberpress_add_post_tabs', array( $this, 'add_post_tabs' ) );
        add_action( 'wp_loaded', array( $this, 'create_child_team' ) );
        add_filter( 'gutenberg_can_edit_post_type', array( $this, 'gutenberg_cant_edit_cyberpress_page_type' ), 10, 2 );
    }

    /**
     * Creating CPT options for admin pages
     */
    public function cyberpress_carbon_fields_register_fields() {
        // Prevent infinite recursion during field registration.
        if ( self::$is_registering_fields ) {
            return;
        }
        self::$is_registering_fields = true;

        $global_prefix = 'cyberpress';

        // List of countries.
        $full_country_array                 = CyberPress_Country::countries( true, false );
        $full_country_options_choises_array = array(
            '' => array(
                'icon'  => '',
                'label' => esc_html__( 'Choose your country', 'cyberpress' ),
                'value' => '',
            ),
        );
        foreach ( $full_country_array as $key => $country ) {
            $full_country_options_choises_array[ $key ] = array(
                'icon'  => CyberPress_Country::get_flag( $key ),
                'label' => $country['name']['common'],
                'value' => $key,
            );
        }
        $date_format = get_option( 'date_format' );

        $player_game_options_choises_array = $this->get_posts_or_taxonomy( 'game' );
        $player_team_options_choises_array = $this->get_posts_or_taxonomy( 'team' );
        $team_player_options_choises_array = $this->get_posts_or_taxonomy( 'player' );
        $tournaments_options_choises_array = $this->get_posts_or_taxonomy( 'tournament' );

        // Fields for Player CPT.
        Container::make( 'post_meta', esc_html__( 'Personal Information', 'cyberpress' ) )
            ->where( 'post_type', '=', 'player' )
            ->add_tab(
                esc_html__( 'Base', 'cyberpress' ),
                array(
                    Field::make( 'text', $global_prefix . '_player_full_name', esc_html__( 'Full Name:', 'cyberpress' ) ),
                    Field::make( 'react_select', $global_prefix . '_player_gender', esc_html__( 'Gender:', 'cyberpress' ) )
                        ->add_options(
                            array(
                                ''       => esc_html__( 'Choose your gender', 'cyberpress' ),
                                'male'   => esc_html__( 'Male', 'cyberpress' ),
                                'female' => esc_html__( 'Female', 'cyberpress' ),
                            )
                        )
                        ->set_props(
                            array(
                                'placeholder' => esc_attr__( 'Choose your gender', 'cyberpress' ),
                                'searchable'  => false,
                            )
                        )
                        ->set_width( 33.33 ),
                    Field::make( 'date', $global_prefix . '_player_date_birth', esc_html__( 'Date of Birth:', 'cyberpress' ) )
                            ->set_input_format( $date_format, $date_format )
                            ->set_storage_format( $date_format )
                            ->set_width( 33.33 ),
                    Field::make( 'react_select', $global_prefix . '_player_country', esc_html__( 'Country:', 'cyberpress' ) )
                        // TODO: add call_back function.
                            ->add_options( $full_country_options_choises_array )
                            ->set_props(
                                array(
                                    'placeholder' => esc_attr__( 'Choose your country', 'cyberpress' ),
                                    'icons'       => true,
                                )
                            )
                            ->set_width( 33.33 ),
                )
            )
            ->add_tab(
                esc_html__( 'Social', 'cyberpress' ),
                array(
                    Field::make( 'text', $global_prefix . '_player_site_url', esc_html__( 'Site Url:', 'cyberpress' ) )
                        ->set_attribute( 'placeholder', esc_html__( 'https://...', 'cyberpress' ) ),
                    Field::make( 'text', $global_prefix . '_player_twitch', esc_html__( 'Twitch Channel:', 'cyberpress' ) )
                        ->set_attribute( 'placeholder', esc_html__( 'lirik', 'cyberpress' ) )
                        ->set_width( 50 ),
                    Field::make( 'text', $global_prefix . '_player_youtube', esc_html__( 'YouTube Channel:', 'cyberpress' ) )
                        ->set_attribute( 'placeholder', esc_html__( 'saqibaz', 'cyberpress' ) )
                        ->set_width( 50 ),
                    Field::make( 'complex', $global_prefix . '_player_social_links', esc_html__( 'Social Links:', 'cyberpress' ) )
                        ->set_layout( 'tabbed-horizontal' )
                        ->setup_labels(
                            array(
                                'plural_name'   => esc_html__( 'Social Links', 'cyberpress' ),
                                'singular_name' => esc_html__( 'Social Link', 'cyberpress' ),
                            )
                        )
                        ->add_fields(
                            array(
                                Field::make( 'react_select', 'icon', '' )
                                    ->add_options( $this->get_social_icons() )
                                    ->set_props(
                                        array(
                                            'placeholder' => esc_attr__( 'Choose Social Icons', 'cyberpress' ),
                                        )
                                    )
                                    ->set_width( 50 ),
                                Field::make( 'text', 'url', '' )
                                    ->set_attribute( 'placeholder', esc_html__( 'https://...', 'cyberpress' ) )
                                    ->set_width( 50 ),
                            )
                        )
                        ->set_header_template( '<%- icon %>' ),
                )
            )
            ->add_tab(
                esc_html__( 'Games', 'cyberpress' ),
                array(
                    Field::make( 'complex', $global_prefix . '_player_games', esc_html__( 'Games Stats:', 'cyberpress' ) )
                    ->setup_labels(
                        array(
                            'plural_name'   => esc_html__( 'Games', 'cyberpress' ),
                            'singular_name' => esc_html__( 'Game', 'cyberpress' ),
                        )
                    )
                    ->add_fields(
                        array(
                            Field::make( 'react_select', 'game', '' )
                                // TODO: add call_back function.
                                ->add_options( $player_game_options_choises_array )
                                ->set_props(
                                    array(
                                        'placeholder' => esc_attr__( 'Choose game', 'cyberpress' ),
                                    )
                                )
                            ->set_width( 50 ),
                            Field::make( 'text', 'nickname', '' )
                                ->set_attribute( 'placeholder', esc_html__( 'Nickname', 'cyberpress' ) )
                                ->set_width( 50 ),
                            Field::make( 'text', 'wins', esc_html__( 'Wins:', 'cyberpress' ) )
                                ->set_attribute( 'placeholder', esc_html__( 'Number Of Wins', 'cyberpress' ) )
                                ->set_attribute( 'type', 'number' )
                                ->set_attribute( 'min', 0 )
                                ->set_attribute( 'step', 1 )
                                ->set_width( 33.33 ),
                            Field::make( 'text', 'draws', esc_html__( 'Draws:', 'cyberpress' ) )
                                ->set_attribute( 'placeholder', esc_html__( 'Number Of Draws', 'cyberpress' ) )
                                ->set_attribute( 'type', 'number' )
                                ->set_attribute( 'min', 0 )
                                ->set_attribute( 'step', 1 )
                                ->set_width( 33.33 ),
                            Field::make( 'text', 'losses', esc_html__( 'Losses:', 'cyberpress' ) )
                                ->set_attribute( 'placeholder', esc_html__( 'Number Of Losses', 'cyberpress' ) )
                                ->set_attribute( 'type', 'number' )
                                ->set_attribute( 'min', 0 )
                                ->set_attribute( 'step', 1 )
                                ->set_width( 33.33 ),
                        )
                    ),
                )
            )
            ->add_tab(
                esc_html__( 'Teams', 'cyberpress' ),
                array(
                    Field::make( 'complex', $global_prefix . '_player_teams', esc_html__( 'Teams:', 'cyberpress' ) )
                    ->setup_labels(
                        array(
                            'plural_name'   => esc_html__( 'Teams', 'cyberpress' ),
                            'singular_name' => esc_html__( 'Team', 'cyberpress' ),
                        )
                    )
                    ->add_fields(
                        array(
                            Field::make( 'react_select', 'team', esc_html__( 'Team:', 'cyberpress' ) )
                                // TODO: add call_back function.
                                ->add_options( $player_team_options_choises_array )
                                ->set_props(
                                    array(
                                        'placeholder' => esc_attr__( 'Choose team', 'cyberpress' ),
                                    )
                                ),
                            Field::make( 'date', 'start_date', esc_html__( 'Start date of participation:', 'cyberpress' ) )
                                    ->set_input_format( $date_format, $date_format )
                                    ->set_storage_format( $date_format )
                                    ->set_width( 50 ),
                            Field::make( 'date', 'end_date', esc_html__( 'End date of participation:', 'cyberpress' ) )
                                    ->set_input_format( $date_format, $date_format )
                                    ->set_storage_format( $date_format )
                                    ->set_width( 50 ),
                        )
                    ),
                )
            );

        // Fields for Team CPT.
        $child_teams  = Container::make( 'post_meta', esc_html__( 'Child Teams', 'cyberpress' ) )
            ->where( 'post_type', '=', 'team' );
        $child_fields = array();

        $teams_player_roles          = json_decode( CyberPress_Settings::get_option( 'teams_player_roles', 'cyberpress_teams' ), true );
        $teams_player_roles_sanitize = array();
        foreach ( $teams_player_roles['slug'] as $key => $player_roles_slug ) {
            $teams_player_roles_sanitize[ $player_roles_slug ] = $teams_player_roles['name'][ $key ];
        }

        // phpcs:ignore
        if ( ! isset( $_REQUEST['post'] ) && empty( $_REQUEST['post'] ) ) {
            $child_fields[] = Field::make( 'html', $global_prefix . '_child_teams_notice', '' )
                ->set_html( esc_html__( 'After creating main team, you can add child teams', 'cyberpress' ) )
                ->set_classes( 'cyberpress-child-teams-notice' );
        }

        $child_fields[] = Field::make( 'html', $global_prefix . '_child_teams_html', esc_html__( 'Section Description', 'cyberpress' ) )
                ->set_html( $this->add_post_tabs() )
                ->set_classes( 'cyberpress-create-child-teams' );

        $child_teams->add_fields( $child_fields );

        Container::make( 'post_meta', esc_html__( 'Team Information', 'cyberpress' ) )
            ->where( 'post_type', '=', 'team' )
            ->add_tab(
                esc_html__( 'Base', 'cyberpress' ),
                array(
                    Field::make( 'react_select', $global_prefix . '_team_country', esc_html__( 'Country of Foundation:', 'cyberpress' ) )
                        // TODO: add call_back function.
                        ->add_options( $full_country_options_choises_array )
                        ->set_props(
                            array(
                                'placeholder'    => esc_attr__( 'Choose your country', 'cyberpress' ),
                                'removeSelected' => false,
                                'isClearable'    => false,
                                'icons'          => true,
                            )
                        )
                        ->set_width( 33.33 ),
                    Field::make( 'date', $global_prefix . '_team_date_of_foundation', esc_html__( 'Date of Foundation:', 'cyberpress' ) )
                            ->set_input_format( $date_format, $date_format )
                            ->set_storage_format( $date_format )
                            ->set_width( 33.33 ),
                    Field::make( 'react_select', $global_prefix . '_team_game', esc_html__( 'Game:', 'cyberpress' ) )
                        // TODO: add call_back function.
                            ->add_options( $player_game_options_choises_array )
                            ->set_props(
                                array(
                                    'placeholder' => esc_attr__( 'Choose game', 'cyberpress' ),
                                )
                            )
                            ->set_width( 33.33 ),
                    Field::make( 'complex', $global_prefix . '_team_players', esc_html__( 'Players:', 'cyberpress' ) )
                    ->setup_labels(
                        array(
                            'plural_name'   => esc_html__( 'Players', 'cyberpress' ),
                            'singular_name' => esc_html__( 'Player', 'cyberpress' ),
                        )
                    )
                    ->add_fields(
                        array(
                            Field::make( 'react_select', 'player', esc_html__( 'Player:', 'cyberpress' ) )
                                // TODO: add call_back function.
                                ->add_options( $team_player_options_choises_array )
                                ->set_props(
                                    array(
                                        'placeholder' => esc_attr__( 'Choose player', 'cyberpress' ),
                                    )
                                )
                                ->set_width( 50 ),
                            Field::make( 'react_select', 'role', esc_html__( 'Role:', 'cyberpress' ) )
                                    ->add_options(
                                        $teams_player_roles_sanitize
                                    )
                                    ->set_props(
                                        array(
                                            'placeholder' => esc_attr__( 'Choose player role', 'cyberpress' ),
                                            'searchable'  => false,
                                        )
                                    )
                                    ->set_width( 50 ),
                        )
                    ),
                )
            )
            ->add_tab(
                esc_html__( 'Social', 'cyberpress' ),
                array(
                    Field::make( 'text', $global_prefix . '_team_site_url', esc_html__( 'Site Url:', 'cyberpress' ) )
                        ->set_attribute( 'placeholder', esc_html__( 'https://...', 'cyberpress' ) ),
                    Field::make( 'text', $global_prefix . '_team_twitch', esc_html__( 'Twitch Channel:', 'cyberpress' ) )
                        ->set_attribute( 'placeholder', esc_html__( 'lirik', 'cyberpress' ) )
                        ->set_width( 50 ),
                    Field::make( 'text', $global_prefix . '_team_youtube', esc_html__( 'YouTube Channel:', 'cyberpress' ) )
                        ->set_attribute( 'placeholder', esc_html__( 'saqibaz', 'cyberpress' ) )
                        ->set_width( 50 ),
                    Field::make( 'complex', $global_prefix . '_team_social_links', esc_html__( 'Social Links:', 'cyberpress' ) )
                        ->set_layout( 'tabbed-horizontal' )
                        ->setup_labels(
                            array(
                                'plural_name'   => esc_html__( 'Social Links', 'cyberpress' ),
                                'singular_name' => esc_html__( 'Social Link', 'cyberpress' ),
                            )
                        )
                        ->add_fields(
                            array(
                                Field::make( 'react_select', 'icon', '' )
                                    ->add_options( $this->get_social_icons() )
                                    ->set_props(
                                        array(
                                            'placeholder' => esc_attr__( 'Choose Social Icons', 'cyberpress' ),
                                        )
                                    )
                                    ->set_width( 50 ),
                                Field::make( 'text', 'url', '' )
                                    ->set_attribute( 'placeholder', esc_html__( 'https://...', 'cyberpress' ) )
                                    ->set_width( 50 ),
                            )
                        )
                        ->set_header_template( '<%- icon %>' ),
                )
            );

        // Fields for Match CPT.
        Container::make( 'post_meta', esc_html__( 'Match Information', 'cyberpress' ) )
            ->where( 'post_type', '=', 'match' )
            ->add_tab(
                esc_html__( 'Participants', 'cyberpress' ),
                array(
                    Field::make( 'complex', $global_prefix . '_match_participants_team', esc_html__( 'Teams:', 'cyberpress' ) )
                        ->set_layout( 'tabbed-vertical' )
                        ->set_max( 2 )
                        ->setup_labels(
                            array(
                                'plural_name'   => esc_html__( 'Teams', 'cyberpress' ),
                                'singular_name' => esc_html__( 'Team', 'cyberpress' ),
                            )
                        )
                        ->add_fields(
                            array(
                                Field::make( 'react_select', 'team_name', esc_html__( 'Team:', 'cyberpress' ) )
                                    // TODO: add call_back function.
                                    ->add_options( $player_team_options_choises_array )
                                    ->set_classes( 'cyberpress_team-repeater-selector' )
                                    ->set_props(
                                        array(
                                            'placeholder' => esc_attr__( 'Choose team', 'cyberpress' ),
                                        )
                                    )
                                    ->set_width( 50 ),
                                Field::make( 'text', 'points', esc_html__( 'Match Points:', 'cyberpress' ) )
                                        ->set_attribute( 'placeholder', esc_html__( 'Enter Match Points', 'cyberpress' ) )
                                        ->set_attribute( 'type', 'number' )
                                        ->set_attribute( 'min', 0 )
                                        ->set_attribute( 'step', 1 )
                                        ->set_width( 50 ),
                                Field::make( 'complex', 'players', esc_html__( 'Players:', 'cyberpress' ) )
                                        ->setup_labels(
                                            array(
                                                'plural_name' => esc_html__( 'Players', 'cyberpress' ),
                                                'singular_name' => esc_html__( 'Player', 'cyberpress' ),
                                            )
                                        )
                                        ->add_fields(
                                            array(
                                                Field::make( 'react_select', 'player', '' )
                                                    // TODO: add call_back function.
                                                    ->add_options( $team_player_options_choises_array )
                                                    ->set_props(
                                                        array(
                                                            'placeholder' => esc_attr__( 'Choose player', 'cyberpress' ),
                                                        )
                                                    ),
                                            )
                                        )
                                    ->set_classes( 'cyberpress_team-repeater-players' ),
                            )
                        ),
                )
            )
            ->add_tab(
                esc_html__( 'Media', 'cyberpress' ),
                array(
                    Field::make( 'media_gallery', $global_prefix . '_match_screenshots', esc_html__( 'Srceenshots:', 'cyberpress' ) )
                        ->set_type( array( 'image' ) ),
                    Field::make( 'complex', $global_prefix . '_match_videos', esc_html__( 'Videos:', 'cyberpress' ) )
                        ->setup_labels(
                            array(
                                'plural_name'   => esc_html__( 'Videos', 'cyberpress' ),
                                'singular_name' => esc_html__( 'Video', 'cyberpress' ),
                            )
                        )
                        ->add_fields(
                            array(
                                Field::make( 'oembed', 'video', '' )
                                    ->set_help_text( esc_html__( 'Enter YouTube/Vimeo/Twitch Video URL', 'cyberpress' ) ),
                            )
                        ),
                )
            );

        Container::make( 'post_meta', esc_html__( 'Match Game', 'cyberpress' ) )
            ->set_context( 'side' )
            ->set_priority( 'default' )
            ->where( 'post_type', '=', 'match' )
            ->add_fields(
                array(
                    Field::make( 'react_select', $global_prefix . '_match_game', '' )
                        // TODO: add call_back function.
                        ->add_options( $player_game_options_choises_array )
                        ->set_props(
                            array(
                                'placeholder' => esc_attr__( 'Choose game', 'cyberpress' ),
                            )
                        ),
                )
            );
        Container::make( 'post_meta', esc_html__( 'Match Time', 'cyberpress' ) )
            ->set_context( 'side' )
            ->set_priority( 'default' )
            ->where( 'post_type', '=', 'match' )
            ->add_fields(
                array(
                    Field::make( 'date_time', $global_prefix . '_match_time_start', esc_html__( 'Start:', 'cyberpress' ) ),
                    Field::make( 'date_time', $global_prefix . '_match_time_end', esc_html__( 'End:', 'cyberpress' ) ),
                )
            );

        Container::make( 'post_meta', esc_html__( 'Tournament', 'cyberpress' ) )
            ->set_context( 'side' )
            ->set_priority( 'default' )
            ->where( 'post_type', '=', 'match' )
            ->add_fields(
                array(
                    Field::make( 'react_select', $global_prefix . '_match_tournament', '' )
                        // TODO: add call_back function.
                        ->add_options( $tournaments_options_choises_array )
                        ->set_props(
                            array(
                                'placeholder' => esc_attr__( 'Choose Tournament', 'cyberpress' ),
                            )
                        ),
                )
            );

        Container::make( 'post_meta', esc_html__( 'Tournament Stage', 'cyberpress' ) )
            ->set_context( 'side' )
            ->set_priority( 'default' )
            ->where( 'post_type', '=', 'match' )
            ->add_fields(
                array(
                    Field::make( 'select', $global_prefix . '_match_tournament_stage', '' )
                        ->add_options( array( $this, 'get_tournament_stages' ) ),
                )
            );

        Container::make( 'post_meta', esc_html__( 'Match Information', 'cyberpress' ) )
            ->where( 'post_type', '=', 'tournament' )
            ->add_tab(
                esc_html__( 'Base', 'cyberpress' ),
                array(
                    Field::make( 'text', $global_prefix . '_tournament_organizer', esc_html__( 'Organizer:', 'cyberpress' ) )
                        ->set_attribute( 'placeholder', esc_attr__( 'Enter the name of organizer', 'cyberpress' ) )
                        ->set_width( 33.3 ),
                    Field::make( 'text', $global_prefix . '_tournament_type', esc_html__( 'Type:', 'cyberpress' ) )
                        ->set_attribute( 'placeholder', esc_attr__( 'Enter Type of Tournament', 'cyberpress' ) )
                        ->set_width( 33.3 ),
                    Field::make( 'text', $global_prefix . '_tournament_total_prize_pool', esc_html__( 'Total Prize Pool', 'cyberpress' ) )
                        ->set_attribute( 'placeholder', esc_attr__( 'Enter Total Prize Pool', 'cyberpress' ) )
                        ->set_width( 33.3 ),
                    Field::make( 'complex', $global_prefix . '_tournament_stages', esc_html__( 'Tournament Stages:', 'cyberpress' ) )
                        ->setup_labels(
                            array(
                                'plural_name'   => esc_html__( 'Stages', 'cyberpress' ),
                                'singular_name' => esc_html__( 'Stage', 'cyberpress' ),
                            )
                        )
                        ->add_fields(
                            array(
                                Field::make( 'text', 'stage', esc_html__( 'Stage', 'cyberpress' ) )
                                    ->set_attribute( 'placeholder', esc_html__( 'Enter Name of Stage', 'cyberpress' ) ),
                            )
                        ),
                )
            )
            ->add_tab(
                esc_html__( 'Media', 'cyberpress' ),
                array(
                    Field::make( 'media_gallery', $global_prefix . '_tournament_screenshots', esc_html__( 'Srceenshots:', 'cyberpress' ) )
                        ->set_type( array( 'image' ) ),
                    Field::make( 'complex', $global_prefix . '_tournament_videos', esc_html__( 'Videos:', 'cyberpress' ) )
                        ->setup_labels(
                            array(
                                'plural_name'   => esc_html__( 'Videos', 'cyberpress' ),
                                'singular_name' => esc_html__( 'Video', 'cyberpress' ),
                            )
                        )
                        ->add_fields(
                            array(
                                Field::make( 'oembed', 'video', '' )
                                    ->set_help_text( esc_html__( 'Enter YouTube/Vimeo/Twitch Video URL', 'cyberpress' ) ),
                            )
                        ),
                )
            );

        Container::make( 'post_meta', esc_html__( 'Tournament Game', 'cyberpress' ) )
            ->set_context( 'side' )
            ->set_priority( 'default' )
            ->where( 'post_type', '=', 'tournament' )
            ->add_fields(
                array(
                    Field::make( 'react_select', $global_prefix . '_tournament_game', '' )
                        ->add_options( $player_game_options_choises_array )
                        ->set_props(
                            array(
                                'placeholder' => esc_attr__( 'Choose game', 'cyberpress' ),
                            )
                        )
                        ->set_classes( 'tournament-games-selector' ),
                )
            );
        Container::make( 'post_meta', esc_html__( 'Tournament Date', 'cyberpress' ) )
            ->set_context( 'side' )
            ->set_priority( 'default' )
            ->where( 'post_type', '=', 'tournament' )
            ->add_fields(
                array(
                    Field::make( 'date', $global_prefix . '_tournament_start_date', esc_html__( 'Start date:', 'cyberpress' ) )
                        ->set_input_format( $date_format, $date_format )
                        ->set_storage_format( $date_format ),
                    Field::make( 'date', $global_prefix . '_tournament_end_date', esc_html__( 'End date:', 'cyberpress' ) )
                        ->set_input_format( $date_format, $date_format )
                        ->set_storage_format( $date_format ),
                )
            );

        // Reset the recursion guard flag.
        self::$is_registering_fields = false;
    }

    /**
     * Get Tournament Stages
     *
     * @return array
     */
    public function get_tournament_stages() {
        global $post;

        $tournament_stage_options_choises_array = array(
            0 => esc_html__( 'Choose stage of tournament', 'cyberpress' ),
        );
        /**
         * Get cyberpress_match_tournament. Formation of an array with the stages of the selected tournament.
         * In this method, the method carbon_get_post_meta is not available, so a low-level get_post_meta WordPress request is used.
         * A global variable $post is also not available.
         */
        if ( isset( $post ) ) {
            $cyberpress_match_tournament = get_post_meta( $post->ID, '_cyberpress_match_tournament', false );

            if ( $cyberpress_match_tournament ) {
                $cyberpress_match_stages = array();
                $cyberpress_match_meta   = get_post_meta( $cyberpress_match_tournament[0] );

                if ( $cyberpress_match_meta ) {
                    foreach ( $cyberpress_match_meta as $key => $meta ) {
                        if ( false !== strripos( $key, '_cyberpress_tournament_stages|stage' ) ) {
                            $cyberpress_match_stages[] = $meta[0];
                        }
                    }
                }

                $tournament_stage_options_choises_array = array_merge( $tournament_stage_options_choises_array, $cyberpress_match_stages );
            }

            return $tournament_stage_options_choises_array;
        }
    }

    /**
     * Add tabs for creating child CPT teams.
     *
     * @return string - layout.
     */
    public function add_post_tabs() {
        $post = null;
        // phpcs:ignore
        if ( isset( $_REQUEST['post'] ) && ! empty( $_REQUEST['post'] ) ) {
            // @codingStandardsIgnoreLine
            $post = get_post( $_REQUEST['post'] );
        }

        if ( null !== $post ) {
            if ( 'team' === $post->post_type ) {
                ob_start();
                $parent_post = wp_get_post_parent_id( $post->ID );
                $tab_class   = 'nav-tab';
                $tab_class  .= $parent_post ? '' : ' nav-tab-active';

                if ( 0 !== $parent_post ) {
                    $parent_post_id = $parent_post;
                } else {
                    $parent_post_id = $post->ID;
                }

                $children_posts = get_children(
                    array(
                        'post_parent' => $parent_post_id,
                        'post_type'   => 'team',
                        // @codingStandardsIgnoreLine
                        'numberposts' => -1,
                        'post_status' => 'any',
                    )
                );

                // special class for game logo.
                $logo_atts = array(
                    'class' => 'cyberpress-img-tabs',
                );

                // game field id for tab game logo output.
                /**
                 * Everything is very bad: if we use the carbon_get_post_meta method,
                 * then all other properties of the Carbon plugin do not correctly preserve the entities.
                 *
                 * TODO: delete get_post_meta and change to carbon_get_post_meta after fixed issue.
                 */
                $parent_team_game = get_post_meta( (int) $parent_post_id, '_cyberpress_team_game', false );
                if ( isset( $parent_team_game ) && is_array( $parent_team_game ) && ! empty( $parent_team_game ) ) {
                    $parent_team_game_id = $parent_team_game[0];
                } else {
                    $parent_team_game_id = 0;
                }
                ?>
                <div class="cyberpress_child-team">
                    <div class="nav-tab-wrapper wp-clearfix">
                        <a href="<?php echo esc_url( get_edit_post_link( $parent_post_id ) ); ?>" class="<?php echo esc_attr( $tab_class ); ?>">
                            <?php
                            // game logo output.
                            if ( $parent_team_game_id ) {
                                echo wp_get_attachment_image( get_post_thumbnail_id( $parent_team_game_id ), array( 20, 20 ), false, $logo_atts );
                            }
                            ?>
                            <?php esc_html_e( 'Team', 'cyberpress' ); ?>
                        </a>
                        <?php foreach ( $children_posts as $children_post ) : ?>
                            <?php
                            /**
                             * Everything is very bad: if we use the carbon_get_post_meta method,
                             * then all other properties of the Carbon plugin do not correctly preserve the entities.
                             *
                             * TODO: delete get_post_meta and change to carbon_get_post_meta after fixed issue.
                             */
                            $children_team_game = get_post_meta( (int) $children_post->ID, '_cyberpress_team_game', false );
                            if ( isset( $children_team_game ) && is_array( $children_team_game ) && ! empty( $children_team_game ) ) {
                                $children_team_game_id = $children_team_game[0];
                            } else {
                                $children_team_game_id = 0;
                            }
                            $tab_class  = 'nav-tab';
                            $tab_class .= ( $children_post->ID === $post->ID ) ? ' nav-tab-active' : '';
                            ?>
                            <a href="<?php echo esc_url( get_edit_post_link( $children_post->ID ) ); ?>" class="<?php echo esc_attr( $tab_class ); ?>">
                                <?php
                                // game logo output.
                                if ( $children_team_game_id ) {
                                    echo wp_get_attachment_image( get_post_thumbnail_id( $children_team_game_id ), array( 20, 20 ), false, $logo_atts );
                                }
                                ?>
                                <?php echo esc_html( $children_post->post_title ); ?>
                            </a>
                        <?php endforeach; ?>
                        <a href="#cyberpress_add-child-team" class="page-title-action cyberpress-action-title"><?php esc_html_e( 'Add New', 'cyberpress' ); ?></a>
                    </div>
                    <form class="card" id="cyberpress_add-child-team" action="<?php echo esc_url( admin_url( 'post-new.php?post_type=team' ) ); ?>" method="post">
                        <div class="cyberpress-row">
                            <div class="container-tab-field cyberpress-col cyberpress-col-6-of-12">
                                <span class="hndle"><?php echo esc_html__( 'Child Team Title', 'cyberpress' ) . ':'; ?></span>
                                <div class="regular-text-container">
                                    <input type="text" name="child_title" class="regular-text cyberpress_child-title" placeholder="<?php esc_attr_e( 'Please enter title for new Child Team', 'cyberpress' ); ?>">
                                </div>
                            </div>
                            <div class="container-tab-field cyberpress-col cyberpress-col-6-of-12">
                                <span class="hndle"><?php echo esc_html__( 'Game', 'cyberpress' ) . ':'; ?></span>
                                <div class="regular-text-container">
                                    <select class="cyberpress-select2 cyberpress-select2-posts-ajax cyberpress_child-game" name="child_game" data-post-type="game" placeholder="<?php esc_attr_e( 'Please select game', 'cyberpress' ); ?>">
                                        <?php foreach ( $this->get_posts_or_taxonomy( 'game' ) as $key => $game ) : ?>
                                            <option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $game ); ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                            </div>
                            <div class="container-tab-field cyberpress-col cyberpress-admin-add-child-item">
                                <input type="submit" class="button button-primary button-large" id="create_child_team_item" value="<?php esc_attr_e( 'Add New Child Team', 'cyberpress' ); ?>">
                            </div>
                            <input type="hidden" name="parent_post_id" value="<?php echo esc_attr( $parent_post_id ); ?>">
                            <?php wp_nonce_field( 'create-child-team-with-parent_' . $parent_post_id ); ?>
                        </div>
                    </form>
                </div>
                <?php
                // phpcs:ignore
                if ( isset( $_REQUEST['action'] ) && ! empty( $_REQUEST['action'] ) && 'cyberpress_add_post_tabs' === $_REQUEST['action'] ) {
                    // phpcs:ignore
                    echo json_encode( ob_get_clean() );
                    wp_die();
                }
                return ob_get_clean();
            }
        }

        return '';
    }

    /**
     * Action for creating CPT child teems
     */
    public function create_child_team() {
        // @codingStandardsIgnoreLine
        if ( isset( $_REQUEST['parent_post_id'] ) && ! empty( $_REQUEST['parent_post_id'] ) && isset( $_REQUEST['post_type'] ) && ! empty( $_REQUEST['post_type'] ) && 'team' === $_REQUEST['post_type'] && current_user_can( 'manage_options' ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'create-child-team-with-parent_' . sanitize_key( $_REQUEST['parent_post_id'] ) ) ) {
            // @codingStandardsIgnoreLine
            $parent_post_id = sanitize_key( wp_unslash( $_REQUEST['parent_post_id'] ) );
            // @codingStandardsIgnoreLine
            if ( isset( $_REQUEST['child_title'] ) && ! empty( $_REQUEST['child_title'] ) ) {
                // @codingStandardsIgnoreLine
                $child_title = sanitize_text_field( wp_unslash( $_REQUEST['child_title'] ) );
            }
            // @codingStandardsIgnoreLine
            if ( isset( $_REQUEST['child_game'] ) && ! empty( $_REQUEST['child_game'] ) ) {
                // @codingStandardsIgnoreLine
                $child_game = sanitize_text_field( wp_unslash( $_REQUEST['child_game'] ) );
            }
            if ( isset( $child_title ) ) {
                $new_post_args = array(
                    'post_title'  => $child_title,
                    'post_type'   => 'team',
                    'post_parent' => $parent_post_id,
                    'post_author' => get_current_user_id(),
                    'post_status' => 'publish',
                );

                $post_id = wp_insert_post( $new_post_args );

                if ( ! is_wp_error( $post_id ) ) {
                    carbon_set_post_meta( $post_id, 'cyberpress_team_game', $child_game );
                    $new_edit_post_link = get_edit_post_link( $post_id, '&' );
                    // phpcs:ignore
                    wp_redirect( $new_edit_post_link );
                    die();
                }
            }
        }
    }

    /**
     * Get all the teams
     *
     * @param string $post_or_taxonomy_type - The name of the taxonomy with which to work.
     * @param bool   $taxonomy - if true - taxonomy, else - post.
     * @return array page names with key value pair
     */
    public function get_posts_or_taxonomy( $post_or_taxonomy_type = 'post', $taxonomy = false ) {
        $pages_options = array(
            '' => esc_html__( 'Choose', 'cyberpress' ) . ' ' . $post_or_taxonomy_type,
        );
        if ( $taxonomy ) {
            $terms = get_terms(
                array(
                    'taxonomy'   => $post_or_taxonomy_type,
                    'hide_empty' => false,
                )
            );
            if ( $terms ) {
                foreach ( $terms as $term ) {
                    $pages_options[ $term->term_id ] = $term->name;
                }
            }
        } else {
            $args = array(
                'sort_order'       => 'ASC',
                'sort_column'      => 'post_title',
                'hierarchical'     => 1,
                'exclude'          => '',
                'include'          => '',
                // @codingStandardsIgnoreLine
                'numberposts'      => -1,
                'authors'          => '',
                'child_of'         => 0,
                'parent'           => -1,
                'exclude_tree'     => '',
                'number'           => '',
                'offset'           => 0,
                'post_type'        => $post_or_taxonomy_type,
                'post_status'      => 'publish',
                'suppress_filters' => true,
            );

            $pages = get_posts( $args );

            if ( $pages ) {
                foreach ( $pages as $page ) {
                    $pages_options[ $page->ID ] = $page->post_title;
                }
            }
        }

        wp_reset_postdata();
        return $pages_options;
    }

    /**
     * Disabling the Gutenberg editor all cyberpress post types.
     *
     * @param bool   $can_edit  Whether to use the Gutenberg editor.
     * @param string $post_type Name of WordPress post type.
     * @return bool  $can_edit
     */
    public function gutenberg_cant_edit_cyberpress_page_type( $can_edit, $post_type ) {
        $gutenberg_unsupported_types = array( 'game', 'player', 'team', 'match' );
        if ( in_array( $post_type, $gutenberg_unsupported_types, true ) ) {
            $can_edit = false;
        }
        return $can_edit;
    }

    /**
     * Functions for get Social Icon Options
     *
     * @return array - Social Icons
     */
    public function get_social_icons() {
        return array(
            ''              => esc_html__( 'Choose Social Icons', 'cyberpress' ),
            'twitch'        => esc_html__( 'Twitch', 'cyberpress' ),
            'vimeo'         => esc_html__( 'Vimeo', 'cyberpress' ),
            'youtube'       => esc_html__( 'Youtube', 'cyberpress' ),
            'facebook'      => esc_html__( 'Facebook', 'cyberpress' ),
            'google-plus'   => esc_html__( 'Google-plus', 'cyberpress' ),
            'twitter'       => esc_html__( 'Twitter', 'cyberpress' ),
            'pinterest'     => esc_html__( 'Pinterest', 'cyberpress' ),
            'instagram'     => esc_html__( 'Instagram', 'cyberpress' ),
            'linkedin'      => esc_html__( 'Linkedin', 'cyberpress' ),
            'behance'       => esc_html__( 'Behance', 'cyberpress' ),
            'odnoklassniki' => esc_html__( 'Odnoklassniki', 'cyberpress' ),
            'skype'         => esc_html__( 'Skype', 'cyberpress' ),
            'vk'            => esc_html__( 'VK', 'cyberpress' ),
            'steam'         => esc_html__( 'Steam', 'cyberpress' ),
            'bitbucket'     => esc_html__( 'Bitbucket', 'cyberpress' ),
            'dropbox'       => esc_html__( 'Dropbox', 'cyberpress' ),
            'dribbble'      => esc_html__( 'Dribbble', 'cyberpress' ),
            'deviantart'    => esc_html__( 'Deviantart', 'cyberpress' ),
            'flickr'        => esc_html__( 'Flickr', 'cyberpress' ),
            'foursquare'    => esc_html__( 'Foursquare', 'cyberpress' ),
            'github'        => esc_html__( 'Github', 'cyberpress' ),
            'medium'        => esc_html__( 'Medium', 'cyberpress' ),
            'paypal'        => esc_html__( 'PayPal', 'cyberpress' ),
            'reddit'        => esc_html__( 'Reddit', 'cyberpress' ),
            'soundcloud'    => esc_html__( 'SoundCloud', 'cyberpress' ),
            'slack'         => esc_html__( 'Slack', 'cyberpress' ),
            'tumblr'        => esc_html__( 'Tumblr', 'cyberpress' ),
            'wordpress'     => esc_html__( 'WordPress', 'cyberpress' ),
        );
    }
}
