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

namespace skyscraper5.Mpeg2.Descriptors
{
	[SkyscraperPlugin]
	[TsDescriptor(0x2a,"PMT")]
    class AvcTimingAndHrdDescriptor : TsDescriptor
    {
        public AvcTimingAndHrdDescriptor(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            byte readUInt8 = ms.ReadUInt8();
            HrdManagementValidFlag = (readUInt8 & 0x80) != 0;
            PictureAndTimingInfoPresentFlag = (readUInt8 & 0x01) != 0;
            if (PictureAndTimingInfoPresentFlag)
            {
                _90khzFlag = (ms.ReadUInt8() & 0x80) != 0;
                if (!_90khzFlag.Value)
                {
                    N = ms.ReadUInt32BE();
                    K = ms.ReadUInt32BE();
                }
                NumUnitsInTick = ms.ReadUInt32BE();
            }

            readUInt8 = ms.ReadUInt8();
            FixedFrameRateFlag = (readUInt8 & 0x80) != 0;
            TemporalPocFlag = (readUInt8 & 0x40) != 0;
            PictureToDisplayConversionFlag = (readUInt8 & 0x20) != 0;
        }

        public bool PictureToDisplayConversionFlag { get; private set; }

        public bool TemporalPocFlag { get; private set; }

        public bool FixedFrameRateFlag { get; private set; }

        public uint? NumUnitsInTick { get; private set; }

        public uint? K { get; private set; }

        public uint? N { get; private set; }

        public bool? _90khzFlag { get; private set; }

        public bool PictureAndTimingInfoPresentFlag { get; private set; }

        public bool HrdManagementValidFlag { get; private set; }
    }
}
