using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using skyscraper5.Skyscraper.IO;

namespace skyscraper5.Dvb.Subtitling
{
    public class RegionCompositionSegment
    {
        public RegionCompositionSegment(byte[] segmentData)
        {
            MemoryStream ms = new MemoryStream(segmentData, false);
            RegionId = ms.ReadUInt8();
            byte readUInt8 = ms.ReadUInt8();
            RegionVersionNumber = (readUInt8 & 0xf0) >> 4;
            RegionFillFlag = (readUInt8 & 0x08) != 0;
            RegionWidth = ms.ReadUInt16BE();
            RegionHeight = ms.ReadUInt16BE();

            readUInt8 = ms.ReadUInt8();
            RegionLevelOfCompatibility = (readUInt8 & 0xe0) >> 5;
            RegionDepth = (readUInt8 & 0x1c);
            ClutId = ms.ReadUInt8();
            Region8bitPixelCode = ms.ReadUInt8();

            readUInt8 = ms.ReadUInt8();
            Region4bitPixelCode = (readUInt8 & 0xf0) >> 4;
            Region2bitPixelCode = (readUInt8 & 0x0c) >> 2;

            List<Object> objects = new List<Object>();
            while (ms.Position < ms.Length)
            {
                Object child = new Object();
                child.Id = ms.ReadUInt16BE();

                ushort readUInt16Be = ms.ReadUInt16BE();
                child.Type = (readUInt16Be & 0xc000) >> 14;
                child.ProviderFlag = (readUInt16Be & 0x3000) >> 12;
                child.HorizontalPosition = (readUInt16Be & 0x0fff);
                child.VerticalPosition = (ms.ReadUInt16BE() & 0x0fff);
                if (child.Type == 0x01 && child.Type == 0x02)
                {
                    child.ForegroundPixelCode = ms.ReadUInt8();
                    child.BackgroundPixelCode = ms.ReadUInt8();
                }

                objects.Add(child);
            }

            Objects = objects.AsReadOnly();
        }

        public ReadOnlyCollection<Object> Objects { get; private set; }

        public int Region2bitPixelCode { get; private set; }

        public int Region4bitPixelCode { get; private set; }

        public byte Region8bitPixelCode { get; private set; }

        public byte ClutId { get; private set; }

        public int RegionDepth { get; private set; }

        public int RegionLevelOfCompatibility { get; private set; }

        public ushort RegionHeight { get; private set; }

        public ushort RegionWidth { get; private set; }

        public bool RegionFillFlag { get; private set; }

        public int RegionVersionNumber { get; private set; }

        public byte RegionId { get; private set; }

        public class Object
        {
            public ushort Id { get; internal set; }
            public int Type { get; internal set; }
            public int ProviderFlag { get; internal set; }
            public int HorizontalPosition { get; internal set; }
            public int VerticalPosition { get; internal set; }
            public byte ForegroundPixelCode { get; internal set; }
            public byte BackgroundPixelCode { get; internal set; }
        }
    }
}