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(0x6a,"PMT")]
    public class Ac3Descriptor : TsDescriptor
    {
        public Ac3Descriptor(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer);
            byte readUInt8 = ms.ReadUInt8();

            bool componentTypeFlag = (readUInt8 & 0x80) != 0;
            bool bsidFlag = (readUInt8 & 0x40) != 0;
            bool mainIdFlag = (readUInt8 & 0x20) != 0;
            bool asvcFlag = (readUInt8 & 0x10) != 0;

            if (componentTypeFlag)
            {
                ComponentType = (ComponentTypeEnum)ms.ReadUInt8();
            }

            if (bsidFlag)
            {
                BSID = ms.ReadUInt8() & 0x1f;
            }

            if (mainIdFlag)
            {
                MainId = ms.ReadUInt8();
            }

            if (asvcFlag)
            {
                Asvc = ms.ReadUInt8();
            }
        }

        public byte? Asvc { get; private set; }

        public byte? MainId { get; private set; }

        public int? BSID { get; private set; }

        public ComponentTypeEnum? ComponentType { get; private set; }

        public enum ComponentTypeEnum : byte
        {
            Ac3 = 0,
            EAc3 = 1
        }
    }
}
