// Screen 08 — Privacy & permissions.

use ltk::
{
	Element, Length,
	column, container, flex, row, scroll, separator, spacer, text, toggle,
};

use crate::app::Msg;
use crate::assets;
use crate::flow::Screen;
use crate::i18n::tr;
use crate::state::{ Permission, State };
use crate::ui::{ chrome, widgets };
use crate::ui::theme as t;

const PERM_ICON_PX: u32 = 24;

pub fn view( state: &State ) -> Element<Msg>
{
	let card = widgets::info_card(
		Some( &tr!( "permissions.card_title" ) ),
		&[
			tr!( "permissions.card_1" ).to_string(),
			tr!( "permissions.card_2" ).to_string(),
			tr!( "permissions.card_3" ).to_string(),
		],
	);

	let mut rows = column::<Msg>().spacing( 0.0 );
	let entries: [ ( &str, &str, Permission, &[ u8 ] ); 4 ] = [
		( "permissions.location_title",      "permissions.location_sub",      Permission::Location,      assets::PERM_LOCATION      ),
		( "permissions.microphone_title",    "permissions.microphone_sub",    Permission::Microphone,    assets::PERM_MICROPHONE    ),
		( "permissions.camera_title",        "permissions.camera_sub",        Permission::Camera,        assets::PERM_CAMERA        ),
		( "permissions.notifications_title", "permissions.notifications_sub", Permission::Notifications, assets::PERM_NOTIFICATIONS ),
	];
	let last = entries.len() - 1;
	for ( i, entry ) in entries.iter().enumerate()
	{
		let ( title_key, sub_key, perm, icon ) = *entry;
		rows = rows.push( perm_row(
			&tr!( title_key ),
			&tr!( sub_key ),
			state.permissions.get( perm ),
			perm,
			icon,
		) );
		if i != last
		{
			rows = rows.push( separator() );
		}
	}

	// The scroll claims the leftover height, keeping the CTA pinned below it.
	let content = column::<Msg>()
		.spacing( t::SECTION_SPACING )
		.push( chrome::title_block(
			&tr!( "permissions.title" ),
			Some( &tr!( "permissions.subtitle" ) ),
		) )
		.push( rows )
		.push( card );

	let body = column::<Msg>()
		.padding( t::PAGE_PADDING_H )
		.spacing( t::SECTION_SPACING )
		.push( chrome::top_bar( Screen::Permissions, true, state.mode ) )
		.push( scroll( content ) )
		.push( chrome::primary_cta( &tr!( "common.continue" ), Some( Msg::Next ) ) );

	chrome::page( body )
}

fn perm_row( title: &str, subtitle: &str, value: bool, p: Permission, icon: &[ u8 ] ) -> Element<Msg>
{
	let palette = ltk::theme_palette();

	let icon_el: Element<Msg> = assets::illustration_widget::<Msg>( icon, PERM_ICON_PX )
		.unwrap_or_else( || spacer().into() );

	let labels = column::<Msg>()
		.spacing( 2.0 )
		.push( text( title.to_string() ).size( t::BODY_SIZE ).color( palette.text_primary ).weight( 700 ) )
		.push( text( subtitle.to_string() ).size( t::SMALL_SIZE ).color( palette.text_secondary ) );

	// Viewport-tied heights so short landscape surfaces fit all four rows.
	let inner = row::<Msg>()
		.spacing( t::ITEM_SPACING )
		.push( icon_el )
		.push( flex( labels ) )
		.push(
			toggle::<Msg>( value )
				.on_toggle( Msg::TogglePermission( p ) )
				.height( t::control_height() ),
		);

	container::<Msg>( inner )
		.padding_h( 12.0 )
		.padding_v( Length::vh( 1.25 ).clamp( 4.0, 12.0 ) )
		.into()
}
