using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Repository.Hierarchy;
using skyscraper5.Skyscraper;
using skyscraper8.GSE;

namespace skyscraper8.GS
{
	internal class IPv4PacketBruteforce : IMisHandler
	{
		public IPv4PacketBruteforce(GsContextDto context)
		{
			Context = context ?? throw new ArgumentNullException(nameof(context));
			logger = LogManager.GetLogger(String.Format("{0} MIS {1}", System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name, context.GetMisId()));
		}


		private ILog logger;
		private bool notified;

		public GsContextDto Context { get; set; }
		public void PushFrame(BBHeader bbHeader, ReadOnlySpan<byte> readOnlySpan)
		{
			List<byte[]> packets = IpPacketFinder.LookForIpPackets(readOnlySpan);
			if (packets == null)
				return;

			if (packets.Count == 0)
				return;

			if (!notified)
			{
				logger.InfoFormat("Found valid IP traffic on MIS {0}.", Context.GetMisId());
				notified = true;
			}

			foreach (byte[] packet in packets)
			{
				Context.IpOutput.OnIpDatagram(0x0118, packet);
			}
		}
	}
}
