// Mostly-static screens without dedicated logic yet. When one of these
// grows real behaviour, move it to its own file.

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

pub( crate ) fn view_system_updater( state: &State ) -> Element<Msg>
{
	let status = match state.updates_available
	{
		None      => tr!( "updater.not_checked" ),
		Some( 0 ) => tr!( "updater.up_to_date" ),
		Some( n ) => if n == 1 { tr!( "updater.update_available" ) } else { tr!( "updater.updates_available", n = n ) },
	};

	column::<Msg>()
		.padding( 24.0 )
		.spacing( 12.0 )
		.push( header( &tr!( "home.updater" ) ) )
		.push( list_item( tr!( "updater.status" ) ).trailing( status ) )
		.push(
			button( tr!( "updater.check_now" ) )
				.variant( ButtonVariant::Secondary )
				.on_press( Msg::TriggerUpdateCheck )
		)
		.push( spacer() )
		.into()
}

pub( crate ) fn view_security_status( state: &State ) -> Element<Msg>
{
	let fw_str = if state.firewall_active { tr!( "common.active" ) } else { tr!( "common.inactive" ) };
	let na     = tr!( "common.not_available" );
	let se_str = state.selinux_state.as_deref().unwrap_or( &na );

	column::<Msg>()
		.padding( 24.0 )
		.spacing( 4.0 )
		.push( header( &tr!( "home.security_status" ) ) )
		.push( list_item( tr!( "security.firewall" ) ).trailing( fw_str ) )
		.push( list_item( tr!( "security.selinux" ) ).trailing( se_str ) )
		.push( spacer() )
		.into()
}

pub( crate ) fn view_cellular( state: &State ) -> Element<Msg>
{
	let no_sim = state.sim_imei.is_none() && state.sim_operator.is_none();

	let mut col = column::<Msg>()
		.padding( 24.0 )
		.spacing( 4.0 )
		.push( header( &tr!( "home.cellular" ) ) );

	if let Some( op ) = state.sim_operator.as_deref()
	{
		col = col.push( list_item( tr!( "cellular.operator" ) ).trailing( op ) );
	}
	if let Some( imei ) = state.sim_imei.as_deref()
	{
		col = col.push( list_item( tr!( "cellular.imei" ) ).trailing( imei ) );
	}
	if no_sim
	{
		col = col.push( t_sec!( tr!( "cellular.no_sim" ) ).size( 14.0 ) );
	}

	col.push( spacer() ).into()
}

pub( crate ) fn view_interconnectivity( state: &State ) -> Element<Msg>
{
	let nfc_str = if state.nfc_available { tr!( "common.available" ) } else { tr!( "common.not_available" ) };

	column::<Msg>()
		.padding( 24.0 )
		.spacing( 8.0 )
		.push( header( &tr!( "home.interconnectivity" ) ) )
		.push( toggle( state.bt_on ).label( tr!( "home.bluetooth" ) ).on_toggle( Msg::ToggleBluetooth ) )
		.push( separator() )
		.push( list_item( tr!( "interconnectivity.nfc" ) ).trailing( nfc_str ) )
		.push( list_item( tr!( "interconnectivity.usb" ) ).trailing( &state.usb_mode ) )
		.push( spacer() )
		.into()
}

pub( crate ) fn view_portable_hotspot( state: &State ) -> Element<Msg>
{
	column::<Msg>()
		.padding( 24.0 )
		.spacing( 8.0 )
		.push( header( &tr!( "home.hotspot" ) ) )
		.push(
			toggle( state.hotspot_active )
				.label( tr!( "home.hotspot" ) )
				.on_toggle( Msg::ToggleHotspot )
		)
		.push( separator() )
		.push( t_sec!( tr!( "hotspot.share_hint" ) ).size( 13.0 ) )
		.push( spacer() )
		.into()
}

pub( crate ) fn view_more_connectivity( state: &State ) -> Element<Msg>
{
	let mut col = column::<Msg>()
		.padding( 24.0 )
		.spacing( 8.0 )
		.push( header( &tr!( "home.more_connectivity" ) ) )
		.push( toggle( state.airplane_mode ).label( tr!( "more_connectivity.airplane" ) ).on_toggle( Msg::ToggleAirplane ) )
		.push( separator() )
		.push( t_sec!( tr!( "more_connectivity.vpn" ) ).size( 13.0 ) );

	if state.vpn_connections.is_empty()
	{
		col = col.push( list_item( tr!( "more_connectivity.no_vpn" ) ).trailing( "" ) );
	}
	else
	{
		for name in &state.vpn_connections
		{
			col = col.push( with_chevron( list_item( name ) ) );
		}
	}

	col.push( spacer() ).into()
}

pub( crate ) fn view_lock_screen( state: &State ) -> Element<Msg>
{
	column::<Msg>()
		.padding( 24.0 )
		.spacing( 8.0 )
		.push( header( &tr!( "home.lock_screen" ) ) )
		.push(
			toggle( state.lockscreen_show_notifs )
				.label( tr!( "lock_screen.show_notifications" ) )
				.on_toggle( Msg::ToggleLockscreenNotifs )
		)
		.push( spacer() )
		.into()
}

pub( crate ) fn view_fingerprint( state: &State ) -> Element<Msg>
{
	let fprint_status = match (state.fprintd_active, state.fprintd_enrolled) {
		(false, _)    => tr!( "fingerprint.unavailable" ),
		(true, false) => tr!( "fingerprint.not_enrolled" ),
		(true, true)  => tr!( "fingerprint.enrolled" ),
	};

	column::<Msg>()
		.padding( 24.0 )
		.spacing( 4.0 )
		.push( header( &tr!( "home.fingerprint" ) ) )
		.push( list_item( tr!( "fingerprint.daemon" ) )
			.trailing( if state.fprintd_active { tr!( "common.active" ) } else { tr!( "common.inactive" ) } ) )
		.push( list_item( tr!( "fingerprint.fingerprint" ) ).trailing( fprint_status ) )
		.push( list_item( tr!( "fingerprint.face" ) ).trailing( tr!( "fingerprint.not_enrolled" ) ) )
		.push( spacer() )
		.into()
}

pub( crate ) fn view_privacy_protection( state: &State ) -> Element<Msg>
{
	let fw_str = if state.firewall_active { tr!( "common.active" ) } else { tr!( "common.inactive" ) };
	let na     = tr!( "common.not_available" );
	let se_str = state.selinux_state.as_deref().unwrap_or( &na );

	column::<Msg>()
		.padding( 24.0 )
		.spacing( 4.0 )
		.push( header( &tr!( "home.privacy_protection" ) ) )
		.push( list_item( tr!( "security.firewall" ) ).trailing( fw_str ) )
		.push( list_item( tr!( "security.selinux" ) ).trailing( se_str ) )
		.push( separator() )
		.push(
			toggle( state.location_enabled )
				.label( tr!( "privacy.location_services" ) )
				.on_toggle( Msg::ToggleLocation )
		)
		.push( spacer() )
		.into()
}

pub( crate ) fn view_privacy( state: &State ) -> Element<Msg>
{
	let fw_str = if state.firewall_active { tr!( "common.active" ) } else { tr!( "common.inactive" ) };

	column::<Msg>()
		.padding( 24.0 )
		.spacing( 8.0 )
		.push( header( &tr!( "home.privacy" ) ) )
		.push(
			toggle( state.location_enabled )
				.label( tr!( "privacy.location_services" ) )
				.on_toggle( Msg::ToggleLocation )
		)
		.push( separator() )
		.push( list_item( tr!( "security.firewall" ) ).trailing( fw_str ) )
		.push( spacer() )
		.into()
}

pub( crate ) fn view_location( state: &State ) -> Element<Msg>
{
	column::<Msg>()
		.padding( 24.0 )
		.spacing( 8.0 )
		.push( header( &tr!( "home.location" ) ) )
		.push(
			toggle( state.location_enabled )
				.label( tr!( "privacy.location_services" ) )
				.on_toggle( Msg::ToggleLocation )
		)
		.push( spacer() )
		.into()
}


pub( crate ) fn view_accessibility( state: &State ) -> Element<Msg>
{
	let scale_pct = ( state.ui_scale * 100.0 ).round() as u32;
	let scale_norm = ( state.ui_scale - 1.0 ) / 1.0; // 1.0..2.0 → 0..1

	// `0.0` is forge deciding on its own. The toggle always names the scale
	// the automatic path would choose, in force or not, so the option says
	// what picking it would do rather than echoing the current pin.
	let iface_auto = state.interface_scale <= 0.0;
	let auto_pct   = ( state.interface_automatic * 100.0 ).round() as u32;
	let last_step  = ( system::INTERFACE_STEPS.len() - 1 ) as f32;
	let iface_norm = system::interface_step_index( state.interface_scale ) as f32 / last_step;

	let mut col = column::<Msg>()
		.padding( 24.0 )
		.spacing( 8.0 )
		.push( header( &tr!( "home.accessibility" ) ) )
		.push( t_sec!( tr!( "accessibility.text_scale", pct = scale_pct ) ).size( 13.0 ) )
		.push(
			slider( scale_norm.clamp( 0.0, 1.0 ) )
				.on_change( |v| Msg::PreviewUiScale( 1.0 + v ) )
				.on_release( |v| Msg::SetUiScale( 1.0 + v ) )
		)
		.push( section_sep() )
		.push(
			toggle( iface_auto )
				.label( tr!( "accessibility.interface_auto", pct = auto_pct ) )
				.on_toggle( Msg::ToggleInterfaceAuto )
		);

	if !iface_auto
	{
		col = col.push(
			t_sec!( tr!( "accessibility.interface_scale", pct = ( state.interface_scale * 100.0 ).round() as u32 ) )
				.size( 13.0 )
		);
		col = col.push(
			slider( iface_norm.clamp( 0.0, 1.0 ) )
				.on_change( |v| Msg::PreviewInterfaceScale( system::interface_step_at( v ) ) )
				.on_release( |v| Msg::SetInterfaceScale( system::interface_step_at( v ) ) )
		);
	}

	col.push( spacer() ).into()
}
