<?php
declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

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

final class HeadingWidget extends WidgetBase {

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

	public function get_title(): string {
		return 'Título';
	}

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

	public function get_defaults(): array {
		return array(
			'text'  => 'Novo título',
			'tag'   => 'h2',
			'align' => 'left',
			'link'  => '',
			'size'  => '',
		);
	}

	public function sanitize_settings( array $settings ): array {
		$tag = isset( $settings['tag'] ) ? strtolower( (string) $settings['tag'] ) : 'h2';
		if ( ! in_array( $tag, array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'p' ), true ) ) {
			$tag = 'h2';
		}
		$align = isset( $settings['align'] ) ? (string) $settings['align'] : 'left';
		if ( ! in_array( $align, array( 'left', 'center', 'right', 'justify' ), true ) ) {
			$align = 'left';
		}
		$size = sanitize_key( (string) ( $settings['size'] ?? '' ) );
		if ( ! in_array( $size, array( '', 'default', 'small', 'medium', 'large', 'xl', 'xxl' ), true ) ) {
			$size = '';
		}
		$out = array(
			'text'  => sanitize_text_field( (string) ( $settings['text'] ?? 'Novo título' ) ),
			'tag'   => $tag,
			'align' => $align,
			'link'  => esc_url_raw( (string) ( $settings['link'] ?? '' ) ),
			'size'  => $size === 'default' ? '' : $size,
		);
		if ( isset( $settings['display_name'] ) && is_string( $settings['display_name'] ) && $settings['display_name'] !== '' ) {
			$out['display_name'] = sanitize_text_field( $settings['display_name'] );
		}
		return $out;
	}

	public function render( Node $node, array $context = array() ): string {
		$s     = $node->settings;
		$tag   = (string) ( $s['tag'] ?? 'h2' );
		if ( ! in_array( $tag, array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'p' ), true ) ) {
			$tag = 'h2';
		}
		$align = (string) ( $s['align'] ?? 'left' );
		if ( ! in_array( $align, array( 'left', 'center', 'right', 'justify' ), true ) ) {
			$align = 'left';
		}
		$text = esc_html( wp_strip_all_tags( (string) ( $s['text'] ?? '' ) ) );

		// Size preset → inline font-size only if styles don't already set it (StyleEngine handles responsive).
		// Global tokens (SiteGlobals) cover h1–h3 via .ab-h* classes when no local size.
		$extra = array(
			'text-align:' . $align,
			'margin:0 0 .5em',
		);
		$tag_class = in_array( $tag, array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ), true ) ? ' ab-h' . substr( $tag, 1 ) : '';
		$size_map = array(
			'small'  => '1rem',
			'medium' => '1.25rem',
			'large'  => '1.75rem',
			'xl'     => '2.25rem',
			'xxl'    => '3rem',
		);
		$size = (string) ( $s['size'] ?? '' );
		$has_style_size = ! empty( $node->styles['desktop']['font_size'] )
			|| ! empty( $node->styles['tablet']['font_size'] )
			|| ! empty( $node->styles['mobile']['font_size'] );
		if ( ! $has_style_size && isset( $size_map[ $size ] ) ) {
			$extra[] = 'font-size:' . $size_map[ $size ];
		}

		$attr = $this->leaf_style_attr( $node, $extra );
		$html = '<' . $tag . ' class="ab-heading' . esc_attr( $tag_class ) . '" data-tag="' . esc_attr( $tag ) . '"' . $attr . '>' . $text . '</' . $tag . '>';

		$link = (string) ( $s['link'] ?? '' );
		if ( $link !== '' ) {
			$html = '<a class="ab-heading-link" href="' . esc_url( $link ) . '" style="color:inherit;text-decoration:none">' . $html . '</a>';
		}
		return $html;
	}
}
