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

namespace skyscraper5.Dvb.Descriptors
{
	[SkyscraperPlugin]
	[TsDescriptor(0x59,"PMT")]
    [BannedTable("TSDT")]
    public class SubtitlingDescriptor : TsDescriptor
    {
        public SubtitlingDescriptor(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            int num = buffer.Length / 8;
            Subtitlings = new Subtitling[num];
            for (int i = 0; i < num; i++)
            {
                Subtitlings[i] = new Subtitling();
                Subtitlings[i].Iso639LanguageCode = Encoding.ASCII.GetString(ms.ReadBytes(3));
                Subtitlings[i].SubtitlingType = ms.ReadUInt8();
                Subtitlings[i].CompositionPageId = ms.ReadUInt16BE();
                Subtitlings[i].AncillaryPageId = ms.ReadUInt16BE();
            }
        }

        public Subtitling[] Subtitlings { get; private set; }
        public class Subtitling
        {
            public string Iso639LanguageCode { get; internal set; }
            public byte SubtitlingType { get; internal set; }

            public bool IsEtsiEn300743
            {
                get
                {
                    return (SubtitlingType >= 0x10 && SubtitlingType <= 0x15) ||
                           (SubtitlingType >= 0x20 && SubtitlingType <= 0x25);
                }
            }

            public ushort CompositionPageId { get; internal set; }
            public ushort AncillaryPageId { get; internal set; }
        }
    }
}
