//! App info overlay.
//!
//! Shown when the user picks "Información" from the edit-mode context
//! menu. Renders a small card with the app's icon, display name, stable
//! `app_id` and exec string. Read-only for phase 3 — later we may surface
//! size / permissions / last-used once a real metadata backend exists.

use std::sync::Arc;

use crate::message::Message;
use rust_i18n::t;
use crate::launcher::AppEntry;
use crate::launcher::icon_cache::IconCache;

pub struct AppInfo
{
	pub app_id: String,
}

impl AppInfo
{
	/// Build the overlay view. Resolves `app_id` against the launcher's
	/// scanned app list at view-build time so a reinstall / rescan is
	/// reflected immediately without a per-overlay cache.
	pub fn view(
		&self,
		all_apps:   &[AppEntry],
		icon_cache: &mut IconCache,
	) -> ltk::Element<Message>
	{
		let palette = ltk::theme_palette();

		let Some( app ) = all_apps.iter().find( |a| a.app_id == self.app_id ) else
		{
			// App was uninstalled between opening the menu and picking
			// "Información" — render a minimal fallback rather than an
			// empty overlay so the user can dismiss cleanly.
			return fallback_view( &self.app_id, palette );
		};

		// Icon — large enough to feel like a header element. Fall back to
		// a blank square if the icon path doesn't load; the decoded buffer
		// is shared with the launcher / ghost overlay via the icon cache.
		let icon: ltk::Element<Message> = match app.icon.as_ref()
			.and_then( |p| icon_cache.get_or_load_sized( p, 72 ) )
		{
			Some( ( rgba, w, h ) ) => ltk::img_widget( rgba, w, h )
				.size( 72.0, 72.0 )
				.into(),
			None => ltk::img_widget( Arc::new( vec![ 0u8; 4 ] ), 1, 1 )
				.size( 72.0, 72.0 )
				.into(),
		};

		let panel = ltk::container(
			ltk::column::<Message>()
				.padding( 16.0 )
				.spacing( 10.0 )
				.align_center_x( true )
				.push( icon )
				.push(
					ltk::text( &app.name )
						.size( 18.0 )
						.color( palette.text_primary )
						.align_center(),
				)
				.push(
					ltk::text( t!( "appinfo_app_id", id = app.app_id.clone() ).to_string() )
						.size( 12.0 )
						.color( palette.text_secondary )
						.align_center(),
				)
				.push(
					ltk::text( t!( "appinfo_executable", exec = app.exec.clone() ).to_string() )
						.size( 12.0 )
						.color( palette.text_secondary )
						.align_center(),
				)
				.push( ltk::separator() )
				.push(
					ltk::button::<Message>( t!( "appinfo_close" ).to_string() )
						.variant( ltk::ButtonVariant::Tertiary )
						.on_press( Message::CloseAppInfo ),
				),
		)
			.background( palette.surface )
			.padding( 4.0 )
			.radius( 14.0 );

		// Centre the panel on the surface. `column()` + flanking spacers
		// matches the power menu's pattern so the look stays consistent.
		ltk::column::<Message>()
			.padding( 0.0 )
			.spacing( 0.0 )
			.align_center_x( true )
			.push( ltk::spacer() )
			.push( panel )
			.push( ltk::spacer() )
			.into()
	}
}

fn fallback_view( app_id: &str, palette: ltk::Palette ) -> ltk::Element<Message>
{
	let panel = ltk::container(
		ltk::column::<Message>()
			.padding( 16.0 )
			.spacing( 8.0 )
			.align_center_x( true )
			.push(
				ltk::text( t!( "appinfo_not_found" ).to_string() )
					.size( 16.0 )
					.color( palette.text_primary )
					.align_center(),
			)
			.push(
				ltk::text( t!( "appinfo_app_id", id = app_id.to_string() ).to_string() )
					.size( 12.0 )
					.color( palette.text_secondary )
					.align_center(),
			)
			.push(
				ltk::button::<Message>( t!( "appinfo_close" ).to_string() )
					.variant( ltk::ButtonVariant::Tertiary )
					.on_press( Message::CloseAppInfo ),
			),
	)
		.background( palette.surface )
		.padding( 4.0 )
		.radius( 14.0 );

	ltk::column::<Message>()
		.padding( 0.0 )
		.spacing( 0.0 )
		.align_center_x( true )
		.push( ltk::spacer() )
		.push( panel )
		.push( ltk::spacer() )
		.into()
}
