//! Round-button bank — eight pills in a 4×2 grid.
//! All sizes use `Length::vw` so ltk resolves them at layout time,
//! regardless of screen resolution or dpi_scale.

use std::sync::Arc;

use crate::message::Message;
use crate::system::toggles::{ ToggleId, ToggleState };

use super::super::icons::Icons;
use super::super::{
	QS_VSLIDER_W_VW, QS_BUTTON_ICON_VW, QS_BTN_PAD_VW, QS_GRID_GAP_VW,
};

pub( super ) fn build(
	icons:        &Icons,
	toggle_state: &ToggleState,
	bt_on:        bool,
	palette:      &ltk::Palette,
) -> ltk::Element<Message>
{
	build_grid( icons, toggle_state, bt_on, palette,
		ltk::Length::vw( QS_VSLIDER_W_VW ),
		ltk::Length::vw( QS_BUTTON_ICON_VW ),
		ltk::Length::vw( QS_BTN_PAD_VW ),
		ltk::Length::vw( QS_GRID_GAP_VW ),
	)
}

pub( super ) fn build_desktop(
	icons:          &Icons,
	toggle_state:   &ToggleState,
	bt_on:          bool,
	palette:        &ltk::Palette,
	vslider_w:      f32,
	button_icon_px: f32,
	gap:            f32,
) -> ltk::Element<Message>
{
	let pad = ( vslider_w - button_icon_px ) / 2.0;
	build_grid( icons, toggle_state, bt_on, palette,
		ltk::Length::px( vslider_w ),
		ltk::Length::px( button_icon_px ),
		ltk::Length::px( pad ),
		ltk::Length::px( gap ),
	)
}

fn build_grid(
	icons:        &Icons,
	toggle_state: &ToggleState,
	bt_on:        bool,
	palette:      &ltk::Palette,
	_diameter:    ltk::Length,
	icon_sz:      ltk::Length,
	pad:          ltk::Length,
	gap:          ltk::Length,
) -> ltk::Element<Message>
{
	let b = |icon: &Option<( Arc<Vec<u8>>, u32, u32 )>, msg: Message, active: bool|
		round_button( icons, icon, msg, active, palette, icon_sz, pad );

	ltk::grid::<Message>( 4 )
		.spacing_x( gap )
		.spacing_y( gap )
		.padding( 0.0 )
		.push( b( &icons.bluetooth,     Message::ToggleBluetooth,                  bt_on ) )
		.push( b( &icons.settings,      Message::OpenSettings,                     false ) )
		.push( b( &icons.airplane,      Message::Toggle( ToggleId::AirplaneMode ), toggle_state.airplane_mode ) )
		.push( b( &icons.notifications, Message::Toggle( ToggleId::DoNotDisturb ), toggle_state.dnd ) )
		.push( b( &icons.dark_mode,     Message::Toggle( ToggleId::DarkMode     ), toggle_state.dark_mode ) )
		.push( b( &icons.rotation,      Message::Toggle( ToggleId::RotationLock ), toggle_state.rotation_lock ) )
		.push( b( &icons.vpn,           Message::Toggle( ToggleId::Vpn          ), toggle_state.vpn ) )
		.push( b( &icons.flashlight,    Message::Toggle( ToggleId::Flashlight   ), toggle_state.flashlight ) )
		.into()
}

fn round_button(
	icons:    &Icons,
	icon_slot: &Option<( Arc<Vec<u8>>, u32, u32 )>,
	msg:      Message,
	active:   bool,
	palette:  &ltk::Palette,
	icon_sz:  ltk::Length,
	pad:      ltk::Length,
) -> ltk::Element<Message>
{
	let icon_el: 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( icon_sz, icon_sz ).into()
		}
		None => ltk::spacer().into(),
	};

	let centered = ltk::column::<Message>()
		.padding( 0.0 )
		.spacing( 0.0 )
		.fit_content()
		.push( icon_el );

	let surface_slot = if active { "surface-toggle-active" } else { "surface-card-flat" };
	let pill = ltk::pressable(
		ltk::container::<Message>( centered )
			.surface( surface_slot )
			.radius( 9999.0 )
			.padding_h( pad )
			.padding_v( pad ),
	)
	.on_press( msg );

	ltk::row::<Message>()
		.padding( 0.0 )
		.spacing( 0.0 )
		.push( ltk::flex( ltk::spacer() ) )
		.push( pill )
		.push( ltk::flex( ltk::spacer() ) )
		.into()
}
