using skyscraper5.Dvb.Descriptors;
using skyscraper5.Dvb.Psi;
using skyscraper5.Dvb.Psi.Model;
using skyscraper8.Skyscraper.Scraper.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using skyscraper8.Skyscraper.Plugins;

namespace skyscraper8.EPGCollectorPort.SkyscraperSide.Freesat
{
    internal class FreesatTunnelScraper : IBatEventHandler, ISdtEventHandler, ITdtEventHandler, ITotEventHandler, IEitEventHandler, INitEventHandler
    {
        public void OnBatBouquet(BatBouquet batBouquet)
        {
	        throw new NotImplementedException();
        }

        public void OnBatTransportStream(BatBouquet batBouquet, BatTransportStream child)
        {
	        throw new NotImplementedException();
		}

        public void OnSdtService(ushort transportStreamId, ushort originalNetworkId, SdtService sdtService)
        {
	        throw new NotImplementedException();
		}
        
        public void OnTdtTime(DateTime utcTime)
        {
	        if (!networkId.HasValue)
		        return;
	        if (!transportStreamId.HasValue)
		        return;

			throw new NotImplementedException();
		}
        
        public void OnTotTime(DateTime utcTime, LocalTimeOffsetDescriptor ltod)
        {
	        if (!networkId.HasValue)
		        return;
	        if (!transportStreamId.HasValue)
		        return;

	        throw new NotImplementedException();
		}

        private ushort? networkId;
        public void SetNetworkId(ushort networkId)
        {
	        throw new NotImplementedException();
		}

        private ushort? transportStreamId;
        public void SetTransportStreamId(ushort transportStreamId)
        {
	        throw new NotImplementedException();
		}

        public void SetNetworkId(ushort networkId, bool forceOverwrite = false)
        {
	        throw new NotImplementedException();
        }

        public void OnNitTransportStream(ushort networkId, NitTransportStream transportStream)
        {
	        if (!_dataStorage.TestForNitTransportStream(networkId, transportStream))
	        {
		        _logger.Log(PluginLogLevel.Info, "NIT Network {0} Transport Stream {1}", networkId, transportStream.TransportStreamId);
		        _dataStorage.StoreNitTransportStream(networkId, transportStream);
		        _hostNotification.Invoke();
			}
        }

        public void OnNitNetwork(NitNetwork nitNetwork)
        {
	        if (!_dataStorage.TestNitNetwork(nitNetwork))
	        {
		        _logger.Log(PluginLogLevel.Info, "NIT Network {0}", nitNetwork.NetworkId);
		        _dataStorage.StoreNitNetwork(nitNetwork);
		        _hostNotification.Invoke();
			}
        }

        private EitEvent[] runningEvents;
        public void OnEitEvent(EitEvent eitEvent)
        {
	        if (_dataStorage.StoreEitEvent(eitEvent))
	        {
		        _logger.Log(PluginLogLevel.Info, "EIT Event: {0}", eitEvent.EventName);
				_hostNotification.Invoke();
	        }
            
			if (eitEvent.RunningStatus == RunningStatus.Running)
			{
				if (runningEvents == null)
					runningEvents = new EitEvent[ushort.MaxValue];
				runningEvents[eitEvent.ServiceId] = eitEvent;
			}
		}

        private PluginLogger _logger;
        private IFreesatTunnelDataStorage _dataStorage;
        private Action _hostNotification;
        public void ConnectToStorage(object[] getPluginConnector, Action onPluginEvent)
        {
	        _dataStorage = new FreesatTunnelDataStorage(getPluginConnector);
			_logger = PluginLogManager.GetLogger(GetType());
	        _hostNotification = onPluginEvent;
        }
    }
}
