using System.IO;
using skyscraper5.Skyscraper.IO;

namespace skyscraper5.Dvb.Subtitling
{
    public class PageCompositionSegment
    {
        public PageCompositionSegment(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            PageTimeOut = ms.ReadUInt8();
            byte readUInt8 = ms.ReadUInt8();
            PageVersionNumber = readUInt8 & 0xf0 >> 4;
            PageState = readUInt8 & 0x0c >> 2;

            long numRegions = ms.GetAvailableBytes() / 6;
            Regions = new Region[numRegions];
            for (int i = 0; i < numRegions; i++)
            {
                Regions[i] = new Region();
                Regions[i].RegionId = ms.ReadUInt8();
                ms.ReadUInt8();
                Regions[i].RegionHorizontalAddress = ms.ReadUInt16BE();
                Regions[i].RegionVerticalAddress = ms.ReadUInt16BE();
            }
        }

        public Region[] Regions { get; set; }
        public byte PageTimeOut { get; set; }
        public int PageVersionNumber { get; set; }
        public int PageState { get; set; }

        public class Region
        {
            public byte RegionId { get; set; }
            public ushort RegionHorizontalAddress { get; set; }
            public ushort RegionVerticalAddress { get; set; }
        }
    }
}