using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper5.Skyscraper.IO;
using skyscraper5.Skyscraper.Plugins;

namespace skyscraper5.Mpeg2.Descriptors
{
	[SkyscraperPlugin]
	[TsDescriptor(0x32,"PMT")]
    class J2KVideoDescriptor : TsDescriptor
    {
        public J2KVideoDescriptor(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            ushort readUInt16Be = ms.ReadUInt16BE();
            bool extendedCapabilityFlag = (readUInt16Be & 0x8000) != 0;
            ProfileAndLevel = (readUInt16Be & 0x7fff);
            HorizontalSize = ms.ReadUInt32BE();
            VerticalSize = ms.ReadUInt32BE();
            MaxBitRate = ms.ReadUInt32BE();
            MaxBufferSize = ms.ReadUInt32BE();
            DenFrameRate = ms.ReadUInt16BE();
            NumFrameRate = ms.ReadUInt16BE();
            if (extendedCapabilityFlag)
            {
                byte readUInt8 = ms.ReadUInt8();
                StripeFlag = (readUInt8 & 0x80) != 0;
                BlockFlag = (readUInt8 & 0x40) != 0;
                MdmFlag = (readUInt8 & 0x20) != 0;
                if ((readUInt8 & 0x1f) != 0)
                    return; //invalid if not all zero
            }
            else
            {
                //T-REC-H.222.0-201808-S!!PDF-E.pdf, page 126
                throw new NotImplementedException(nameof(J2KVideoDescriptor));
            }

            throw new NotImplementedException(nameof(J2KVideoDescriptor));
        }

        public bool MdmFlag { get; set; }

        public bool BlockFlag { get; private set; }

        public bool StripeFlag { get; private set; }

        public ushort NumFrameRate { get; private set; }

        public ushort DenFrameRate { get; private set; }

        public uint MaxBufferSize { get; private set; }

        public uint MaxBitRate { get; private set; }

        public uint VerticalSize { get; private set; }

        public uint HorizontalSize { get; private set; }

        public int ProfileAndLevel { get; private set; }
    }
}
