<?php
/**
 * Stats counter template.
 *
 * @package Rsmspark_Content_Widgets
 */

defined( 'ABSPATH' ) || exit;

if ( empty( $items ) ) {
	return;
}

$classes = array(
	'rsmspark-cw',
	'rsmspark-cw-stats',
	'rsmspark-cw-stats--cols-' . sanitize_html_class( (string) $columns ),
);

if ( ! empty( $animate ) ) {
	$classes[] = 'rsmspark-cw-stats--animate';
}
if ( ! empty( $has_charts ) ) {
	$classes[] = 'rsmspark-cw-stats--has-charts';
}

/**
 * Echo a sanitized color style attribute.
 *
 * @param string $color    Raw color value.
 * @param string $property CSS property name.
 * @return void
 */
$echo_stat_style = static function ( $color, $property = 'color' ) {
	$color = sanitize_hex_color( (string) $color );
	if ( $color ) {
		echo ' style="' . esc_attr( $property . ':' . $color ) . '"';
	}
};

/**
 * Render prefix / value / suffix line for a metric.
 *
 * @param array<string, mixed> $item       Metric row.
 * @param bool                 $animate    Whether count-up is enabled.
 * @param bool                 $chart_mode Centered chart label styling.
 * @return void
 */
$render_value_line = static function ( array $item, $animate, $chart_mode = false ) use ( $echo_stat_style ) {
	$raw_value = (string) ( $item['value'] ?? '' );
	$numeric   = is_numeric( str_replace( array( ',', ' ' ), '', $raw_value ) );
	$value_cls = $chart_mode ? 'rsmspark-cw-stats__value rsmspark-cw-stats__value--chart' : 'rsmspark-cw-stats__value';
	?>
	<p class="<?php echo esc_attr( $value_cls ); ?>"<?php $echo_stat_style( (string) ( $item['value_color'] ?? '' ), 'color' ); ?>>
		<?php if ( ! empty( $item['prefix'] ) ) : ?>
			<span class="rsmspark-cw-stats__prefix"<?php $echo_stat_style( (string) ( $item['prefix_color'] ?? '' ), 'color' ); ?>><?php echo esc_html( $item['prefix'] ); ?></span>
		<?php endif; ?>
		<span
			class="rsmspark-cw-stats__number"
			<?php if ( $animate && $numeric ) : ?>
				data-rsmspark-cw-counter="<?php echo esc_attr( preg_replace( '/[^0-9.\-]/', '', $raw_value ) ); ?>"
			<?php endif; ?>
		><?php echo esc_html( $raw_value ); ?></span>
		<?php if ( ! empty( $item['suffix'] ) ) : ?>
			<span class="rsmspark-cw-stats__suffix"<?php $echo_stat_style( (string) ( $item['suffix_color'] ?? '' ), 'color' ); ?>><?php echo esc_html( $item['suffix'] ); ?></span>
		<?php endif; ?>
	</p>
	<?php
};
?>
<section class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>" style="<?php echo esc_attr( $style_vars ); ?>" data-rsmspark-cw-stats-host<?php echo ! empty( $animate ) ? ' data-rsmspark-cw-stats' : ''; ?>>
	<?php if ( ! empty( $title ) ) : ?>
		<h2 class="rsmspark-cw-stats__title"><?php echo esc_html( $title ); ?></h2>
	<?php endif; ?>
	<div class="rsmspark-cw-stats__grid">
		<?php foreach ( $items as $item ) : ?>
			<?php
			$display = Rsmspark_Content_Widgets_Widget_Stats_Counter::sanitize_display( $item['display'] ?? 'number' );
			$percent = (float) ( $item['percent'] ?? Rsmspark_Content_Widgets_Widget_Stats_Counter::metric_percent( $item['value'] ?? '' ) );
			$radius  = (float) ( $item['radius'] ?? 54 );
			$is_chart = in_array( $display, array( 'pie', 'donut' ), true );
			?>
			<article class="rsmspark-cw-stats__item<?php echo $is_chart ? ' rsmspark-cw-stats__item--' . esc_attr( $display ) : ''; ?>">
				<?php if ( $is_chart ) : ?>
					<div class="rsmspark-cw-stats__chart">
						<svg class="rsmspark-cw-stats__chart-svg rsmspark-cw-stats__chart-svg--<?php echo esc_attr( $display ); ?>" viewBox="0 0 120 120" role="img" aria-label="<?php echo esc_attr( sprintf( '%s: %s%%', (string) ( $item['label'] ?? '' ), (string) $percent ) ); ?>">
							<?php if ( 'pie' === $display ) : ?>
								<circle class="rsmspark-cw-stats__pie-track" cx="60" cy="60" r="<?php echo esc_attr( (string) $radius ); ?>" />
								<path
									class="rsmspark-cw-stats__pie-fill"
									d="<?php echo esc_attr( (string) ( $item['pie_path'] ?? Rsmspark_Content_Widgets_Widget_Stats_Counter::pie_svg_path( $percent ) ) ); ?>"
									data-rsmspark-cw-pie="<?php echo esc_attr( (string) $percent ); ?>"
									data-rsmspark-cw-pie-r="<?php echo esc_attr( (string) $radius ); ?>"
									<?php $echo_stat_style( (string) ( $item['value_color'] ?? '' ), 'fill' ); ?>
								/>
							<?php else : ?>
								<circle class="rsmspark-cw-stats__ring-track" cx="60" cy="60" r="<?php echo esc_attr( (string) $radius ); ?>" />
								<circle
									class="rsmspark-cw-stats__ring-fill"
									cx="60"
									cy="60"
									r="<?php echo esc_attr( (string) $radius ); ?>"
									data-rsmspark-cw-ring="<?php echo esc_attr( (string) $percent ); ?>"
									<?php $echo_stat_style( (string) ( $item['value_color'] ?? '' ), 'stroke' ); ?>
								/>
							<?php endif; ?>
						</svg>
						<div class="rsmspark-cw-stats__chart-center">
							<?php $render_value_line( $item, ! empty( $animate ), true ); ?>
						</div>
					</div>
				<?php else : ?>
					<?php $render_value_line( $item, ! empty( $animate ), false ); ?>
				<?php endif; ?>
				<?php if ( ! empty( $item['label'] ) ) : ?>
					<p class="rsmspark-cw-stats__label"><?php echo esc_html( $item['label'] ); ?></p>
				<?php endif; ?>
			</article>
		<?php endforeach; ?>
	</div>
</section>
