using System.Runtime.InteropServices;

namespace Minint.Core.Models;

/// <summary>
/// 4-byte RGBA color value. Equality is component-wise.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly record struct RgbaColor(byte R, byte G, byte B, byte A)
{
    public static readonly RgbaColor Transparent = new(0, 0, 0, 0);
    public static readonly RgbaColor Black = new(0, 0, 0, 255);
    public static readonly RgbaColor White = new(255, 255, 255, 255);

    /// <summary>
    /// Packs color into a single uint as 0xAABBGGRR (little-endian RGBA).
    /// Suitable for writing directly into BGRA bitmap buffers after byte-swap,
    /// or for use as a dictionary key.
    /// </summary>
    public uint ToPackedRgba() =>
        (uint)(R | (G << 8) | (B << 16) | (A << 24));

    public static RgbaColor FromPackedRgba(uint packed) =>
        new(
            (byte)(packed & 0xFF),
            (byte)((packed >> 8) & 0xFF),
            (byte)((packed >> 16) & 0xFF),
            (byte)((packed >> 24) & 0xFF));

    /// <summary>
    /// Packs as 0xAARRGGBB — used for Avalonia/SkiaSharp pixel buffers.
    /// </summary>
    public uint ToPackedArgb() =>
        (uint)(B | (G << 8) | (R << 16) | (A << 24));

    public override string ToString() => $"#{R:X2}{G:X2}{B:X2}{A:X2}";
}
