//! `cargo run --example inputs`
//!
//! Shows a plain username field and a secure password field with a
//! built-in show / hide-password eye toggle. Tap the eye to flip
//! the bullets ↔ plaintext rendering — the buffer keeps its
//! wipe-on-drop guarantee either way. Tab / Shift+Tab moves focus
//! between fields. Enter or the Login button submits. Esc exits.
//!
//! NOTE: ltk is a Wayland layer-shell toolkit. This example requires a running
//! Wayland compositor (e.g. sway, labwc, or a full desktop session).

use ltk::{ App, Element, Keysym, ButtonVariant, button, column, text, text_edit, spacer };

// ── Messages ──────────────────────────────────────────────────────────────────

#[ derive( Clone ) ]
enum Message
{
	UsernameChanged( String ),
	PasswordChanged( String ),
	TogglePasswordVisibility,
	Submit,
}

// ── App state ─────────────────────────────────────────────────────────────────

struct InputsApp
{
	username:         String,
	password:         String,
	password_visible: bool,
	submitted:        bool,
}

impl InputsApp
{
	fn new() -> Self
	{
		Self
		{
			username:         String::new(),
			password:         String::new(),
			password_visible: false,
			submitted:        false,
		}
	}
}

// ── App trait ─────────────────────────────────────────────────────────────────

impl App for InputsApp
{
	type Message = Message;

	fn view( &self ) -> Element<Message>
	{
		let palette   = ltk::theme_palette();
		let primary   = palette.text_primary;
		let secondary = palette.text_secondary;

		let status = if self.submitted
		{
			format!( "Submitted as: {}", self.username )
		} else {
			String::from( "Fill in the fields and press Login" )
		};

		column::<Message>()
			.padding( 32.0 )
			.spacing( 16.0 )
			.max_width( 380.0 )
			.center_y( true )
			.push( text( "ltk — text input showcase" ).size( 24.0 ).color( primary ).align_center() )
			.push( text( status ).size( 14.0 ).color( secondary ).align_center() )
			.push( spacer() )
			.push(
				text_edit::<Message>( "Username", &self.username )
					.on_change( Message::UsernameChanged )
					.on_submit( Message::Submit ),
			)
			.push(
				text_edit::<Message>( "Password", &self.password )
					.on_change( Message::PasswordChanged )
					.on_submit( Message::Submit )
					.password_toggle( self.password_visible, Message::TogglePasswordVisibility ),
			)
			.push(
				button::<Message>( "Login" )
					.variant( ButtonVariant::Primary )
					.on_press( Message::Submit ),
			)
			.push( spacer() )
			.push(
				text( "Tab = next field  ·  Enter = submit  ·  Esc = quit" )
					.size( 12.0 )
					.color( secondary )
					.align_center(),
			)
			.into()
	}

	fn update( &mut self, msg: Message )
	{
		match msg
		{
			Message::UsernameChanged( v )      => self.username = v,
			Message::PasswordChanged( v )      => self.password = v,
			Message::TogglePasswordVisibility  => self.password_visible = !self.password_visible,
			Message::Submit                    => self.submitted = true,
		}
	}

	fn on_key( &mut self, keysym: Keysym ) -> Option<Message>
	{
		if keysym == Keysym::Escape
		{
			std::process::exit( 0 );
		}
		None
	}
}

// ── Entry point ───────────────────────────────────────────────────────────────

fn main()
{
	ltk::run( InputsApp::new() );
}
