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

namespace skyscraper5.Dvb.Descriptors
{
	[SkyscraperPlugin]
	[TsDescriptor(0x7c,"PMT")]
    class AacDescriptor : TsDescriptor
    {
        public AacDescriptor(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            ProfileAndLevel = ms.ReadUInt8();
            if (ms.GetAvailableBytes() > 0)
            {
                byte readUInt8 = ms.ReadUInt8();
                bool aacTypeFlag = (readUInt8 & 0x80) != 0;
                SaocDeFlag = (readUInt8 & 0x40) != 0;
                if (aacTypeFlag)
                {
	                if (ms.GetAvailableBytes() == 0)
	                {
		                Valid = false;
		                return;
	                }
                    AacType = ms.ReadUInt8();
                }

                AdditionalInfo = ms.ReadBytes(ms.GetAvailableBytes());
            }
        }

        public byte[] AdditionalInfo { get; private set; }

        public byte? AacType { get; private set; }

        public bool? SaocDeFlag { get; private set; }

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