namespace YMhut.Box.Core.Media;

public enum MediaVolumeOutputMode
{
    Silent,
    Normal,
    Boosted
}

public readonly record struct MediaVolumeState(
    int Percent,
    double PlatformVolume,
    double AudioGain,
    MediaVolumeOutputMode Mode)
{
    public bool IsMuted => Percent <= 0;

    public bool IsBoosted => Mode == MediaVolumeOutputMode.Boosted;
}

public static class MediaVolumeModel
{
    public const int MinPercent = 0;
    public const int MaxPercent = 120;
    public const int NormalMaxPercent = 100;

    public static MediaVolumeState FromPercent(double percent)
    {
        var normalized = (int)Math.Round(Math.Clamp(percent, MinPercent, MaxPercent), MidpointRounding.AwayFromZero);
        return new MediaVolumeState(
            normalized,
            normalized <= 0 ? 0 : Math.Min(normalized, NormalMaxPercent) / 100d,
            normalized / 100d,
            normalized <= 0
                ? MediaVolumeOutputMode.Silent
                : normalized > NormalMaxPercent
                    ? MediaVolumeOutputMode.Boosted
                    : MediaVolumeOutputMode.Normal);
    }

    public static string FormatPercent(int percent, string language = "zh-CN")
    {
        var state = FromPercent(percent);
        var english = string.Equals(language, "en-US", StringComparison.OrdinalIgnoreCase);
        return state.Mode switch
        {
            MediaVolumeOutputMode.Silent => english ? "0% (Muted)" : "0%（静音）",
            MediaVolumeOutputMode.Boosted => english ? $"{state.Percent}% (Boosted)" : $"{state.Percent}%（增强音量）",
            _ => $"{state.Percent}%"
        };
    }
}
