using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Security.Cryptography;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace Launcher
{
    public class Methods
    {
        public static string VersionPath = "reslauncher/Version.dat";
        public static string LogFile = "reslauncher/Log.txt";

        public static void Load()
        {
            try
            {
                if (!File.Exists(VersionPath))
                {
                    Directory.CreateDirectory("reslauncher");
                    File.WriteAllText(VersionPath, Encrypt("Version=0"));
                }
                if (!File.Exists(LogFile))
                {
                    Directory.CreateDirectory("reslauncher");
                    File.WriteAllText(LogFile, "");
                }
                Thread.Sleep(5000);
            }
            catch { }
        }

        public static string Encrypt(string String)
        {
            string EncryptionKey = "c8YXEft&+fMs@]ntv2!JyUuFs[#xgXek";
            byte[] Key = new byte[] { };
            byte[] IV = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
            try
            {
                Key = System.Text.Encoding.UTF8.GetBytes(EncryptionKey.Substring(0, 16));

                RijndaelManaged rManaged = new RijndaelManaged();
                byte[] InputByteArray = Encoding.UTF8.GetBytes(String);
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, rManaged.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
                cStream.Write(InputByteArray, 0, InputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        public static string Decrypt(string String)
        {
            string DecryptionKey = "c8YXEft&+fMs@]ntv2!JyUuFs[#xgXek";
            byte[] Key = new byte[] { };
            byte[] IV = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F };
            byte[] InputByteArray = new byte[String.Length];

            try
            {
                Key = System.Text.Encoding.UTF8.GetBytes(DecryptionKey.Substring(0, 16));
                RijndaelManaged rManaged = new RijndaelManaged();
                InputByteArray = Convert.FromBase64String(String);
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, rManaged.CreateDecryptor(Key, IV), CryptoStreamMode.Write);
                cStream.Write(InputByteArray, 0, InputByteArray.Length);
                cStream.FlushFinalBlock();
                Encoding Encoding = Encoding.UTF8;

                return Encoding.GetString(mStream.ToArray());
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        public static int CountPatches(string PatchDb)
        {
            WebClient Client = new WebClient();
            int Content = Client.DownloadString(new Uri(PatchDb)).Split('\n').Length;
            return Content - 1;
        }

        public static void WriteLog(string Message)
        {
            string LogMessage = String.Format("{0} > {1}", DateTime.Now.ToString(), Message);
            File.AppendAllText(LogFile, LogMessage + Environment.NewLine);
        }

        public static void RunGame(Patch Window, string Bin, string Args = null)
        {
            try
            {
                Process Game = new Process();
                Game.StartInfo.FileName = Bin;
                Game.StartInfo.Arguments = Args;
                Game.StartInfo.UseShellExecute = false;
                Game.Start();
                Environment.Exit(0);
            }
            catch
            {
                Window.Status.Text = "Game file not found. Downloading a new version...";
                Window.Progress.Value = 100;
                Window.Percent.Text = "100%";
                WebClient GameDownloader = new WebClient();
                GameDownloader.DownloadFileCompleted += delegate
                {
                    RunGame(Window, Bin, Args);
                };
                GameDownloader.DownloadFileAsync(new Uri("http://192.168.1.19/patches/" + Patch.Bin), Patch.Bin); // put the location of your patches in the quotes. like "http://192.168.1.19/patches/"
                return;
            }
        }
    }
}
