using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.Hdmv
{
    internal class HdmvReader : IDisposable
    {
        public Stream Wrapped { get; }

        public HdmvReader(Stream wrapped)
        {
            Wrapped = wrapped;
            if (!wrapped.CanRead)
                throw new ArgumentException("not readable", nameof(wrapped));

            buffer = new byte[188];
        }

        private byte[] buffer;
        public byte[] ReadPacket()
        {
            if (Wrapped.Read(buffer, 0, 4) != 4)
                return null;

            if (Wrapped.Read(buffer, 0, 188) != 188)
                return null;

            if (buffer[0] != 0x47)
                return null;

            return buffer;
        }

        public void Dispose()
        {
            Wrapped?.Dispose();
        }
    }
}
