//! WiFi network and Bluetooth device pickers — modal-style overlay
//! sub-views that replace the QS panel content while one is open.
//! Each is a centred column with a header row (title + close X), a
//! separator, and either a `scroll(list_item)` of available entries
//! or a "no entries found" fallback text.

use crate::message::Message;
use crate::system::network::{ BluetoothDevice, WifiNetwork };
use rust_i18n::t;

pub( super ) fn wifi( networks: &[ WifiNetwork ] ) -> ltk::Element<Message>
{
	let palette = ltk::theme_palette();

	let mut panel = ltk::column::<Message>()
		.padding( 16.0 )
		.spacing( 4.0 )
		.max_width( 340.0 )
		.push(
			ltk::row::<Message>()
				.spacing( 8.0 )
				.push( ltk::text( t!( "qs_wifi_networks" ).to_string() ).size( 18.0 ).color( palette.text_primary ) )
				.push( ltk::spacer() )
				.push( ltk::button::<Message>( "X" ).variant( ltk::ButtonVariant::Tertiary ).on_press( Message::CloseQuickSettings ) ),
		)
		.push( ltk::separator() );

	if networks.is_empty()
	{
		panel = panel.push( ltk::text( t!( "qs_wifi_no_networks" ).to_string() ).size( 14.0 ).color( palette.text_secondary ) );
	}
	else
	{
		let mut list = ltk::column::<Message>().spacing( 0.0 );
		for net in networks
		{
			let signal = format!( "{}%", net.signal );
			let mut item = ltk::list_item::<Message>( &net.ssid ).trailing( &signal );
			if net.connected
			{
				item = item.subtitle( t!( "qs_status_connected" ).to_string() );
			}
			else
			{
				item = item.on_press( Message::ConnectWifi( net.ssid.clone() ) );
			}
			list = list.push( item );
		}
		panel = panel.push( ltk::scroll( list ) );
	}

	ltk::column::<Message>()
		.padding( 0.0 ).spacing( 0.0 )
		.push( ltk::spacer() )
		.push( panel )
		.push( ltk::spacer() )
		.into()
}

pub( super ) fn bluetooth( devices: &[ BluetoothDevice ] ) -> ltk::Element<Message>
{
	let palette = ltk::theme_palette();

	let mut panel = ltk::column::<Message>()
		.padding( 16.0 )
		.spacing( 4.0 )
		.max_width( 340.0 )
		.push(
			ltk::row::<Message>()
				.spacing( 8.0 )
				.push( ltk::text( t!( "qs_bluetooth_devices" ).to_string() ).size( 18.0 ).color( palette.text_primary ) )
				.push( ltk::spacer() )
				.push( ltk::button::<Message>( "X" ).variant( ltk::ButtonVariant::Tertiary ).on_press( Message::CloseQuickSettings ) ),
		)
		.push( ltk::separator() );

	if devices.is_empty()
	{
		panel = panel.push( ltk::text( t!( "qs_bluetooth_no_devices" ).to_string() ).size( 14.0 ).color( palette.text_secondary ) );
	}
	else
	{
		let mut list = ltk::column::<Message>().spacing( 0.0 );
		for dev in devices
		{
			let mut item = ltk::list_item::<Message>( &dev.name );
			if dev.connected
			{
				item = item.subtitle( t!( "qs_status_connected" ).to_string() );
			}
			else
			{
				item = item.on_press( Message::ConnectBluetooth( dev.address.clone() ) );
			}
			list = list.push( item );
		}
		panel = panel.push( ltk::scroll( list ) );
	}

	ltk::column::<Message>()
		.padding( 0.0 ).spacing( 0.0 )
		.push( ltk::spacer() )
		.push( panel )
		.push( ltk::spacer() )
		.into()
}
