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

pub( crate ) fn view_sound( state: &State ) -> Element<Msg>
{
	let vol_label  = tr!( "sound.volume", pct = format!( "{:.0}", state.volume * 100.0 ) );
	let mute_label = if state.muted { tr!( "sound.muted" ) } else { tr!( "sound.unmuted" ) };

	let mut col = column::<Msg>()
		.padding( 24.0 )
		.spacing( 8.0 )
		.push( header( &tr!( "home.sound" ) ) )
		.push( t_sec!( vol_label ).size( 14.0 ) )
		.push( slider( state.volume ).on_change( Msg::SetVolume ) )
		.push( toggle( state.muted ).label( mute_label ).on_toggle( Msg::ToggleMute ) );

	if !state.sinks.is_empty()
	{
		col = col.push( separator() )
			.push( t_sec!( tr!( "sound.output" ) ).size( 13.0 ) );
		for dev in &state.sinks
		{
			let mut item = list_item( dev.name.as_str() );
			if dev.default { item = item.trailing( "✓" ); } else { item = item.on_press( Msg::SelectSink( dev.id ) ); }
			col = col.push( item );
		}
	}

	if !state.sources.is_empty()
	{
		let mic_label  = tr!( "sound.mic_volume", pct = format!( "{:.0}", state.mic_volume * 100.0 ) );
		let mmute_label = if state.mic_muted { tr!( "sound.mic_muted" ) } else { tr!( "sound.mic_unmuted" ) };

		col = col.push( separator() )
			.push( t_sec!( tr!( "sound.input" ) ).size( 13.0 ) );
		for dev in &state.sources
		{
			let mut item = list_item( dev.name.as_str() );
			if dev.default { item = item.trailing( "✓" ); } else { item = item.on_press( Msg::SelectSource( dev.id ) ); }
			col = col.push( item );
		}
		col = col.push( t_sec!( mic_label ).size( 14.0 ) )
			.push( slider( state.mic_volume ).on_change( Msg::SetMicVolume ) )
			.push( toggle( state.mic_muted ).label( mmute_label ).on_toggle( Msg::ToggleMicMute ) );
	}

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