<?php
/**
 * Text Animation — typing, fade, slide, gradient, bounce.
 *
 * @package AuraBuilder
 */

declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;


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

final class TextAnimationWidget extends WidgetBase {

	public function get_type(): string {
		return 'text_animation';
	}

	public function get_title(): string {
		return 'Animação de texto';
	}

	public function get_category(): string {
		return 'creative';
	}

	public function get_icon(): string {
		return 'text_path';
	}

	public function get_defaults(): array {
		return array(
			'prefix'  => 'Somos o ',
			'words'   => 'Colégio JM Monteiro|futuro|excelência|cuidado',
			'suffix'  => '',
			'effect'  => 'typing',
			'speed'   => '80',
			'pause'   => '2000',
			'tag'     => 'h2',
			'align'   => 'center',
		);
	}

	public function sanitize_settings( array $settings ): array {
		$effect = sanitize_key( (string) ( $settings['effect'] ?? 'typing' ) );
		if ( ! in_array( $effect, array( 'typing', 'fade', 'slide', 'bounce', 'gradient' ), true ) ) {
			$effect = 'typing';
		}
		$tag = sanitize_key( (string) ( $settings['tag'] ?? 'h2' ) );
		if ( ! in_array( $tag, array( 'h1', 'h2', 'h3', 'h4', 'p', 'div' ), true ) ) {
			$tag = 'h2';
		}
		$align = in_array( ( $settings['align'] ?? '' ), array( 'left', 'center', 'right' ), true )
			? (string) $settings['align']
			: 'center';
		return array(
			'prefix' => sanitize_text_field( (string) ( $settings['prefix'] ?? '' ) ),
			'words'  => sanitize_text_field( (string) ( $settings['words'] ?? '' ) ),
			'suffix' => sanitize_text_field( (string) ( $settings['suffix'] ?? '' ) ),
			'effect' => $effect,
			'speed'  => (string) max( 20, min( 300, (int) ( $settings['speed'] ?? 80 ) ) ),
			'pause'  => (string) max( 500, min( 8000, (int) ( $settings['pause'] ?? 2000 ) ) ),
			'tag'    => $tag,
			'align'  => $align,
		);
	}

	/**
	 * @return list<string>
	 */
	public static function parse_words( string $raw ): array {
		$parts = preg_split( '/\s*\|\s*/', $raw ) ?: array();
		$out   = array();
		foreach ( $parts as $p ) {
			$p = trim( $p );
			if ( $p !== '' ) {
				$out[] = $p;
			}
		}
		return $out;
	}

	public function render( Node $node, array $context = array() ): string {
		$s      = $node->settings;
		$prefix = (string) ( $s['prefix'] ?? '' );
		$suffix = (string) ( $s['suffix'] ?? '' );
		$words  = self::parse_words( (string) ( $s['words'] ?? '' ) );
		$effect = esc_attr( (string) ( $s['effect'] ?? 'typing' ) );
		$speed  = (int) ( $s['speed'] ?? 80 );
		$pause  = (int) ( $s['pause'] ?? 2000 );
		$tag    = (string) ( $s['tag'] ?? 'h2' );
		$align  = esc_attr( (string) ( $s['align'] ?? 'center' ) );

		if ( ! $words ) {
			return ! empty( $context['editor'] )
				? '<div class="ab-text-anim ab-empty">Animação de texto — palavras separadas por |</div>'
				: '';
		}

		$first = $words[0];
		$json  = Json::encode( array_values( $words ) );
		if ( ! is_string( $json ) ) {
			$json = '[]';
		}

		$html  = '<' . $tag . ' class="ab-text-anim ab-text-anim--' . $effect . ' ab-text-anim--' . $align . '" data-ab-text-anim="1" data-effect="' . $effect . '" data-speed="' . esc_attr( (string) $speed ) . '" data-pause="' . esc_attr( (string) $pause ) . '" data-words="' . esc_attr( $json ) . '">';
		if ( $prefix !== '' ) {
			$html .= '<span class="ab-text-anim__prefix">' . esc_html( $prefix ) . '</span>';
		}
		$html .= '<span class="ab-text-anim__word">' . esc_html( $first ) . '</span>';
		if ( $effect === 'typing' ) {
			$html .= '<span class="ab-text-anim__cursor" aria-hidden="true">|</span>';
		}
		if ( $suffix !== '' ) {
			$html .= '<span class="ab-text-anim__suffix">' . esc_html( $suffix ) . '</span>';
		}
		$html .= '</' . $tag . '>';
		return $html;
	}
}
