using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using AprsSharp.Parsers.Aprs;
using GeoCoordinatePortable;
using Npgsql;
using skyscraper5.Aprs.AprsSharp;
using skyscraper5.Aprs.AprsStorage;
using skyscraper5.Data.PostgreSql;
using skyscraper5.Ietf.Rfc971;
using skyscraper5.Skyscraper.Plugins;
using skyscraper5.Skyscraper.Scraper.Storage;
using skyscraper5.Skyscraper.Scraper;
using skyscraper5.Skyscraper.Scraper.Storage.InMemory;

namespace skyscraper5.Aprs
{
	[SkyscraperPlugin]
	[PluginPriority(1)]
	internal class Lx9SesPlugin : ISkyscraperMpePlugin
	{
		private LX9SESStorage storage;
		public void ConnectToStorage(object[] connector)
		{
			object o = connector[0];
			switch (o)
			{
				case InMemoryPluginToken t1:
					storage = new AprsInMemoryStorage(t1);
					break;
				case PostgresqlToken t2:
					storage = new AprsPostgresqlStorage(t2);
					break;
				default:
					throw new NotImplementedException(o.GetType().FullName);
			}
		}

		public void SetContext(DateTime? currentTime, object skyscraperContext)
		{
			
		}

		public void SetContext(DateTime? currentDateTime)
		{
			messageReceiver.currentTime = currentDateTime;
		}

		public bool CanHandlePacket(InternetHeader internetHeader, byte[] ipv4Packet)
		{
			if (internetHeader.Protocol == 0x11)
			{
				if (internetHeader.SourceAddress.Equals(_lx9sesSrc) && internetHeader.DestinationAddress.Equals(_lx9sesDst))
				{
					return true;
				}
			}

			return false;
		}

		public void HandlePacket(InternetHeader internetHeader, byte[] ipv4Packet)
		{
			if (_aprsDecoder == null)
				_aprsDecoder = new AprsDecoder(messageReceiver);
			_aprsDecoder.OnIpv4PacketArrival(internetHeader, ipv4Packet);
		}

		public bool StopProcessingAfterThis()
		{
			return true;
		}

		private AprsDecoder _aprsDecoder;
		private static readonly IPAddress _lx9sesDst = IPAddress.Parse("228.64.0.56");
		private static readonly IPAddress _lx9sesSrc = IPAddress.Parse("10.225.49.1");
		private LX9SESStorage _storage;
		private LX9SESMessageReceiver messageReceiver;


	}


}
