using System;
using System.IO;
using System.Net;
using System.Collections.Generic;

using FilterAPI.External;
using System.Diagnostics;

namespace FilterAPI
{
    public class Authentication
    {
        public Dictionary<String, String> ConfigValues = new Dictionary<String, String>();

        public Authentication()
        {
			String DecryptedConfigURL = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Conf", "Config.txt");
			String DecryptedHashesURL = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Conf", "Hashes.txt");

			WebClient AuthenticationClient = new WebClient();
			
			try
			{
				using (var HashStream = new StreamReader(AuthenticationClient.OpenRead(DecryptedHashesURL)))
				{
					List<String> Lines = new List<String>();

					String CurrentLine;

					while ((CurrentLine = HashStream.ReadLine()) != null) { Lines.Add(CurrentLine); }

					String[] DecryptedLines = Lines.ToArray();
					Dictionary<String, String> FileHashes = new Dictionary<String, String>();
					foreach (String DecryptedLine in DecryptedLines)
					{
						String[] LineSplit = DecryptedLine.Split(new String[] { ": " }, StringSplitOptions.None);

						FileHashes.Add(LineSplit[0], LineSplit[1]);
					}

					if (Convert.ToBoolean(FileHashes["Enabled"]))
					{
						String FileName = AppDomain.CurrentDomain.FriendlyName.Replace(".exe", "");

						if (FileName.StartsWith("FilterZone")) { FileName = "FilterZone"; }

						if (!FileHashes.ContainsKey(FileName)) { Environment.Exit(0); }
						else if (FileHashes[FileName] != ExecutingHash.GetMD5OfExE(String.Concat(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName))) { Environment.Exit(0); }
					}
				}
				
				using (var ConfigStream = new StreamReader(AuthenticationClient.OpenRead(DecryptedConfigURL)))
				{
					List<String> Lines = new List<String>();

					String CurrentLine;

					while ((CurrentLine = ConfigStream.ReadLine()) != null) { Lines.Add(CurrentLine); }

					String[] DecryptedLines = Lines.ToArray();
					foreach (String DecryptedLine in DecryptedLines)
					{
						String[] LineSplit = DecryptedLine.Split(new String[] { ": " }, StringSplitOptions.None);

						if (ConfigValues.ContainsKey(LineSplit[0])) { File.WriteAllLines(String.Format("{0}{1}AuthDuplicateRow.txt", AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName.Replace(".exe", "")), new String[] { LineSplit[0] }); }
						ConfigValues.Add(LineSplit[0], LineSplit[1]);
					}
				}

				if (Convert.ToBoolean(ConfigValues["TimeLocked"]))
				{
					String[] DateSplit = ConfigValues["TimeLockEnd"].Split('/');

					DateTime LockDate = new DateTime(Convert.ToInt32(DateSplit[2]), Convert.ToInt32(DateSplit[1]), Convert.ToInt32(DateSplit[0]), 0, 0, 0);

					if (DateTime.Now > LockDate) { Environment.Exit(0); }
				}
			}
			catch (Exception Error)
			{
				File.WriteAllLines(String.Format("{0}{1}Crash.txt", AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName.Replace(".exe", "")), new String[] { Error.ToString() });

				Environment.Exit(0);
			}
        }
    }
}
