<?php
declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

use AuraBuilder\Document\Node;
use AuraBuilder\Widgets\WidgetBase;

final class ProgressBarWidget extends WidgetBase {
	public function get_type(): string { return 'progress_bar'; }
	public function get_title(): string { return 'Barra de progresso'; }
	public function get_icon(): string { return 'progress'; }
	public function get_defaults(): array {
		return array( 'title' => 'Progresso', 'percent' => '75', 'show_pct' => '1' );
	}
	public function sanitize_settings( array $settings ): array {
		return array(
			'title'    => sanitize_text_field( (string) ( $settings['title'] ?? '' ) ),
			'percent'  => (string) max( 0, min( 100, (int) ( $settings['percent'] ?? 0 ) ) ),
			'show_pct' => empty( $settings['show_pct'] ) || $settings['show_pct'] === '0' ? '0' : '1',
		);
	}
	public function render( Node $node, array $context = array() ): string {
		$s   = $node->settings;
		$pct = max( 0, min( 100, (int) ( $s['percent'] ?? 0 ) ) );
		$ttl = esc_html( (string) ( $s['title'] ?? '' ) );
		$lab = ( $s['show_pct'] ?? '1' ) !== '0' ? '<span class="ab-progress__pct">' . esc_html( (string) $pct ) . '%</span>' : '';
		return '<div class="ab-progress">'
			. '<div class="ab-progress__head"><span class="ab-progress__title">' . $ttl . '</span>' . $lab . '</div>'
			. '<div class="ab-progress__track"><div class="ab-progress__bar" style="width:' . esc_attr( (string) $pct ) . '%"></div></div>'
			. '</div>';
	}
}
