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.ExtensionDescriptors
{
    [SkyscraperPlugin]
    [Mpeg2ExtensionDescriptor(0x03)]
    class HevcTimingAndHrdDescriptor : TsDescriptor
    {
        public HevcTimingAndHrdDescriptor(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            byte readUInt8 = ms.ReadUInt8();
            HrdManagementValidFlag = (readUInt8 & 0x80) != 0;
            TargetScheduleIdxNotPresentFlag = (readUInt8 & 0x40) != 0;
            TargetScheduleIdx = (readUInt8 & 0x3e) >> 1;
            PictureAndTimingInfoPresentFlag = (readUInt8 & 0x01) != 0;
            if (PictureAndTimingInfoPresentFlag)
            {
                _90khzFlag = (ms.ReadUInt8() & 0x80) != 0;
                if (!_90khzFlag.Value)
                {
                    N = ms.ReadUInt32BE();
                    K = ms.ReadUInt32BE();
                }

                NumUnitsInTick = ms.ReadUInt32BE();
            }
        }

        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 int TargetScheduleIdx { get; private set; }

        public bool TargetScheduleIdxNotPresentFlag { get; private set; }

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