<?php
/**
 * Output of the single match participants.
 *
 * @package cyberpress/templates
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$match                    = cyberpress()->get_the_match();
$match_participants_teams = $match->get_participants();
$print_participants       = $match_participants_teams && count( $match_participants_teams ) >= 2;
$match_game               = $match->get_game();
$match_time_start         = $match->get_time_start_formatted();
$match_time_end           = $match->get_time_end_formatted();
$match_tournament         = $match->get_tournament();
$match_tournament_stage   = $match->get_tournament_stage();

if ( $print_participants || $match_game || $match_tournament ) : ?>
    <div class="cyberpress-match">
        <?php if ( $print_participants ) : ?>
            <div class="cyberpress-match-participants">
                <?php
                $count_participants = 0;
                $participant_1      = $match_participants_teams[0];
                $participant_2      = $match_participants_teams[1];

                $participant_1_country = $participant_1['team']->get_country_of_foundation();
                $participant_2_country = $participant_2['team']->get_country_of_foundation();

                $players_1 = $participant_1['players'];
                $players_2 = $participant_2['players'];
                ?>
                <div class="cyberpress-match-participant">
                    <div class="cyberpress-match-participant-thumbnail">
                        <a href="<?php echo esc_url( $participant_1['team']->get_url() ); ?>">
                            <?php echo wp_kses( $participant_1['team']->get_thumbnail(), cyberpress()->get_kses_allowed_img() ); ?>
                        </a>
                    </div>
                    <h2 class="cyberpress-match-participant-title">
                        <a href="<?php echo esc_url( $participant_1['team']->get_url() ); ?>">
                            <?php echo esc_html( $participant_1['team']->get_title() ); ?>
                            <?php if ( $participant_1_country && $participant_1_country['flag'] ) : ?>
                                <?php
                                echo wp_kses( $participant_1_country['flag'], cyberpress()->get_kses_allowed_img() );
                                ?>
                            <?php endif; ?>
                        </a>
                    </h2>
                    <?php if ( ! empty( $players_1 ) ) : ?>
                        <div class="cyberpress-match-participant-players">
                            <h3><?php echo esc_html__( 'Players:', 'cyberpress' ); ?></h3>
                            <ul>
                                <?php foreach ( $players_1 as $player ) : ?>
                                    <li>
                                        <a class="cyberpress-match-participant-players-player" href="<?php echo esc_url( $player->get_url() ); ?>">
                                            <?php echo wp_kses( $player->get_thumbnail( 'thumbnail' ), cyberpress()->get_kses_allowed_img() ); ?>
                                            <span><?php echo esc_html( $player->get_title() ); ?></span>
                                        </a>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                    <?php endif; ?>
                </div>
                <div class="cyberpress-match-participant-vs">
                    <?php if ( $match_time_start ) : ?>
                        <div class="cyberpress-match-participant-time-start">
                            <?php echo esc_html( $match_time_start ); ?>
                        </div>
                    <?php endif; ?>
                    <span class="cyberpress-match-points">
                        <?php if ( $participant_1['points'] || $participant_2['points'] ) : ?>
                            <?php echo esc_html( $participant_1['points'] ); ?> : <?php echo esc_html( $participant_2['points'] ); ?>
                        <?php else : ?>
                            <?php echo esc_html__( 'VS', 'cyberpress' ); ?>
                        <?php endif; ?>
                    </span>
                </div>
                <div class="cyberpress-match-participant">
                    <div class="cyberpress-match-participant-thumbnail">
                        <a href="<?php echo esc_url( $participant_2['team']->get_url() ); ?>">
                            <?php echo wp_kses( $participant_2['team']->get_thumbnail(), cyberpress()->get_kses_allowed_img() ); ?>
                        </a>
                    </div>
                    <h2 class="cyberpress-match-participant-title">
                        <a href="<?php echo esc_url( $participant_2['team']->get_url() ); ?>">
                            <?php echo esc_html( $participant_2['team']->get_title() ); ?>
                            <?php if ( $participant_2_country && $participant_2_country['flag'] ) : ?>
                                <?php
                                echo wp_kses( $participant_2_country['flag'], cyberpress()->get_kses_allowed_img() );
                                ?>
                            <?php endif; ?>
                        </a>
                    </h2>
                    <?php if ( ! empty( $players_2 ) ) : ?>
                        <div class="cyberpress-match-participant-players">
                            <h3><?php echo esc_html__( 'Players:', 'cyberpress' ); ?></h3>
                            <ul>
                                <?php foreach ( $players_2 as $player ) : ?>
                                    <li>
                                        <a class="cyberpress-match-participant-players-player" href="<?php echo esc_url( $player->get_url() ); ?>">
                                            <?php echo wp_kses( $player->get_thumbnail( 'thumbnail' ), cyberpress()->get_kses_allowed_img() ); ?>
                                            <span><?php echo esc_html( $player->get_title() ); ?></span>
                                        </a>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        <?php endif; ?>

        <?php if ( $match_game || $match_tournament ) : ?>
            <ul class="cyberpress-match-info">
                <?php if ( $match_game ) : ?>
                    <li>
                        <?php
                        echo wp_kses_post(
                            sprintf(
                                // translators: %1$s - game url, %2$s - game title.
                                __( '<strong>Game</strong>: <a href="%1$s" class="cyberpress-game-inline-link">%2$s</a>', 'cyberpress' ),
                                esc_url( $match_game->get_url() ),
                                $match_game->get_thumbnail() . ' ' . esc_html( $match_game->get_title() )
                            )
                        );
                        ?>
                    </li>
                <?php endif; ?>
                <?php if ( $match_tournament ) : ?>
                    <li>
                        <?php
                        echo wp_kses_post(
                            sprintf(
                                // translators: %1$s - tournament url, %2$s - tournament title.
                                __( '<strong>Tournament</strong>: <a href="%1$s" class="cyberpress-tournament-inline-link">%2$s</a>', 'cyberpress' ),
                                esc_url( $match_tournament->get_url() ),
                                esc_html( $match_tournament->get_title() )
                            )
                        );
                        ?>
                    </li>
                    <?php if ( $match_tournament_stage ) : ?>
                        <li>
                            <?php
                            echo wp_kses(
                                // translators: %s tournament stage name.
                                sprintf( __( '<strong>Tournament Stage</strong>: %s', 'cyberpress' ), esc_html( $match_tournament_stage ) ),
                                array(
                                    'strong' => array(),
                                )
                            );
                            ?>
                        </li>
                    <?php endif; ?>
                <?php endif; ?>
            </ul>
        <?php endif; ?>
    </div>
<?php endif; ?>
