<?php
/**
 * Output of the single player game stats.
 *
 * @package cyberpress/templates
 */

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

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

$player             = cyberpress()->get_the_player();
$player_games_stats = $player->get_games_stats();

if ( $player_games_stats && ! empty( $player_games_stats ) ) : ?>
    <h3><?php echo esc_html__( 'Games', 'cyberpress' ); ?></h3>

    <?php
    $table_headings = array(
        'thumbnail'  => array(
            'class' => 'cyberpress-table-item-small',
            'label' => '',
        ),
        'game'       => __( 'Game', 'cyberpress' ),
        'nickname'   => __( 'Nickname', 'cyberpress' ),
        'statistics' => __( 'Statistics', 'cyberpress' ),
    );

    $table_items = array();

    foreach ( $player_games_stats as $game_stats ) {
        $game      = $game_stats['game'];
        $nickname  = $game_stats['nickname'];
        $thumbnail = $game->get_thumbnail(
            'thumbnail',
            array(
                'class' => 'cyberpress-player-games-thumb',
            )
        );

        ob_start();
        do_action(
            'cyberpress_progress',
            array(
                array(
                    'label' => esc_html__( 'Wins', 'cyberpress' ),
                    'value' => $game_stats['wins'],
                ),
                array(
                    'label' => esc_html__( 'Draws', 'cyberpress' ),
                    'value' => $game_stats['draws'],
                ),
                array(
                    'label' => esc_html__( 'Losses', 'cyberpress' ),
                    'value' => $game_stats['losses'],
                ),
            )
        );
        $statistics = ob_get_clean();

        $table_items[] = array(
            'thumbnail'  => $thumbnail ? ( '
                <a href="' . esc_url( $game->get_url() ) . '">
                    ' . wp_kses( $thumbnail, cyberpress()->get_kses_allowed_img() ) . '
                </a>
            ' ) : '',
            'game'       => '
                <a href="' . esc_url( $game->get_url() ) . '">
                    ' . esc_html( $game->get_title() ) . '
                </a>
            ',
            'nickname'   => $nickname ? esc_html( $nickname ) : '',
            'statistics' => $statistics,
        );
    }

    CyberPress_Template_Parts::get_template(
        'global/table.php',
        array(
            'table_class'    => 'cyberpress-player-games',
            'table_headings' => $table_headings,
            'table_items'    => $table_items,
        )
    );
endif;
