<?php
/**
 * Single entry block template - match loop items.
 *
 * @var $attributes - attributes
 *
 * @package cyberpress/templates
 */

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

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

$match                    = cyberpress()->get_the_match();
$match_url                = $match->get_url();
$match_title              = $match->get_title();
$match_game               = $match->get_game();
$match_participants_teams = $match->get_participants();
$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();
?>

<?php do_action( 'cyberpress_matches_before_loop', $attributes ); ?>

<li id="post-<?php echo esc_attr( $match->get_id() ); ?>" <?php $match->match_classes( 'cyberpress-col cyberpress-match' ); ?>>

    <?php do_action( 'cyberpress_matches_loop_before_title', $attributes ); ?>

    <?php if ( $match_title && $attributes['displayMatchTitle'] ) : ?>
        <h2 class="cyberpress-match-title">
            <a href="<?php echo esc_url( $match_url ); ?>">
                <?php echo esc_html( $match_title ); ?>
            </a>
        </h2>
    <?php endif; ?>

    <?php do_action( 'cyberpress_matches_loop_after_title', $attributes ); ?>

    <?php do_action( 'cyberpress_matches_loop_before_participants', $attributes ); ?>

    <?php if ( $match_participants_teams && count( $match_participants_teams ) >= 2 ) : ?>
        <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">
                <?php if ( $attributes['displayTeamLogo'] ) : ?>
                <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>
                <?php endif; ?>
                <?php if ( $attributes['displayTeamName'] ) : ?>
                    <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 endif; ?>
                <?php if ( ! empty( $players_1 ) && $attributes['displayTeamPlayers'] ) : ?>
                    <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">
                <a href="<?php echo esc_url( $match_url ); ?>">
                    <?php if ( $match_time_start && $attributes['displayMatchStartDate'] ) : ?>
                        <span class="cyberpress-match-participant-time-start">
                            <?php echo esc_html( $match_time_start ); ?>
                        </span>
                    <?php endif; ?>
                    <span class="cyberpress-match-points">
                        <?php if ( ( $participant_1['points'] || $participant_2['points'] ) && $attributes['displayMatchPoints'] ) : ?>
                            <?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>
                </a>
            </div>
            <div class="cyberpress-match-participant">
                <?php if ( $attributes['displayTeamLogo'] ) : ?>
                <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>
                <?php endif; ?>
                <?php if ( $attributes['displayTeamName'] ) : ?>
                    <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 endif; ?>
                <?php if ( ! empty( $players_2 ) && $attributes['displayTeamPlayers'] ) : ?>
                    <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 do_action( 'cyberpress_matches_loop_after_participants', $attributes ); ?>

    <?php do_action( 'cyberpress_matches_loop_before_info', $attributes ); ?>

    <?php if ( ( $match_game && $attributes['displayMetaGame'] ) || ( $match_time_start && $attributes['displayMetaMatchStartDate'] ) || ( $match_time_end && $attributes['displayMetaMatchEndDate'] ) || ( $match_tournament && $attributes['displayMetaTournament'] ) ) : ?>
        <ul class="cyberpress-match-info">
            <?php if ( $match_game && $attributes['displayMetaGame'] ) : ?>
                <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 && $attributes['displayMetaTournament'] ) : ?>
                <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 && $attributes['displayMetaTournamentStage'] ) : ?>
                    <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; ?>
            <?php if ( $match_time_start && $attributes['displayMetaMatchStartDate'] ) : ?>
                <li>
                    <?php
                    echo wp_kses(
                        // translators: %s start time.
                        sprintf( __( '<strong>Starts</strong>: %s', 'cyberpress' ), esc_html( $match_time_start ) ),
                        array(
                            'strong' => array(),
                        )
                    );
                    ?>
                </li>
            <?php endif; ?>
            <?php if ( $match_time_end && $attributes['displayMetaMatchEndDate'] ) : ?>
                <li>
                    <?php
                    echo wp_kses(
                        // translators: %s end time.
                        sprintf( __( '<strong>Ends</strong>: %s', 'cyberpress' ), esc_html( $match_time_end ) ),
                        array(
                            'strong' => array(),
                        )
                    );
                    ?>
                </li>
            <?php endif; ?>
        </ul>
    <?php endif; ?>

    <?php do_action( 'cyberpress_matches_loop_after_info', $attributes ); ?>

    <?php do_action( 'cyberpress_matches_loop_before_read_more', $attributes ); ?>

    <?php if ( $attributes['displayMetaReadMoreButton'] ) : ?>
        <div class="cyberpress-match-read-more">
            <a href="<?php echo esc_url( $match_url ); ?>" class="cyberpress-btn">
                <?php esc_html_e( 'Read More', 'cyberpress' ); ?>
            </a>
        </div>
    <?php endif; ?>

    <?php do_action( 'cyberpress_matches_loop_after_read_more', $attributes ); ?>

</li><!-- #post-## -->

<?php do_action( 'cyberpress_matches_after_loop', $attributes ); ?>
