<?php
/**
 * Progress bar (used in wins/draws/losses progresses in players and teams).
 *
 * @var $args
 * @package    cyberpress/templates
 */

if ( ! defined( 'ABSPATH' ) || ! $args || empty( $args ) ) {
    exit;
}

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

$colors = array( 'green', 'grey', 'red' );
$total  = 0;

foreach ( $args as $item ) {
    $total += $item['value'] ? $item['value'] : 0;
}

if ( $total ) {
    ?>

    <div class="cyberpress-progress">
        <?php
        $count = 0;

        foreach ( $args as $item ) {
            $val     = isset( $item['value'] ) && $item['value'] ? $item['value'] : 0;
            $percent = 100 * $val / $total;
            $color   = isset( $item['color'] ) && $item['color'] ? $item['color'] : ( isset( $colors[ $count ] ) ? $colors[ $count ] : '#aaa' );
            ?>
            <div class="cyberpress-progress-item <?php echo esc_attr( 'cyberpress-progress-item-' . $color ); ?>" style="width: <?php echo esc_attr( $percent ); ?>%;"></div>
            <?php
            $count++;
        }
        ?>
    </div>
    <div class="cyberpress-progress-labels">
        <?php
        $count = 0;

        foreach ( $args as $item ) {
            $val           = isset( $item['value'] ) && $item['value'] ? $item['value'] : 0;
            $percent       = 100 * $val / $total;
            $percent_label = ceil( $percent * 10 ) / 10 . '%';
            $color         = isset( $item['color'] ) && $item['color'] ? $item['color'] : ( isset( $colors[ $count ] ) ? $colors[ $count ] : '#aaa' );
            ?>
            <div class="cyberpress-progress-label <?php echo esc_attr( 'cyberpress-progress-label-' . $color ); ?>">
                <?php echo esc_html( isset( $item['label'] ) ? $item['label'] : '' ); ?>
                <?php echo esc_html( $percent_label ); ?>
            </div>
            <?php
            $count++;
        }
        ?>
    </div>
    <?php
}
