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

namespace skyscraper5.Mpeg2
{
    class Mpeg2ExtensionDescriptorConverter : IDescriptorConverter
    {
        private ConstructorInfo[] constructors;
        public Mpeg2ExtensionDescriptorConverter()
        {
	        constructors = PluginManager.GetInstance().GetMpeg2ExtensionDescriptors();
        }

        public TsDescriptor Convert(TsDescriptor descriptor, string callingTable)
        {
            ExtensionDescriptor extensionDescriptor = (ExtensionDescriptor)descriptor;
            ConstructorInfo constructorInfo = constructors[extensionDescriptor.ExtensionDescriptorTag];
            if (constructorInfo == null)
            {
                //Defined in T-REC-H.222.0-201808-S!!PDF-E.pdf
                //Page 135
                throw new NotImplementedException(String.Format("MPEG-2 Extension Descriptor {0:X2}", extensionDescriptor.ExtensionDescriptorTag));
            }

            object invoke = constructorInfo.Invoke(new object[] { extensionDescriptor.SelectorBytes });
            TsDescriptor result = (TsDescriptor)invoke;
            return result;
        }
    }
}
