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

pub( crate ) fn view_storage( state: &State ) -> Element<Msg>
{
	let mut col = column::<Msg>()
		.padding( 24.0 )
		.spacing( 12.0 )
		.push( header( &tr!( "home.storage" ) ) );

	if let Some( disk ) = state.storage.first()
	{
		let used_frac = ( disk.used as f32 / disk.total.max( 1 ) as f32 ).clamp( 0.0, 1.0 );
		let free      = disk.total.saturating_sub( disk.used );

		col = col.push(
			t_sec!( tr!(
				"common.used_of",
				used  = storage::fmt_bytes( disk.used ),
				total = storage::fmt_bytes( disk.total )
			) ).size( 13.0 )
		);
		col = col.push( progress_bar( used_frac ) );
		col = col.push( separator() );
		col = col.push( list_item( tr!( "common.used" )  ).trailing( &storage::fmt_bytes( disk.used ) ) );
		col = col.push( list_item( tr!( "common.free" )  ).trailing( &storage::fmt_bytes( free ) ) );
		col = col.push( list_item( tr!( "common.total" ) ).trailing( &storage::fmt_bytes( disk.total ) ) );
		col = col.push( separator() );
		col = col.push( list_item( tr!( "storage.mount_point" ) ).trailing( &disk.mount ) );
	}
	else
	{
		col = col.push( t_sec!( tr!( "storage.no_info" ) ).size( 14.0 ) );
	}

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