using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace skyscraper5.src.Skyscraper.Scraper.Storage.InMemory
{
	internal class KnownTsMemory
	{
		public KnownTsMemory(string filename)
		{
			importFilesKnownFilename = filename;
		}
		public class KnownTs
		{
			public string name;
			public TimeSpan timespan;
			public int tsType;

			public override string ToString()
			{
				return name;
			}
		}
		private List<KnownTs> importFilesKnown;
		private string importFilesKnownFilename;

		private void LoadImportFilesKnown()
		{
			if (importFilesKnown != null)
				return;

			if (File.Exists(importFilesKnownFilename))
			{
				importFilesKnown = JsonConvert.DeserializeObject<List<KnownTs>>(File.ReadAllText(importFilesKnownFilename));
			}
			else
			{
				importFilesKnown = new List<KnownTs>();
			}
		}

		public void ImportMarkFileAsKnown(FileInfo fi, TimeSpan ts, int tstype)
		{
			LoadImportFilesKnown();
			KnownTs child = new KnownTs();
			child.name = fi.FullName;
			child.timespan = ts;
			child.tsType = tstype;
			importFilesKnown.Add(child);
			File.WriteAllText(importFilesKnownFilename, JsonConvert.SerializeObject(importFilesKnown, Formatting.Indented));
		}

		public bool ImportFileKnown(FileInfo fi)
		{
			LoadImportFilesKnown();

			foreach(KnownTs filename in importFilesKnown)
			{
				if (filename.name.Equals(fi.FullName))
					return true;
			}
			return false;
		}
	}
}
