//! Top-row connectivity chips — WiFi + Datos móviles.
//! All sizes use `Length::vw` so ltk resolves them at layout time.

use std::sync::Arc;

use crate::message::Message;
use crate::system::SystemState;
use crate::system::toggles::ToggleState;
use rust_i18n::t;

use super::super::icons::Icons;
use super::super::{
	CARD_RADIUS,
	QS_CARD_ICON_VW, QS_CARD_PAD_V_VW, QS_CARD_PADDING_H_VW, QS_GRID_GAP_VW, QS_TEXT_GAP_VW,
	QS_TITLE_VW, QS_STATUS_VW, QS_CARD_SIDE_VW, QS_CARD_ROW_GAP_VW,
};

pub( super ) fn build(
	icons:        &Icons,
	system_state: &SystemState,
	toggle_state: &ToggleState,
	palette:      &ltk::Palette,
) -> ltk::Element<Message>
{
	let wifi_label = t!( "qs_wifi" ).to_string();
	let ( wifi_title, wifi_status ) = if system_state.wifi.connected
	{
		(
			system_state.wifi.ssid.clone().unwrap_or_else( || wifi_label.clone() ),
			t!( "qs_status_connected" ).to_string(),
		)
	} else if system_state.wifi.enabled {
		( wifi_label.clone(), t!( "qs_status_on" ).to_string() )
	} else {
		( wifi_label.clone(), t!( "qs_status_off" ).to_string() )
	};

	let mobile_status = if toggle_state.mobile_data
	{
		t!( "qs_status_on" ).to_string()
	} else {
		t!( "qs_status_off" ).to_string()
	};

	let wifi_card = ltk::pressable(
		card_inner( icons, &icons.wifi_card, &wifi_title, wifi_status, palette ),
	)
	.on_press( Message::OpenWifiPicker );

	let mobile_card = card_inner(
		icons, &icons.mobile_card, t!( "qs_mobile_data" ).to_string(), mobile_status, palette,
	);

	ltk::grid::<Message>( 2 )
		.spacing( ltk::Length::vw( QS_GRID_GAP_VW ) )
		.padding( 0.0 )
		.push( wifi_card )
		.push( mobile_card )
		.into()
}

pub( super ) fn card_inner(
	icons:    &Icons,
	icon_slot: &Option<( Arc<Vec<u8>>, u32, u32 )>,
	title:    impl Into<String>,
	status:   impl Into<String>,
	palette:  &ltk::Palette,
) -> ltk::Element<Message>
{
	ltk::container::<Message>(
		ltk::row::<Message>()
			.spacing( ltk::Length::vw( QS_CARD_ROW_GAP_VW ) )
			.padding( 0.0 )
			.push( ltk::spacer().width( ltk::Length::vw( QS_CARD_SIDE_VW ) ) )
			.push( card_icon( icons, icon_slot, palette ) )
			.push( ltk::flex(
				ltk::column::<Message>()
					.padding( 0.0 )
					.spacing( ltk::Length::vw( QS_TEXT_GAP_VW ) )
					.push( ltk::text( title.into()  ).size( ltk::Length::vw( QS_TITLE_VW ) ).color( palette.text_primary ) )
					.push( ltk::text( status.into() ).size( ltk::Length::vw( QS_STATUS_VW ) ).color( palette.text_secondary ) ),
			) )
			.push( ltk::spacer().width( ltk::Length::vw( QS_CARD_SIDE_VW ) ) ),
	)
	.surface( "surface-card-flat" )
	.radius( CARD_RADIUS )
	.padding_h( ltk::Length::vw( QS_CARD_PADDING_H_VW ) )
	.padding_v( ltk::Length::vw( QS_CARD_PAD_V_VW ) )
	.into()
}

fn card_icon(
	icons:    &Icons,
	icon_slot: &Option<( Arc<Vec<u8>>, u32, u32 )>,
	palette:  &ltk::Palette,
) -> ltk::Element<Message>
{
	match icon_slot
	{
		Some( ( raw, w, h ) ) =>
		{
			let tinted = icons.tint_cached( raw, palette.text_primary );
			ltk::img_widget( tinted, *w, *h )
				.size( ltk::Length::vw( QS_CARD_ICON_VW ), ltk::Length::vw( QS_CARD_ICON_VW ) )
				.into()
		}
		None => ltk::spacer().into(),
	}
}
