// Appearance screens: display/brightness, wallpaper and themes.

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

pub( crate ) fn view_display( state: &State ) -> Element<Msg>
{
	let bri_label  = tr!( "display.brightness", pct = format!( "{:.0}", state.brightness * 100.0 ) );
	let nl_label   = tr!( "display.night_light_temp", k = state.night_light_temp );
	let nl_norm    = ( state.night_light_temp as f32 - 1000.0 ) / 5500.0;
	let sleep_val  = if state.sleep_timeout == 0 { tr!( "common.never" ) }
	                 else { tr!( "common.minutes", n = state.sleep_timeout / 60 ) };
	let sleep_norm = state.sleep_timeout as f32 / 60.0 / 30.0;

	let mut col = column::<Msg>()
		.padding( 24.0 )
		.spacing( 12.0 )
		.push( header( &tr!( "home.display" ) ) )
		// ── Brightness ──────────────────────────────────────────────────
		.push( t_sec!( bri_label ).size( 13.0 ) )
		.push( slider( state.brightness ).on_change( Msg::SetBrightness ) )
		// ── Night Light ─────────────────────────────────────────────────
		.push( section_sep() )
		.push( toggle( state.night_light_on ).label( tr!( "display.night_light" ) ).on_toggle( Msg::ToggleNightLight ) );

	if state.night_light_on
	{
		col = col.push( t_sec!( nl_label ).size( 13.0 ) );
		col = col.push(
			slider( nl_norm.clamp( 0.0, 1.0 ) )
				.on_change( |v| Msg::SetNightLightTemp( ( 1000.0 + v * 5500.0 ) as u32 ) )
		);
	}

	// ── Rotation ────────────────────────────────────────────────────────
	// A row of five segments does not fit a phone-width window, and the
	// options are mutually exclusive, so this is a picker.
	col = col.push( section_sep() );
	let rotation = combo(
		state.rotation_combo.clone(),
		vec![
			tr!( "common.auto" ).to_string(),
			tr!( "display.portrait" ).to_string(),
			tr!( "display.landscape" ).to_string(),
			tr!( "display.portrait_inv" ).to_string(),
			tr!( "display.landscape_inv" ).to_string(),
		],
	)
		.label( tr!( "display.rotation" ) )
		.on_toggle_open( Msg::RotationToggle )
		.on_select_idx( Msg::RotationSelect )
		.on_dismiss( Msg::RotationDismiss );
	col = col.push( rotation.trigger() );

	// ── Scale ────────────────────────────────────────────────────────────
	col = col.push( section_sep() );
	// Both scales live in Accessibility; this control used to claim to be a
	// display scale while writing the text-scaling factor.
	col = col.push( row_link!( tr!( "display.scale_link" ), "accessibility/magnifier", Screen::Accessibility ) );

	// ── Sleep ─────────────────────────────────────────────────────────────
	col = col.push( section_sep() );
	col = col.push( t_sec!( tr!( "display.sleep", val = sleep_val ) ).size( 13.0 ) );
	col = col.push(
		slider( sleep_norm.clamp( 0.0, 1.0 ) )
			.on_change( |v| Msg::SetSleepTimeout( ( v * 30.0 ).round() as u32 * 60 ) )
	);

	// The popup layers over the page from the same surface.
	let mut s = stack::<Msg>().push( col.push( spacer() ) );
	if let Some( p ) = rotation.popup() { s = s.push( p ); }
	s.into()
}

pub( crate ) fn view_wallpaper( state: &State ) -> Element<Msg>
{
	let variant_lbl = match state.wallpaper_variant
	{
		ThemeMode::Light => tr!( "wallpaper.light_variant" ),
		ThemeMode::Dark  => tr!( "wallpaper.dark_variant" ),
	};

	let mut col = column::<Msg>()
		.padding( 24.0 )
		.spacing( 12.0 )
		.push( header( &tr!( "home.wallpaper" ) ) )
		.push( t_sec!( tr!( "wallpaper.current" ) ).size( 13.0 ) )
		.push( list_item( tr!( "wallpaper.name" ) ).trailing( tr!( "wallpaper.default" ) ) )
		.push( list_item( tr!( "wallpaper.variant" ) ).trailing( variant_lbl ) );

	// Resolved by the `Msg::Open` arm — views must not probe the disk.
	if let Some( path ) = state.wallpaper_file.as_deref()
	{
		col = col.push( list_item( tr!( "wallpaper.file" ) ).subtitle( path ) );
	}
	else
	{
		col = col.push( t_sec!( tr!( "wallpaper.not_found" ) ).size( 13.0 ) );
	}

	col = col.push( section_sep() );
	col = col.push( t_sec!( tr!( "wallpaper.variant" ) ).size( 13.0 ) );
	col = col.push(
		row::<Msg>()
			.spacing( 6.0 )
			.push( seg_btn( &tr!( "wallpaper.light_variant" ), state.wallpaper_variant == ThemeMode::Light, Msg::SetWallpaperVariant( ThemeMode::Light ) ) )
			.push( seg_btn( &tr!( "wallpaper.dark_variant" ),  state.wallpaper_variant == ThemeMode::Dark,  Msg::SetWallpaperVariant( ThemeMode::Dark ) ) )
	);
	col = col.push( section_sep() );
	col = col.push(
		t_sec!( tr!( "wallpaper.extra_hint" ) )
			.size( 12.0 )
	);

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

pub( crate ) fn view_themes( state: &State ) -> Element<Msg>
{
	use chrono::Timelike as _;
	let hour    = chrono::Local::now().hour();
	let active  = state.theme_pref.resolve( hour );
	let mode_lbl = match active
	{
		ThemeMode::Light => tr!( "common.light" ),
		ThemeMode::Dark  => tr!( "common.dark" ),
	};
	let pref_lbl = match state.theme_pref
	{
		ThemePreference::Auto  => tr!( "common.auto" ),
		ThemePreference::Light => tr!( "common.light" ),
		ThemePreference::Dark  => tr!( "common.dark" ),
	};
	let summary = tr!( "themes.preference", pref = pref_lbl, mode = mode_lbl );

	column::<Msg>()
		.padding( 24.0 )
		.spacing( 12.0 )
		.push( header( &tr!( "home.themes" ) ) )
		.push( t_sec!( tr!( "themes.appearance" ) ).size( 13.0 ) )
		.push(
			row::<Msg>()
				.spacing( 6.0 )
				.push( seg_btn( &tr!( "common.auto" ),  state.theme_pref == ThemePreference::Auto,  Msg::SetThemePref( ThemePreference::Auto  ) ) )
				.push( seg_btn( &tr!( "common.light" ), state.theme_pref == ThemePreference::Light, Msg::SetThemePref( ThemePreference::Light ) ) )
				.push( seg_btn( &tr!( "common.dark" ),  state.theme_pref == ThemePreference::Dark,  Msg::SetThemePref( ThemePreference::Dark  ) ) )
		)
		.push( section_sep() )
		.push( t_sec!( summary ).size( 13.0 ) )
		.push( t_sec!( tr!( "themes.auto_hint" ) ).size( 12.0 ) )
		.push( spacer() )
		.into()
}
