using System;
using System.Runtime.CompilerServices;
using CommunityToolkit.Mvvm.DependencyInjection;
using StrixMusic.Sdk.WinUI.Services.Localization;
using Windows.UI.Xaml.Data;
namespace StrixMusic.Sdk.WinUI.Converters.Time
{
///
/// A converter that converts a given to a natural time format string.
///
///
/// "1 Hr 4 Min 40 Sec"
///
public sealed class TimeSpanToShortTextConverter : IValueConverter
{
///
/// Converts a to a formatted string.
///
/// The to convert.
/// A formatted string of the .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string Convert(TimeSpan value)
{
var localizationService = Ioc.Default.GetRequiredService();
var returnValue = string.Empty;
if (value.Hours > 0)
returnValue += string.Format(localizationService.Time?.GetString("HrCount") ?? string.Empty, value.Hours) + " ";
if (value.Minutes > 0)
returnValue += string.Format(localizationService.Time?.GetString("MinCount") ?? string.Empty, value.Minutes) + " ";
if (value.Seconds > 0 || returnValue == string.Empty)
returnValue += string.Format(localizationService.Time?.GetString("SecCount") ?? string.Empty, value.Seconds) + " ";
return returnValue;
}
///
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is TimeSpan timeSpan)
{
return Convert(timeSpan);
}
return Convert(TimeSpan.Zero);
}
///
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}