//! Home screen — the "Phone" surface with a Calls/Contacts pill switch,
//! a favorites strip, grouped recents, and a floating action cluster.

use ltk::
{
	column, container, row, scroll, spacer, text,
	stack, Element, HAlign, VAlign,
};

use crate::app::{ HomeTab, Message };
use crate::palette;
use crate::types::{ ContactRef, HistoryEntry };
use crate::views::components as ui;

pub fn view(
	tab:       HomeTab,
	favorites: &[ ContactRef ],
	recents:   &[ HistoryEntry ],
	contacts:  &[ ContactRef ],
	now_ms:    i64,
) -> Element<Message>
{
	let palette = ltk::theme_palette();

	let header = container::<Message>(
		row::<Message>()
			.spacing( palette::dp( 6.0 ) )
			.push( text( "Phone" ).size( palette::dp( 24.0 ) ).weight( 600 ).color( palette.text_primary ) )
			.push( spacer() )
			.push( container( ui::icon( "actions/search", 22.0, palette.text_primary ) ).padding( palette::dp( 9.0 ) ) )
			.push( container( ui::icon( "navigation/menu-kebab", 22.0, palette.text_primary ) ).padding( palette::dp( 9.0 ) ) )
	)
		.padding_h( palette::dp( 8.0 ) )
		.padding_top( palette::dp( 8.0 ) )
		.padding_bottom( palette::dp( 4.0 ) );

	let search = container::<Message>(
		container::<Message>(
			row::<Message>()
				.spacing( palette::dp( 12.0 ) )
				.push( ui::icon( "actions/search", 20.0, palette.text_secondary ) )
				.push( text( "Search contacts" ).size( palette::dp( 15.0 ) ).color( palette.text_secondary ) )
				.push( spacer() )
		)
			.background( palette::chip() )
			.radius( 100.0 )
			.padding_h( palette::dp( 18.0 ) )
			.padding_v( palette::dp( 14.0 ) )
	)
		.padding_h( palette::dp( 8.0 ) )
		.padding_bottom( palette::dp( 6.0 ) );

	let tabs = container::<Message>(
		row::<Message>()
			.spacing( palette::dp( 8.0 ) )
			.push( ui::pill_tab( "Calls",    tab == HomeTab::Calls,    Message::SelectHomeTab( HomeTab::Calls ) ) )
			.push( ui::pill_tab( "Contacts", tab == HomeTab::Contacts, Message::SelectHomeTab( HomeTab::Contacts ) ) )
			.push( spacer() )
	)
		.padding_h( palette::dp( 8.0 ) )
		.padding_bottom( palette::dp( 2.0 ) );

	let content = match tab
	{
		HomeTab::Calls    => calls_content( favorites, recents, now_ms ),
		HomeTab::Contacts => contacts_content( contacts ),
	};

	let body = column::<Message>()
		.push( header )
		.push( search )
		.push( tabs )
		.push( scroll( content ) );

	let root = container::<Message>(
		stack::<Message>()
			.push( body )
			.push_aligned_margin( fab_cluster( &palette ), HAlign::End, VAlign::Bottom, 16.0 )
	)
		.background( palette.surface );

	root.into()
}

fn calls_content( favorites: &[ ContactRef ], recents: &[ HistoryEntry ], now_ms: i64 ) -> Element<Message>
{
	let mut col = column::<Message>();

	if !favorites.is_empty()
	{
		col = col.push( ui::section_label( "Favorites" ) );

		let mut strip = row::<Message>().spacing( palette::dp( 8.0 ) );
		for c in favorites
		{
			strip = strip.push( ui::fav_bubble( c ) );
		}
		col = col.push(
			container( scroll( strip ).horizontal() )
				.padding_h( palette::dp( 8.0 ) )
				.padding_bottom( palette::dp( 4.0 ) )
		);
	}

	let mut last_bucket = "";
	for e in recents
	{
		let bucket = ui::day_bucket( now_ms, e.started_at_ms );
		if bucket != last_bucket
		{
			col = col.push( ui::section_label( bucket ) );
			last_bucket = bucket;
		}
		col = col.push( ui::recent_row( e, now_ms ) );
	}

	col.push( spacer().height( palette::dp( 120.0 ) ) ).into()
}

fn contacts_content( contacts: &[ ContactRef ] ) -> Element<Message>
{
	let mut sorted = contacts.to_vec();
	sorted.sort_by( |a, b| a.display_name.to_lowercase().cmp( &b.display_name.to_lowercase() ) );

	let mut col = column::<Message>()
		.push( ui::section_label( &format!( "{} contacts", sorted.len() ) ) );

	let mut last = '\0';
	for c in &sorted
	{
		let first = c.display_name.chars().next().unwrap_or( '#' ).to_ascii_uppercase();
		if first != last
		{
			col  = col.push( letter_header( first ) );
			last = first;
		}
		col = col.push( ui::contact_row( c ) );
	}

	col.push( spacer().height( palette::dp( 120.0 ) ) ).into()
}

fn letter_header( ch: char ) -> Element<Message>
{
	container::<Message>(
		text( ch.to_string() ).size( palette::dp( 14.0 ) ).weight( 600 ).color( ltk::theme_palette().accent )
	)
		.padding_left( palette::dp( 8.0 ) )
		.padding_top( palette::dp( 6.0 ) )
		.padding_bottom( palette::dp( 2.0 ) )
		.into()
}

fn fab_cluster( palette: &ltk::Palette ) -> Element<Message>
{
	let add = ltk::pressable(
		container::<Message>( ui::icon( "actions/add", 22.0, palette.text_primary ) )
			.background( palette.surface )
			.border( palette.divider, 1.0 )
			.radius( 18.0 )
			.padding( palette::dp( 15.0 ) )
	)
		.on_press( Message::OpenCreateContact );

	let dialpad = ltk::pressable(
		container::<Message>( ui::icon( "communication/dial-pad", 26.0, ui::ON_ACCENT ) )
			.background( palette.accent )
			.radius( 22.0 )
			.padding( palette::dp( 19.0 ) )
	)
		.on_press( Message::OpenDialpad );

	column::<Message>()
		.fit_content()
		.spacing( palette::dp( 10.0 ) )
		.push(
			row::<Message>()
				.spacing( palette::dp( 10.0 ) )
				.push( spacer() )
				.push( add )
				.push( dialpad )
		)
		.into()
}
