// Screen 07-02 — Proton sign-in form.

use ltk::
{
	Element,
	column, scroll, text, text_edit,
};

use crate::app::Msg;
use crate::flow::Screen;
use crate::i18n::tr;
use crate::state::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 why_card = widgets::info_card(
		Some( &tr!( "proton.why_title" ) ),
		&[
			tr!( "proton.why_1" ).to_string(),
			tr!( "proton.why_2" ).to_string(),
			tr!( "proton.why_3" ).to_string(),
		],
	);

	let form_ready = !state.proton.full_name.trim().is_empty()
		&& !state.proton.email.trim().is_empty()
		&& state.proton.password.len() >= 8;

	let content = column::<Msg>()
		.spacing( t::ITEM_SPACING )
		.push( chrome::title_block(
			&tr!( "proton.title" ),
			Some( &tr!( "proton.subtitle" ) ),
		) )
		.push( field_label( &tr!( "proton.full_name" ), palette.text_primary ) )
		.push(
			text_edit::<Msg>( tr!( "proton.full_name_ph" ).to_string(), &state.proton.full_name )
				.on_change( Msg::ProtonFullName )
				.font_size( t::control_font_size() )
				.height( t::control_height() ),
		)
		.push( field_label( &tr!( "proton.email" ), palette.text_primary ) )
		.push(
			text_edit::<Msg>( tr!( "proton.email_ph" ).to_string(), &state.proton.email )
				.on_change( Msg::ProtonEmail )
				.font_size( t::control_font_size() )
				.height( t::control_height() ),
		)
		.push( field_label( &tr!( "proton.password" ), palette.text_primary ) )
		.push(
			text_edit::<Msg>( tr!( "proton.password_ph" ).to_string(), &state.proton.password )
				.on_change( Msg::ProtonPassword )
				.secure( !state.proton.show_password )
				.font_size( t::control_font_size() )
				.height( t::control_height() ),
		)
		.push(
			text( tr!( "proton.password_hint" ).to_string() )
				.size( t::SMALL_SIZE )
				.color( palette.text_secondary ),
		)
		.push( why_card )
		.push(
			text( tr!( "proton.legal" ).to_string() )
				.size( t::SMALL_SIZE )
				.color( palette.text_secondary )
				.align_center(),
		);

	let body = column::<Msg>()
		.padding( t::PAGE_PADDING_H )
		.spacing( t::ITEM_SPACING )
		.push( chrome::top_bar( Screen::DataStorageProton, true, state.mode ) )
		.push( scroll( content ) )
		.push( chrome::primary_cta(
			&tr!( "proton.submit" ),
			if form_ready { Some( Msg::ProtonSubmit ) } else { None },
		) )
		.push( chrome::skip_link( &tr!( "proton.skip" ), Msg::ProtonSkip ) );

	chrome::page( body )
}

fn field_label( label: &str, color: ltk::Color ) -> Element<Msg>
{
	text( label.to_string() ).size( t::BODY_SIZE ).color( color ).into()
}
