// Screen 09 — Security method selector.
//
// The selector here decides which security flow to enter. Picking
// "Fingerprint" routes through `fingerprint::view_register` →
// `view_done`; the other methods are placeholders today and just
// continue the wizard.

pub mod fingerprint;

use ltk::
{
	Color, Corners, Element, Length,
	column, container, pressable, radio, row, scroll, spacer, text,
};

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

pub fn view( state: &State ) -> Element<Msg>
{
	let palette = ltk::theme_palette();

	let methods = column::<Msg>()
		.spacing( 0.0 )
		.push( method_row(
			&tr!( "security.pin_title" ),
			&tr!( "security.pin_sub" ),
			state.security_method == Some( SecurityMethod::Pin ),
			SecurityMethod::Pin,
		) )
		.push( method_row(
			&tr!( "security.fingerprint_title" ),
			&tr!( "security.fingerprint_sub" ),
			state.security_method == Some( SecurityMethod::Fingerprint ),
			SecurityMethod::Fingerprint,
		) )
		.push( method_row(
			&tr!( "security.face_title" ),
			&tr!( "security.face_sub" ),
			state.security_method == Some( SecurityMethod::Face ),
			SecurityMethod::Face,
		) )
		.push( method_row(
			&tr!( "security.pattern_title" ),
			&tr!( "security.pattern_sub" ),
			state.security_method == Some( SecurityMethod::Pattern ),
			SecurityMethod::Pattern,
		) );

	let info = widgets::info_card( None, &[ tr!( "security.card" ).to_string() ] );

	let cta = state.security_method.map( |_| Msg::Next );

	// 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!( "security.title" ),
			Some( &tr!( "security.subtitle" ) ),
		) )
		.push( methods )
		.push( info );

	let body = column::<Msg>()
		.padding( t::PAGE_PADDING_H )
		.spacing( t::SECTION_SPACING )
		.push( chrome::top_bar( Screen::SecuritySelector, true, state.mode ) )
		.push( scroll( content ) )
		.push( chrome::primary_cta( &tr!( "security.cta" ), cta ) )
		.push( chrome::skip_link( &tr!( "common.skip_for_now" ), Msg::SkipSecurity ) );

	let _ = palette;
	chrome::page( body )
}

fn method_row(
	title:    &str,
	subtitle: &str,
	selected: bool,
	method:   SecurityMethod,
) -> Element<Msg>
{
	let palette = ltk::theme_palette();

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

	let r = radio::<Msg>( selected )
		.on_select( Msg::SelectSecurityMethod( method ) )
		.height( t::control_height() );

	// Viewport-tied heights so short landscape surfaces fit all four rows.
	let body = row::<Msg>()
		.spacing( t::ITEM_SPACING )
		.push( labels )
		.push( spacer() )
		.push( r );

	let bg: Color = if selected { palette.surface } else { Color::TRANSPARENT };
	let bg_layer = container::<Msg>( body )
		.background( bg )
		.radius( Corners::all( t::CARD_RADIUS / 2.0 ) )
		.padding_h( 12.0 )
		.padding_v( Length::vh( 1.25 ).clamp( 4.0, 12.0 ) );

	pressable::<Msg>( bg_layer )
		.on_press( Msg::SelectSecurityMethod( method ) )
		.into()
}
