namespace SoulstormReplayReader.Core.Utils;

public ref struct SpanReader
{
    public Span<byte> bytes;
    public int offset = 0;

    public SpanReader(Span<byte> bytes)
    {
        this.bytes = bytes;
    }

    public SpanReader SliceToOffset()
    {
        bytes = bytes[offset..];
        offset = 0;
        return this;
    }

    public SpanReader Skip(uint count)
    {
        offset += (int)count;
        return this;
    }

    public byte ReadByte()
    {
        var value = bytes[offset];
        offset++;
        return value;
    }

    public short ReadInt16()
    {
        var value = BitConverter.ToInt16(bytes[offset..]);
        offset += sizeof(short);
        return value;
    }

    public int ReadInt32()
    {
        var value = BitConverter.ToInt32(bytes[offset..]);
        offset += sizeof(int);
        return value;
    }

    public long ReadInt64()
    {
        var value = BitConverter.ToInt64(bytes[offset..]);
        offset += sizeof(long);
        return value;
    }

    public readonly void RequireInRange()
    {
        if (offset >= bytes.Length)
            throw new ArgumentOutOfRangeException(nameof(offset));
    }
}