using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;

namespace skyscraper8.Skyscraper.Scraper
{
	internal class PmtTracker
	{
		private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name);

		class PmtInfo
		{
			public bool IsProcessed { get; set; }
		}

		private Dictionary<int, PmtInfo> knownPmts;

		public bool AllPmtsProcessed
		{
			get
			{
				foreach (KeyValuePair<int, PmtInfo> kvp in knownPmts)
				{
					if (!kvp.Value.IsProcessed)
						return false;
				}

				return true;
			}
		}

		public void AddPmtPid(int pmtPid)
		{
			if (knownPmts == null)
				knownPmts = new Dictionary<int, PmtInfo>();

			if (!knownPmts.ContainsKey(pmtPid))
				knownPmts.Add(pmtPid, new PmtInfo());
		}

		private bool allProccessedAnnounced;
		public void MarkAsProcessed(int pmtPid)
		{
			if (!knownPmts.ContainsKey(pmtPid))
				return;

			knownPmts[pmtPid].IsProcessed = true;

			if (!allProccessedAnnounced && AllPmtsProcessed)
			{
				logger.DebugFormat("All {0} PMTs have now been checked.", knownPmts.Count);
				allProccessedAnnounced = true;
			}
		}

		private bool autodetectionFired;
		public bool ShouldFireAutodetection()
		{
			if (autodetectionFired)
				return false;

			if (AllPmtsProcessed)
			{
				autodetectionFired = true;
				return true;
			}

			return false;
		}
	}
}
