<?php
/**
 * Social Bar — branded social icons with shapes and hover.
 *
 * @package AuraBuilder
 */

declare(strict_types=1);

namespace AuraBuilder\Widgets\Basic;

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

final class SocialBarWidget extends WidgetBase {

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

	public function get_title(): string {
		return 'Barra social';
	}

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

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

	public function get_defaults(): array {
		return array(
			'items'      => "Facebook|https://facebook.com\nInstagram|https://instagram.com\nYouTube|https://youtube.com\nWhatsApp|https://wa.me/\nLinkedIn|https://linkedin.com",
			'direction'  => 'row',
			'shape'      => 'circle',
			'show_label' => '0',
			'align'      => 'center',
			'size'       => '44',
		);
	}

	public function sanitize_settings( array $settings ): array {
		$dir = sanitize_key( (string) ( $settings['direction'] ?? 'row' ) );
		if ( ! in_array( $dir, array( 'row', 'column' ), true ) ) {
			$dir = 'row';
		}
		$shape = sanitize_key( (string) ( $settings['shape'] ?? 'circle' ) );
		if ( ! in_array( $shape, array( 'circle', 'rounded', 'square' ), true ) ) {
			$shape = 'circle';
		}
		$align = in_array( ( $settings['align'] ?? '' ), array( 'left', 'center', 'right' ), true )
			? (string) $settings['align']
			: 'center';
		$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'] ?? '' ) ),
			'direction'  => $dir,
			'shape'      => $shape,
			'show_label' => $flag( $settings['show_label'] ?? null, '0' ),
			'align'      => $align,
			'size'       => (string) max( 28, min( 72, (int) ( $settings['size'] ?? 44 ) ) ),
		);
	}

	/**
	 * @return list<array{network:string,url:string,label:string,slug: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 );
			$network = sanitize_text_field( trim( (string) ( $p[0] ?? '' ) ) );
			if ( $network === '' ) {
				continue;
			}
			$url   = esc_url_raw( trim( (string) ( $p[1] ?? '#' ) ) );
			$label = sanitize_text_field( trim( (string) ( $p[2] ?? $network ) ) );
			$slug  = sanitize_title( $network );
			$out[] = array(
				'network' => $network,
				'url'     => $url !== '' ? $url : '#',
				'label'   => $label !== '' ? $label : $network,
				'slug'    => $slug !== '' ? $slug : 'link',
			);
		}
		return $out;
	}

	/** Initial letter badge for known networks. */
	private static function badge( string $network ): string {
		$map = array(
			'facebook'  => 'f',
			'instagram' => 'ig',
			'youtube'   => '▶',
			'twitter'   => '𝕏',
			'x'         => '𝕏',
			'linkedin'  => 'in',
			'whatsapp'  => 'wa',
			'tiktok'    => '♪',
			'pinterest' => 'p',
			'email'     => '@',
			'telegram'  => 'tg',
		);
		$key = strtolower( $network );
		return $map[ $key ] ?? mb_strtoupper( mb_substr( $network, 0, 1 ) );
	}

	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-social-bar ab-empty">Barra social — Rede|URL por linha</div>'
				: '';
		}
		$dir    = esc_attr( (string) ( $s['direction'] ?? 'row' ) );
		$shape  = esc_attr( (string) ( $s['shape'] ?? 'circle' ) );
		$align  = esc_attr( (string) ( $s['align'] ?? 'center' ) );
		$size   = (int) ( $s['size'] ?? 44 );
		$labels = ( $s['show_label'] ?? '0' ) === '1';

		$html = '<div class="ab-social-bar ab-social-bar--' . $dir . ' ab-social-bar--' . $shape . ' ab-social-bar--' . $align . '" style="--ab-sb-size:' . esc_attr( (string) $size ) . 'px" data-ab-social-bar="1">';
		foreach ( $items as $item ) {
			$slug = esc_attr( $item['slug'] );
			$html .= '<a class="ab-social-bar__item ab-social-bar__item--' . $slug . '" href="' . esc_url( $item['url'] ) . '" target="_blank" rel="noopener noreferrer" title="' . esc_attr( $item['label'] ) . '">';
			$html .= '<span class="ab-social-bar__badge">' . esc_html( self::badge( $item['network'] ) ) . '</span>';
			if ( $labels ) {
				$html .= '<span class="ab-social-bar__label">' . esc_html( $item['label'] ) . '</span>';
			}
			$html .= '</a>';
		}
		$html .= '</div>';
		return $html;
	}
}
