// Screen 09 — Fingerprint registration sub-flow.
//
// Two views: `view_register` is the live registration screen with the
// circular fingerprint glyph and the three numbered steps;
// `view_done` is the success acknowledgement with the option to
// register another finger.

use ltk::
{
	Element,
	column, row, scroll, spacer, text,
};

use crate::app::Msg;
use crate::assets;
use crate::flow::Screen;
use crate::i18n::tr;
use crate::state::State;
use crate::ui::chrome;
use crate::ui::theme as t;

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

	let glyph: Element<Msg> = assets::theme_icon::<Msg>( "safety/fingerprint", 96 )
		.unwrap_or_else( || spacer().into() );

	let steps = column::<Msg>()
		.spacing( t::TIGHT_SPACING )
		.push( numbered_step( 1, &tr!( "security.fingerprint_step_1" ) ) )
		.push( numbered_step( 2, &tr!( "security.fingerprint_step_2" ) ) )
		.push( numbered_step( 3, &tr!( "security.fingerprint_step_3" ) ) );

	let content = column::<Msg>()
		.spacing( t::SECTION_SPACING )
		.push( chrome::title_block(
			&tr!( "security.fingerprint_register_title" ),
			None,
		) )
		.push( glyph )
		.push(
			text( tr!( "security.fingerprint_register_hint" ).to_string() )
				.size( t::SUBTITLE_SIZE )
				.color( palette.text_secondary )
				.align_center(),
		)
		.push( steps );

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

	chrome::page( body )
}

pub fn view_done( state: &State ) -> Element<Msg>
{
	let check: Element<Msg> = assets::theme_icon::<Msg>( "feedback/success", 64 )
		.unwrap_or_else( || spacer().into() );

	// The inner spacer keeps the block bottom-anchored while it fits.
	let content = column::<Msg>()
		.spacing( t::SECTION_SPACING )
		.push( spacer() )
		.push( check )
		.push( chrome::title_block(
			&tr!( "security.fingerprint_done_title" ),
			Some( &tr!( "security.fingerprint_done_subtitle" ) ),
		) );

	let body = column::<Msg>()
		.padding( t::PAGE_PADDING_H )
		.spacing( t::SECTION_SPACING )
		.push( chrome::top_bar( Screen::SecurityFingerprintDone, true, state.mode ) )
		.push( scroll( content ) )
		.push( chrome::primary_cta(
			&tr!( "security.fingerprint_finish" ),
			Some( Msg::Next ),
		) )
		.push( chrome::skip_link(
			&tr!( "security.fingerprint_register_more" ),
			Msg::RegisterAnotherFingerprint,
		) );

	chrome::page( body )
}

fn numbered_step( n: u32, label: &str ) -> Element<Msg>
{
	let palette = ltk::theme_palette();
	row::<Msg>()
		.spacing( 12.0 )
		.padding( 12.0 )
		.push(
			text( format!( "{}", n ) )
				.size( t::BODY_SIZE )
				.color( palette.surface ),
		)
		.push(
			text( label.to_string() )
				.size( t::BODY_SIZE )
				.color( palette.text_primary ),
		)
		.into()
}

