using skyscraper5.Skyscraper.IO.CrazycatStreamReader;
using skyscraper5.src.Skyscraper.FrequencyListGenerator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper8.Skyscraper.Scraper.Storage.InMemory.Model
{
	internal class DbBlindscanJobContainer
	{
		private DbBlindscanJob jobInDb;
		private List<SearchResultContainer> searchResults;

		public DbBlindscanJobContainer(DbBlindscanJob jobInDb)
		{
			this.jobInDb = jobInDb;
		}

		public override bool Equals(object? obj)
		{
			return obj is DbBlindscanJobContainer container &&
				   EqualityComparer<DbBlindscanJob>.Default.Equals(jobInDb, container.jobInDb);
		}

		public override int GetHashCode()
		{
			return HashCode.Combine(jobInDb);
		}

		internal void AddSearchResult(bool satellite, SearchResult searchResult, int polarityIndex, SearchResult2 searchResult2)
		{
			if (searchResults == null)
				searchResults = new List<SearchResultContainer>();

			searchResults.Add(new SearchResultContainer(satellite, searchResult, polarityIndex, searchResult2));
		}

		internal SearchResultContainer GetSearchResult(bool satellit, SearchResult searchResult, int polarityIndex, SearchResult2 searchResult2)
		{
			SearchResultContainer searchResultContainer = searchResults.First(x => x.Matches(satellit, searchResult, polarityIndex, searchResult2));
			return searchResultContainer;
		}

		internal SearchResultContainer GetSearchResult(bool satellit, SearchResult searchResult, SearchResult2 searchResult2)
		{
			SearchResultContainer searchResultContainer = searchResults.First(x => x.Matches(satellit, searchResult, searchResult2));
			return searchResultContainer;
		}

		public Guid JobGuid
		{
			get
			{
				return jobInDb.JobGuid;
			}
		}

		internal class SearchResultContainer
		{
			private bool satellite;
			private SearchResult searchResult;
			private int polarityIndex;
			private SearchResult2 searchResult2;
			private List<HumanReadableService> services;

			public SearchResultContainer(bool satellite, SearchResult searchResult, int polarityIndex, SearchResult2 searchResult2)
			{
				this.satellite = satellite;
				this.searchResult = searchResult;
				this.polarityIndex = polarityIndex;
				this.searchResult2 = searchResult2;
			}

			public override bool Equals(object? obj)
			{
				return obj is SearchResultContainer container &&
					   satellite == container.satellite &&
					   EqualityComparer<SearchResult>.Default.Equals(searchResult, container.searchResult) &&
					   polarityIndex == container.polarityIndex &&
					   EqualityComparer<SearchResult2>.Default.Equals(searchResult2, container.searchResult2);
			}

			public override int GetHashCode()
			{
				return HashCode.Combine(satellite, searchResult, polarityIndex, searchResult2);
			}

			public bool Matches(bool satellite, SearchResult searchResult, int polarityIndex, SearchResult2 searchResult2)
			{
				if (this.satellite != satellite)
					return false;
				if (!EqualityComparer<SearchResult>.Default.Equals(searchResult, this.searchResult))
					return false;
				if (this.polarityIndex != polarityIndex)
					return false;
				if (!EqualityComparer<SearchResult2>.Default.Equals(searchResult2, this.searchResult2))
					return false;
				return true;
			}

			public bool Matches(bool satellite, SearchResult searchResult,SearchResult2 searchResult2)
			{
				if (this.satellite != satellite)
					return false;
				if (!EqualityComparer<SearchResult>.Default.Equals(searchResult, this.searchResult))
					return false;
				if (!EqualityComparer<SearchResult2>.Default.Equals(searchResult2, this.searchResult2))
					return false;
				return true;
			}

			internal void AddService(HumanReadableService humanReadableService)
			{
				if (services == null)
					services = new List<HumanReadableService>();
				services.Add(humanReadableService);
			}
		}
	}
}
