namespace ZB.MOM.WW.LmxProxy.Client.Domain;

/// <summary>Extension methods for <see cref="Quality"/>.</summary>
public static class QualityExtensions
{
    /// <summary>Returns true if quality is in the Good family (byte >= 192).</summary>
    public static bool IsGood(this Quality q) => (byte)q >= 192;

    /// <summary>Returns true if quality is in the Uncertain family (byte 64-127).</summary>
    public static bool IsUncertain(this Quality q) => (byte)q is >= 64 and < 128;

    /// <summary>Returns true if quality is in the Bad family (byte < 64).</summary>
    public static bool IsBad(this Quality q) => (byte)q < 64;

    /// <summary>
    /// Converts an OPC UA 32-bit status code to the simplified <see cref="Quality"/> enum.
    /// Uses the top two bits to determine the quality family.
    /// </summary>
    public static Quality FromStatusCode(uint statusCode)
    {
        uint category = statusCode & 0xC0000000;
        return category switch
        {
            0x00000000 => Quality.Good,
            0x40000000 => Quality.Uncertain,
            _ => Quality.Bad
        };
    }
}
