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

namespace skyscraper5.Dvb.Descriptors
{
	[SkyscraperPlugin]
	[TsDescriptor(0x4d,"EIT","RCT")]
    [BannedTable("PMT")]
    class ShortEventDescriptor : TsDescriptor
    {
        public ShortEventDescriptor(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            Iso639LanguageCode = Encoding.UTF8.GetString(ms.ReadBytes(3));

            int eventNameLength = ms.ReadUInt8();
            EventName = En300468AnnexATextDecoder.GetInstance().Decode(ms.ReadBytes(eventNameLength));

            int textLength = ms.ReadUInt8();
            if (textLength > 0)
            {
                if (ms.GetAvailableBytes() >= textLength)
                {
                    Text = En300468AnnexATextDecoder.GetInstance().Decode(ms.ReadBytes(textLength));
                    Valid = true;
                }
                else
                {
                    Valid = false;
                }
            }
        }

        public string Text { get; private set; }

        public string EventName { get; private set; }

        public string Iso639LanguageCode { get; private set; }
    }
}
