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

using FilterAPI.External;

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

        public Authentication()
        {
            String Base = AppDomain.CurrentDomain.BaseDirectory;
            String Password = "KGDQIN5B42lY2HoLQrusdehnyCcMoM4Xo2EZIiL0B96nhNo7sAIAtkrHwXNJ8Gk3";
            String ConfigURL = $"{Base}\\Config\\config.ini";
            String HashesURL = $"{Base}\\Config\\hashes.ini";
            String ConfigFolder = "Config";
            if(!Directory.Exists(Path.Combine(Base, ConfigFolder)))
            {
                Directory.CreateDirectory(Path.Combine(Base, ConfigFolder));
            } else
            {
                Environment.Exit(0);
            }

            try
            {
                using (var HashStream = new StreamReader(File.OpenRead(HashesURL)))
                {
                    List<String> Lines = new List<String>();

                    String CurrentLine;

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

                    String[] DecryptedLines = Encryption.DecryptStringArray(Lines.ToArray(), Password);

                    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(File.OpenRead(ConfigURL)))
                {
                    List<String> Lines = new List<String>();

                    String CurrentLine;

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

                    String[] DecryptedLines = Encryption.DecryptStringArray(Lines.ToArray(), Password);

                    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]);
                    }
                }
            }
            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);
            }
        }
    }
}
