<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Sabriel_Base {

	const OPTION_KEY = 'sabriel_ai_settings';

	private static $predefiniti_in_cache = [];

	private static $tutte_in_cache = null;

	public static function get_defaults() {
		$sab_locale = get_locale();
		if ( isset( self::$predefiniti_in_cache[ $sab_locale ] ) ) {
			return self::$predefiniti_in_cache[ $sab_locale ];
		}
		$chiavi_orfane = [
			'ai_provider'                          => 'gemini',
			'ai_model_gemini'                      => class_exists( 'Sabriel_AI_Provider_Gemini' ) ? Sabriel_AI_Provider_Gemini::DEFAULT_MODEL : 'gemini-3.1-flash-lite',
			'ai_model_openai'                      => class_exists( 'Sabriel_AI_Provider_OpenAI' ) ? Sabriel_AI_Provider_OpenAI::DEFAULT_MODEL : 'gpt-5.6-luna',
			'ai_model_claude'                      => class_exists( 'Sabriel_AI_Provider_Claude' ) ? Sabriel_AI_Provider_Claude::DEFAULT_MODEL : 'claude-haiku-4-5-20251001',
			'elimina_dati_alla_disinstallazione'    => false,
			'limite_msg_giornalieri'                => '',

			'orari_apertura'                        => [],

		];
		$fields_settings = class_exists( 'Sabriel_Module_Settings' ) ? Sabriel_Module_Settings::FIELDS : [];
		$fields_branding = class_exists( 'Sabriel_Module_Branding' ) ? Sabriel_Module_Branding::FIELDS : [];
		$predefiniti     = array_merge( $chiavi_orfane, $fields_settings, $fields_branding );

		if ( class_exists( 'Sabriel_Module_Settings' ) ) {
			$predefiniti['bot_lingua'] = Sabriel_Module_Settings::lingua_rilevata_da_wp();
		}

		self::$predefiniti_in_cache[ $sab_locale ] = $predefiniti;

		return $predefiniti;
	}

	public static function get( $key, $default = null ) {
		$settings = self::get_all();
		return array_key_exists( $key, $settings ) ? $settings[ $key ] : $default;
	}

	public static function has( $key ) {
		$saved = get_option( self::OPTION_KEY, [] );
		return is_array( $saved ) && array_key_exists( $key, $saved );
	}

	public static function update( $key, $value ) {
		return self::update_many( [ $key => $value ] );
	}

	public static function update_many( $coppie ) {
		if ( ! is_array( $coppie ) || ! $coppie ) {
			return false;
		}
		$settings = self::get_all();
		foreach ( $coppie as $chiave => $valore ) {
			$settings[ $chiave ] = $valore;
		}
		self::$tutte_in_cache = $settings;
		return update_option( self::OPTION_KEY, $settings );
	}

	public static function svuota_cache() {
		self::$tutte_in_cache      = null;
		self::$predefiniti_in_cache = [];
	}

	public static function get_all() {
		if ( is_array( self::$tutte_in_cache ) ) {
			return self::$tutte_in_cache;
		}
		$saved = get_option( self::OPTION_KEY, [] );
		if ( ! is_array( $saved ) ) {
			$saved = [];
		}
		self::$tutte_in_cache = wp_parse_args( $saved, self::get_defaults() );
		return self::$tutte_in_cache;
	}

	public static function icona_libro() {
		return '<svg width="13" height="13" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg" style="display:block;flex-shrink:0" aria-hidden="true"><path d="M3 4.2C3 3.5 3.5 3 4.2 3H9.5V16.5H4.2C3.5 16.5 3 16 3 15.3V4.2Z" fill="currentColor" opacity=".75"/><path d="M17 4.2C17 3.5 16.5 3 15.8 3H10.5V16.5H15.8C16.5 16.5 17 16 17 15.3V4.2Z" fill="currentColor"/></svg>';
	}

	public static function icona( $nome, $px = 17 ) {
		$forme = [
			'lucchetto'  => '<g transform="translate(0.0 0.4)"><path d="M7.6 10.2V7.9a4.4 4.4 0 0 1 8.8 0v2.3" stroke="#64748B" stroke-width="1.9" fill="none" stroke-linecap="round"/><rect x="4.9" y="10.1" width="14.2" height="10.6" rx="2.2" fill="#64748B"/><circle cx="12" cy="15.4" r="1.55" fill="#fff"/></g>',
			'razzo'       => '<g transform="translate(0.0 0.3)"><path d="M12 1.6c2.7 2.3 4.3 5.6 4.3 9.2v4.6H7.7v-4.6c0-3.6 1.6-6.9 4.3-9.2Z" fill="#fff" stroke="#CBD5E1" stroke-width="1.4"/><circle cx="12" cy="8.9" r="2.1" fill="#3B82F6"/><path d="M7.7 11.4 4.5 14.6c-.5.5-.8 1.2-.8 1.9v2.6l4-2.3v-5.4Z" fill="#EF4444"/><path d="m16.3 11.4 3.2 3.2c.5.5.8 1.2.8 1.9v2.6l-4-2.3v-5.4Z" fill="#EF4444"/><path d="M12 17.1c1.3 1.4 2 2.7 2 3.8 0 1.1-.9 1.9-2 1.9s-2-.8-2-1.9c0-1.1.7-2.4 2-3.8Z" fill="#F59E0B"/></g>',
			'robot'       => '<g transform="translate(0.0 1.9)"><rect x="11.1" y="2.6" width="1.8" height="3.2" rx=".9" fill="#94A3B8"/><circle cx="12" cy="2.4" r="1.6" fill="#3B82F6"/><rect x="1.2" y="9.6" width="2.2" height="4.6" rx="1.1" fill="#94A3B8"/><rect x="20.6" y="9.6" width="2.2" height="4.6" rx="1.1" fill="#94A3B8"/><rect x="3.3" y="5.6" width="17.4" height="13" rx="4" fill="#fff" stroke="#CBD5E1" stroke-width="1.4"/><circle cx="8.7" cy="11" r="1.8" fill="#3B82F6"/><circle cx="15.3" cy="11" r="1.8" fill="#3B82F6"/><rect x="8.4" y="14.6" width="7.2" height="1.8" rx=".9" fill="#CBD5E1"/></g>',
			'tavolozza'   => '<g transform="translate(-0.3 1.6)"><path d="M12 3.1c-5.2 0-9.4 3.7-9.4 8.3 0 4.6 4.2 8.3 9.4 8.3 1 0 1.8-.8 1.8-1.8 0-.5-.2-.9-.5-1.2-.3-.3-.5-.7-.5-1.2 0-1 .8-1.8 1.8-1.8h2c3 0 5.4-2.4 5.4-5.4 0-3.9-4.5-7.2-10-7.2Z" fill="#fff" stroke="#CBD5E1" stroke-width="1.4"/><circle cx="7.4" cy="10.6" r="1.5" fill="#EF4444"/><circle cx="11.1" cy="7.4" r="1.5" fill="#F59E0B"/><circle cx="15.4" cy="8.4" r="1.5" fill="#3B82F6"/><circle cx="7.2" cy="15" r="1.5" fill="#10B981"/></g>',
			'etichetta'   => '<path d="M11.5 3.1h7.4c1 0 1.9.8 1.9 1.9v7.4c0 .5-.2 1-.6 1.3l-6.5 6.5c-.7.7-1.9.7-2.6 0l-7.4-7.4c-.7-.7-.7-1.9 0-2.6l6.5-6.5c.3-.4.8-.6 1.3-.6Z" fill="#3B82F6"/><circle cx="16.5" cy="7.5" r="1.8" fill="#fff"/>',
			'lista'       => '<rect x="4" y="3.6" width="16" height="18" rx="2.6" fill="#fff" stroke="#CBD5E1" stroke-width="1.4"/><rect x="8.4" y="1.7" width="7.2" height="3.8" rx="1.6" fill="#94A3B8"/><rect x="7.4" y="9" width="9.2" height="1.6" rx=".8" fill="#94A3B8"/><rect x="7.4" y="12.6" width="9.2" height="1.6" rx=".8" fill="#CBD5E1"/><rect x="7.4" y="16.2" width="5.8" height="1.6" rx=".8" fill="#CBD5E1"/>',
			'cervello'    => '<g transform="translate(0.0 0.3)"><path d="M9.6 2.8A3.3 3.3 0 0 0 6.4 5.2a3.1 3.1 0 0 0-2.6 3.1c0 .5.1 1 .3 1.4a3.3 3.3 0 0 0-1 2.4c0 1 .4 1.9 1.1 2.5-.1.3-.1.5-.1.8a3.1 3.1 0 0 0 3.6 3.1 3.3 3.3 0 0 0 3.1 2.2c.6 0 1.2-.2 1.7-.5V3.6c-.5-.5-1.1-.8-1.9-.8Z" fill="#3B82F6"/><path d="M14.4 2.8a3.3 3.3 0 0 1 3.2 2.4 3.1 3.1 0 0 1 2.6 3.1c0 .5-.1 1-.3 1.4a3.3 3.3 0 0 1 1 2.4c0 1-.4 1.9-1.1 2.5.1.3.1.5.1.8a3.1 3.1 0 0 1-3.6 3.1 3.3 3.3 0 0 1-3.1 2.2c-.6 0-1.2-.2-1.7-.5V3.6c.5-.5 1.1-.8 1.9-.8Z" fill="#60A5FA"/><path d="M12 3.4v16.4" stroke="#fff" stroke-width="1.4" stroke-linecap="round"/><path d="M8.8 7.4c-1.8.9-1.8 2.9 0 3.8" stroke="#fff" stroke-width="1.4" fill="none" stroke-linecap="round"/><path d="M8.6 13c-1.6.8-1.6 2.6 0 3.3" stroke="#fff" stroke-width="1.4" fill="none" stroke-linecap="round"/><path d="M15.2 7.4c1.8.9 1.8 2.9 0 3.8" stroke="#fff" stroke-width="1.4" fill="none" stroke-linecap="round"/><path d="M15.4 13c1.6.8 1.6 2.6 0 3.3" stroke="#fff" stroke-width="1.4" fill="none" stroke-linecap="round"/></g>',
			'catena'      => '<path d="M10.4 13.6a3.7 3.7 0 0 1 0-5.2l2.8-2.8a3.7 3.7 0 0 1 5.2 5.2l-1.4 1.4" stroke="#3B82F6" stroke-width="2.1" fill="none" stroke-linecap="round"/><path d="M13.6 10.4a3.7 3.7 0 0 1 0 5.2l-2.8 2.8a3.7 3.7 0 0 1-5.2-5.2l1.4-1.4" stroke="#64748B" stroke-width="2.1" fill="none" stroke-linecap="round"/>',
			'grafico'     => '<g transform="translate(0.0 -1.9)"><rect x="4.6" y="11" width="3.6" height="8.2" rx="1" fill="#3B82F6"/><rect x="10.2" y="6.4" width="3.6" height="12.8" rx="1" fill="#F59E0B"/><rect x="15.8" y="13.4" width="3.6" height="5.8" rx="1" fill="#EF4444"/><rect x="2.4" y="19.2" width="19.2" height="2.2" rx="1.1" fill="#94A3B8"/></g>',
			'spunta'      => '<circle cx="12" cy="12" r="9.4" fill="#10B981"/><path d="m7.6 12.3 3 3 5.8-6.1" stroke="#fff" stroke-width="2.2" fill="none" stroke-linecap="round" stroke-linejoin="round"/>',
		];
		if ( ! isset( $forme[ $nome ] ) ) {
			return '';
		}
		$px = max( 10, min( 64, (int) $px ) );

		return '<svg width="' . $px . '" height="' . $px . '" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" style="display:inline-block;vertical-align:-0.22em;flex-shrink:0" aria-hidden="true" focusable="false">' . $forme[ $nome ] . '</svg>';
	}

	public static function bandiera_svg( $codice ) {
		$base = strtolower( substr( $codice, 0, 2 ) );
		$svg  = [
			'it' => '<svg viewBox="0 0 3 2"><rect width="1" height="2" fill="#009246"/><rect x="1" width="1" height="2" fill="#fff"/><rect x="2" width="1" height="2" fill="#CE2B37"/></svg>',
			'en' => '<svg viewBox="0 0 3 2"><rect width="3" height="2" fill="#00247d"/><rect y=".8" width="3" height=".4" fill="#fff"/><rect x="1.3" width=".4" height="2" fill="#fff"/><rect y=".9" width="3" height=".2" fill="#cf142b"/><rect x="1.4" width=".2" height="2" fill="#cf142b"/></svg>',
			'es' => '<svg viewBox="0 0 3 2"><rect width="3" height="2" fill="#AA151B"/><rect y=".5" width="3" height="1" fill="#F1BF00"/></svg>',
			'fr' => '<svg viewBox="0 0 3 2"><rect width="1" height="2" fill="#0055A4"/><rect x="1" width="1" height="2" fill="#fff"/><rect x="2" width="1" height="2" fill="#EF4135"/></svg>',
			'de' => '<svg viewBox="0 0 3 2"><rect width="3" height=".667" fill="#000"/><rect y=".667" width="3" height=".667" fill="#DD0000"/><rect y="1.333" width="3" height=".667" fill="#FFCE00"/></svg>',
			'pt' => '<svg viewBox="0 0 3 2"><rect width="1.2" height="2" fill="#046A38"/><rect x="1.2" width="1.8" height="2" fill="#DA291C"/></svg>',
			'nl' => '<svg viewBox="0 0 3 2"><rect width="3" height=".667" fill="#AE1C28"/><rect y=".667" width="3" height=".667" fill="#fff"/><rect y="1.333" width="3" height=".667" fill="#21468B"/></svg>',
			'ru' => '<svg viewBox="0 0 3 2"><rect width="3" height=".667" fill="#fff"/><rect y=".667" width="3" height=".667" fill="#0039A6"/><rect y="1.333" width="3" height=".667" fill="#D52B1E"/></svg>',
			'id' => '<svg viewBox="0 0 3 2"><rect width="3" height="1" fill="#CE1126"/><rect y="1" width="3" height="1" fill="#fff"/></svg>',
		];
		$corpo = $svg[ $base ] ?? '<svg viewBox="0 0 3 2"><rect width="3" height="2" fill="#ccc"/></svg>';

		return str_replace( '<svg viewBox', '<svg width="16" height="11" style="display:block;border-radius:2px;flex-shrink:0" viewBox', $corpo );
	}

	public static function toast_toggle_script() {
		?>
		<style>
		#sab-toast-contenitore{position:fixed;bottom:24px;right:24px;z-index:99999;display:flex;flex-direction:column;gap:8px;pointer-events:none}
		.sab-toast{background:#0d0d0d;color:#f2f2f2;font-size:12.5px;font-weight:600;padding:10px 16px;border-radius:8px;box-shadow:0 6px 20px rgba(0,0,0,.25);opacity:0;transform:translateY(8px);transition:opacity .25s ease,transform .25s ease;display:flex;align-items:center;gap:8px;white-space:nowrap}
		.sab-toast.sab-toast-mostra{opacity:1;transform:translateY(0)}
		.sab-toast-pallino{width:7px;height:7px;border-radius:50%;flex-shrink:0}
		</style>
		<div id="sab-toast-contenitore"></div>
		<script>
		(function(){
			var SAB_TXT_ON  = '<?php echo esc_js( __( 'enabled', 'sabriel-ai' ) ); ?>';
			var SAB_TXT_OFF = '<?php echo esc_js( __( 'disabled', 'sabriel-ai' ) ); ?>';
			document.addEventListener('change', function(e){
				var input = e.target;
				if ( ! input.matches || ! input.matches('.sab-toggle input[type="checkbox"]') ) { return; }
				var riga = input.closest('.sab-toggle-row, .sab-toggle-text') ? input.closest('label, div.sab-toggle-row') : null;
				var testoEl = riga ? riga.querySelector('.sab-toggle-text') : null;
				var etichetta = '';
				if ( testoEl && testoEl.childNodes.length && testoEl.childNodes[0].nodeType === 3 ) {
					etichetta = testoEl.childNodes[0].textContent.trim();
				}
				var contenitore = document.getElementById('sab-toast-contenitore');
				if ( ! contenitore ) { return; }
				var toast = document.createElement('div');
				toast.className = 'sab-toast';
				var pallino = document.createElement('span');
				pallino.className = 'sab-toast-pallino';
				pallino.style.background = input.checked ? '#4ade80' : '#888';
				toast.appendChild(pallino);
				var testo = document.createElement('span');
				testo.textContent = ( etichetta ? etichetta + ': ' : '' ) + ( input.checked ? SAB_TXT_ON : SAB_TXT_OFF );
				toast.appendChild(testo);
				contenitore.appendChild(toast);
				requestAnimationFrame(function(){ toast.classList.add('sab-toast-mostra'); });
				setTimeout(function(){
					toast.classList.remove('sab-toast-mostra');
					setTimeout(function(){ toast.remove(); }, 300);
				}, 2000);
			});
		})();
		</script>
		<?php
	}
}
