using System;
using Unity.Mathematics;

namespace GamecraftModdingAPI.Blocks
{
    public struct BlockColor
    {
        public BlockColors Color => Index == byte.MaxValue
            ? BlockColors.Default
            : (BlockColors) (Index % 10);

        public byte Darkness => (byte) (Index == byte.MaxValue
            ? 0
            : Index / 10);

        public byte Index { get; }

        public BlockColor(byte index)
        {
            if (index > 99 && index != byte.MaxValue)
                throw new ArgumentOutOfRangeException(nameof(index), "Invalid color index. Must be 0-90 or 255.");
            Index = index;
        }

        public BlockColor(BlockColors color, byte darkness = 0)
        {
            if (darkness > 9)
                throw new ArgumentOutOfRangeException(nameof(darkness), "Darkness must be 0-9 where 0 is default.");
            if (color > BlockColors.Red) //Last valid color
                throw new ArgumentOutOfRangeException(nameof(color), "Invalid color!");
            Index = (byte) (darkness * 10 + (byte) color);
        }
        
        public static implicit operator BlockColor(BlockColors color)
        {
            return new BlockColor(color);
        }

        public float4 RGBA => Block.BlockEngine.ConvertBlockColor(Index);

        public override string ToString()
        {
            return $"{nameof(Color)}: {Color}, {nameof(Darkness)}: {Darkness}";
        }
    }

    /// <summary>
    /// Preset block colours
    /// </summary>
    public enum BlockColors : byte
    {
        Default = byte.MaxValue,
        White = 0,
        Pink,
        Purple,
        Blue,
        Aqua,
        Green,
        Lime,
        Yellow,
        Orange,
        Red
    }
}