using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
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(0x5D,"SDT")]
    [BannedTable("PMT")]
    public class MultilingualServiceNameDescriptor : TsDescriptor
    {
        public MultilingualServiceNameDescriptor(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream(buffer, false);
            List<MultilingualServiceName> result = new List<MultilingualServiceName>();
            while (ms.GetAvailableBytes() > 4)
            {
                MultilingualServiceName child = new MultilingualServiceName();
                child.Iso639LanguageCode = Encoding.ASCII.GetString(ms.ReadBytes(3));
                byte serviceProviderNameLength = ms.ReadUInt8();
                child.ServiceProviderName = En300468AnnexATextDecoder.GetInstance().Decode(ms.ReadBytes(serviceProviderNameLength));
                byte serviceNameLength = ms.ReadUInt8();
                child.ServiceName = En300468AnnexATextDecoder.GetInstance().Decode(ms.ReadBytes(serviceNameLength));
                result.Add(child);
            }

            ServiceNames = result.AsReadOnly();
        }

        public ReadOnlyCollection<MultilingualServiceName> ServiceNames { get; private set; }


        public class MultilingualServiceName
        {
            public string Iso639LanguageCode { get; set; }
            public string ServiceProviderName { get; set; }
            public string ServiceName { get; set; }
        }
    }
}
