use crate::ui::prelude::*;

fn home_wifi_row( state: &State ) -> Element<Msg>
{
	let mut it = with_chevron( list_item( tr!( "home.wifi" ) )
		.on_press( Msg::Open( Screen::Wifi ) ) );
	if state.wifi_on
	{
		if let Some( ssid ) = state.connected_ssid.as_deref()
		{
			it = it.subtitle( ssid );
		} else {
			it = it.subtitle( tr!( "common.on" ) );
		}
	}
	with_icon( it, "system/wifi-signal-full" ).into()
}

fn home_battery_row( state: &State ) -> Element<Msg>
{
	let label = match state.battery_pct
	{
		Some( p ) => format!( "{} ({}%)", tr!( "home.battery" ), p ),
		None      => tr!( "home.battery" ).to_string(),
	};
	let it = list_item( label ).on_press( Msg::Open( Screen::Battery ) );
	let it = with_icon( it, "energy/battery-full" );
	with_chevron( it ).into()
}

pub( crate ) fn view_home( state: &State ) -> Element<Msg>
{
	let q = state.search_query.trim().to_lowercase();

	// Flat entry table used both for search and grouped layout
	let entries: Vec<( String, &str, Screen )> = vec![
		( tr!( "home.about" ).into(),                "feedback/information",                    Screen::About              ),
		( tr!( "home.updater" ).into(),              "hardware/chip",                           Screen::SystemAppUpdater   ),
		( tr!( "home.security_status" ).into(),      "safety/administrator-shield",             Screen::SecurityStatus     ),
		( tr!( "home.wifi" ).into(),                 "system/wifi-signal-full",                 Screen::Wifi               ),
		( tr!( "home.bluetooth" ).into(),            "system/bluetooth-connected",              Screen::Bluetooth          ),
		( tr!( "home.cellular" ).into(),             "system/sim-card",                         Screen::Cellular           ),
		( tr!( "home.interconnectivity" ).into(),    "system/ethernet",                         Screen::Interconnectivity  ),
		( tr!( "home.hotspot" ).into(),              "system/hotspot-access-point",             Screen::PortableHotspot    ),
		( tr!( "home.more_connectivity" ).into(),    "general/right-circle",                    Screen::MoreConnectivity   ),
		( tr!( "home.lock_screen" ).into(),          "safety/lock-screen",                      Screen::LockScreen         ),
		( tr!( "home.notifications" ).into(),        "communication/notifications-status",      Screen::Notifications      ),
		( tr!( "home.display" ).into(),              "hardware/brightness-maximum",             Screen::Display            ),
		( tr!( "home.monitors" ).into(),             "hardware/brightness-maximum",             Screen::Monitors           ),
		( tr!( "home.sound" ).into(),                "hardware/speaker-high",                   Screen::Sound              ),
		( tr!( "home.wallpaper" ).into(),            "customisation/wallpaper",                 Screen::Wallpaper          ),
		( tr!( "home.themes" ).into(),               "customisation/colour-palette",            Screen::Themes             ),
		( tr!( "home.fingerprint" ).into(),          "safety/fingerprint",                      Screen::FingerprintAndLock ),
		( tr!( "home.privacy_protection" ).into(),   "safety/padlock",                          Screen::PrivacyProtection  ),
		( tr!( "home.apps" ).into(),                 "features/app-store",                      Screen::Apps               ),
		( tr!( "home.battery" ).into(),              "energy/battery-full",                     Screen::Battery            ),
		( tr!( "home.privacy" ).into(),              "safety/face-id",                          Screen::Privacy            ),
		( tr!( "home.location" ).into(),             "features/maps-gps",                       Screen::Location           ),
		( tr!( "home.storage" ).into(),              "hardware/harddrive",                      Screen::Storage            ),
		( tr!( "home.language" ).into(),             "accessibility/languages",                 Screen::Language           ),
		( tr!( "home.datetime" ).into(),             "features/clock",                          Screen::DateTime           ),
		( tr!( "home.accessibility" ).into(),        "accessibility/magnifier",                  Screen::Accessibility      ),
	];

	let list_content: Element<Msg> = if !q.is_empty()
	{
		let mut results = column::<Msg>().spacing( 2.0 );
		let mut found   = false;
		for ( label, icon_path, screen ) in &entries
		{
			if label.to_lowercase().contains( &q )
			{
				results = results.push( row_link!( label.as_str(), *icon_path, screen.clone() ) );
				found = true;
			}
		}
		if !found
		{
			results = results.push( t_sec!( tr!( "home.no_results" ) ).size( 14.0 ) );
		}
		scroll( results ).into()
	} else {
		scroll(
			column::<Msg>()
				.spacing( 2.0 )
				// ── Group 1 ─────────────────────────────────────
				.push( row_link!( tr!( "home.about" ),           "feedback/information",          Screen::About            ) )
				.push( row_link!( tr!( "home.updater" ),         "hardware/chip",                 Screen::SystemAppUpdater ) )
				.push( row_link!( tr!( "home.security_status" ), "safety/administrator-shield",   Screen::SecurityStatus   ) )
				.push( separator() )
				// ── Group 2 ─────────────────────────────────────
				.push( home_wifi_row( state ) )
				.push( row_link!( tr!( "home.bluetooth" ),         "system/bluetooth-connected",    Screen::Bluetooth        ) )
				.push( row_link!( tr!( "home.cellular" ),          "system/sim-card",               Screen::Cellular         ) )
				.push( row_link!( tr!( "home.interconnectivity" ), "system/ethernet",               Screen::Interconnectivity) )
				.push( row_link!( tr!( "home.hotspot" ),           "system/hotspot-access-point",   Screen::PortableHotspot  ) )
				.push( row_link!( tr!( "home.more_connectivity" ), "general/right-circle",          Screen::MoreConnectivity ) )
				.push( separator() )
				// ── Group 3 ─────────────────────────────────────
				.push( row_link!( tr!( "home.lock_screen" ),   "safety/lock-screen",                 Screen::LockScreen         ) )
				.push( row_link!( tr!( "home.notifications" ), "communication/notifications-status", Screen::Notifications      ) )
				.push( row_link!( tr!( "home.display" ),       "hardware/brightness-maximum",        Screen::Display            ) )
			.push( row_link!( tr!( "home.monitors" ),      "hardware/brightness-maximum",        Screen::Monitors           ) )
				.push( row_link!( tr!( "home.sound" ),         "hardware/speaker-high",              Screen::Sound              ) )
				.push( row_link!( tr!( "home.wallpaper" ),     "customisation/wallpaper",            Screen::Wallpaper          ) )
				.push( row_link!( tr!( "home.themes" ),        "customisation/colour-palette",       Screen::Themes             ) )
				.push( separator() )
				// ── Group 4 ─────────────────────────────────────
				.push( row_link!( tr!( "home.fingerprint" ),        "safety/fingerprint", Screen::FingerprintAndLock ) )
				.push( row_link!( tr!( "home.privacy_protection" ), "safety/padlock",     Screen::PrivacyProtection  ) )
				.push( row_link!( tr!( "home.apps" ),               "features/app-store", Screen::Apps               ) )
				.push( home_battery_row( state ) )
				.push( separator() )
				// ── Group 5 ─────────────────────────────────────
				.push( row_link!( tr!( "home.privacy" ),  "safety/face-id",   Screen::Privacy  ) )
				.push( row_link!( tr!( "home.location" ), "features/maps-gps", Screen::Location ) )
				.push( separator() )
				// ── Group 6 ─────────────────────────────────────
				.push( row_link!( tr!( "home.storage" ),       "hardware/harddrive",      Screen::Storage       ) )
				.push( row_link!( tr!( "home.language" ),      "accessibility/languages", Screen::Language      ) )
				.push( row_link!( tr!( "home.datetime" ),      "features/clock",          Screen::DateTime      ) )
				.push( row_link!( tr!( "home.accessibility" ), "accessibility/magnifier", Screen::Accessibility ) )
		).into()
	};

	column::<Msg>()
		.padding( 12.0 )
		.spacing( 12.0 )
		.align_center_x( false )
		.push( container( t!( tr!( "home.title" ) ).size( 28.0 ).weight( 700 ) ).padding_v( 8.0 ) )
		.push(
			text_edit( tr!( "home.search" ), &state.search_query )
				.on_change( Msg::SearchInput )
		)
		.push( list_content )
		.into()
}
