<?php
/**
 * Advanced Accordion — creative multi-item accordion with icons & accents.
 *
 * @package AuraBuilder
 */

declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

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

final class AdvancedAccordionWidget extends WidgetBase {

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

	public function get_title(): string {
		return 'Acordeão avançado';
	}

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

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

	public function get_defaults(): array {
		return array(
			'items'    => "Missão|Formar cidadãos críticos e solidários com excelência acadêmica.|🎯|#7c3aed\nVisão|Ser referência em educação integral na região.|👁|#2563eb\nValores|Respeito, responsabilidade, criatividade e colaboração.|💎|#059669",
			'style'    => 'modern',
			'multiple' => '0',
			'open_first' => '1',
		);
	}

	public function sanitize_settings( array $settings ): array {
		$style = sanitize_key( (string) ( $settings['style'] ?? 'modern' ) );
		if ( ! in_array( $style, array( 'modern', 'classic', 'bordered', 'soft' ), true ) ) {
			$style = 'modern';
		}
		$flag = static function ( $v, string $d = '0' ): string {
			if ( $v === null || $v === '' ) {
				return $d;
			}
			return ( $v === true || $v === 1 || $v === '1' || $v === 'true' || $v === 'yes' ) ? '1' : '0';
		};
		return array(
			'items'      => sanitize_textarea_field( (string) ( $settings['items'] ?? '' ) ),
			'style'      => $style,
			'multiple'   => $flag( $settings['multiple'] ?? null, '0' ),
			'open_first' => $flag( $settings['open_first'] ?? null, '1' ),
		);
	}

	/**
	 * @return list<array{title:string,content:string,icon:string,color:string}>
	 */
	public static function parse_items( string $raw ): array {
		$out   = array();
		$lines = preg_split( '/\r\n|\r|\n/', $raw ) ?: array();
		foreach ( $lines as $line ) {
			$line = trim( $line );
			if ( $line === '' ) {
				continue;
			}
			$p = explode( '|', $line );
			$color = trim( (string) ( $p[3] ?? '#7c3aed' ) );
			if ( ! preg_match( '/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/', $color ) ) {
				$color = '#7c3aed';
			}
			$out[] = array(
				'title'   => sanitize_text_field( trim( (string) ( $p[0] ?? '' ) ) ),
				'content' => sanitize_textarea_field( trim( (string) ( $p[1] ?? '' ) ) ),
				'icon'    => sanitize_text_field( trim( (string) ( $p[2] ?? '' ) ) ),
				'color'   => strtolower( $color ),
			);
		}
		return $out;
	}

	public function render( Node $node, array $context = array() ): string {
		$s     = $node->settings;
		$items = self::parse_items( (string) ( $s['items'] ?? '' ) );
		if ( ! $items ) {
			return ! empty( $context['editor'] )
				? '<div class="ab-adv-acc ab-empty">Acordeão avançado — Título|Conteúdo|Ícone|#cor</div>'
				: '';
		}
		$style      = esc_attr( (string) ( $s['style'] ?? 'modern' ) );
		$multiple   = ( $s['multiple'] ?? '0' ) === '1' ? '1' : '0';
		$open_first = ( $s['open_first'] ?? '1' ) === '1';

		$html = '<div class="ab-adv-acc ab-adv-acc--' . $style . '" data-ab-adv-acc="1" data-multiple="' . $multiple . '">';
		foreach ( $items as $i => $item ) {
			$open  = $open_first && $i === 0 ? ' is-open' : '';
			$color = esc_attr( $item['color'] );
			$html .= '<div class="ab-adv-acc__item' . $open . '" style="--ab-acc-accent:' . $color . '">';
			$html .= '<button type="button" class="ab-adv-acc__head">';
			if ( $item['icon'] !== '' ) {
				$html .= '<span class="ab-adv-acc__icon">' . esc_html( $item['icon'] ) . '</span>';
			}
			$html .= '<span class="ab-adv-acc__title">' . esc_html( $item['title'] ) . '</span>';
			$html .= '<span class="ab-adv-acc__chev" aria-hidden="true"></span>';
			$html .= '</button>';
			$html .= '<div class="ab-adv-acc__body"><div class="ab-adv-acc__body-inner">' . esc_html( $item['content'] ) . '</div></div>';
			$html .= '</div>';
		}
		$html .= '</div>';
		return $html;
	}
}
