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

namespace ServerFix
{
    class FileSystem
    {
        public Dictionary<String, String> ConfigValues = new Dictionary<String, String>();
        public List<String> BanList;

        public FileSystem()
        {
            if (!Directory.Exists(Program.LogFolder)) { Directory.CreateDirectory(Program.LogFolder); }
            if (!Directory.Exists(Program.ErrorFolder)) { Directory.CreateDirectory(Program.ErrorFolder); }
            if (!Directory.Exists(Program.ExceptionFolder)) { Directory.CreateDirectory(Program.ExceptionFolder); }
            if (!Directory.Exists(Program.PacketFolder)) { Directory.CreateDirectory(Program.PacketFolder); }
            if (!Directory.Exists(Program.BlockFolder)) { Directory.CreateDirectory(Program.BlockFolder); }
            if (!File.Exists(Program.ConfigFile)) { File.WriteAllBytes(Program.ConfigFile, Properties.Resources.Config); }

            BanList = new List<String>();

            ConfigValues = LoadConfigValues();
        }

        public void WriteBlock(Int32 Length, String Packet)
        {
            if (!Program.Logging) { return; }

            try
            {
                String BlockFile = String.Format("{0}{1}.Log", Program.BlockFolder, DateTime.Now.ToString("dd-MM-yyyy"));

                using (FileStream BlockStream = new FileStream(BlockFile, FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter Writer = new StreamWriter(BlockStream))
                    {
                        String Line = String.Format("Time: {0}{1}Length: {2}{1}Packet: {3}{1}", DateTime.Now.ToString("HH:mm:ss"), Environment.NewLine, Length, Packet);

                        Writer.WriteLine(Line);
                        Writer.Close();
                    }
                    BlockStream.Close();
                }
            }
            catch { }
        }

        public void WriteError(String Error)
        {
            if (!Program.Logging) { return; }

            try
            {
                String ErrorFile = String.Format("{0}{1}.Log", Program.ErrorFolder, DateTime.Now.ToString("dd-MM-yyyy"));

                using (FileStream ErrorStream = new FileStream(ErrorFile, FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter Writer = new StreamWriter(ErrorStream))
                    {
                        String Line = String.Format("Time: {0}{1}Error: {2}{1}", DateTime.Now.ToString("HH:mm:ss"), Environment.NewLine, Error);

                        Writer.WriteLine(Line);
                        Writer.Close();
                    }
                    ErrorStream.Close();
                }
            }
            catch { }
        }

        public void WriteException(String Exception)
        {
            if (!Program.Logging) { return; }

            try
            {
                String ExceptionFile = String.Format("{0}{1}.Log", Program.ExceptionFolder, DateTime.Now.ToString("dd-MM-yyyy"));

                using (FileStream ExceptionStream = new FileStream(ExceptionFile, FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter Writer = new StreamWriter(ExceptionStream))
                    {
                        String Line = String.Format("Time: {0}{1}Exception: {2}{1}", DateTime.Now.ToString("HH:mm"), Environment.NewLine, Exception);

                        Writer.WriteLine(Line);
                        Writer.Close();
                    }
                    ExceptionStream.Close();
                }
            }
            catch { }
        }

        public void WritePacket(String IP, String Packet, Int16 Length)
        {
            if (!Program.Logging) { return; }

            try
            {
                String PacketFile = String.Format("{0}{1}.Log", Program.PacketFolder, DateTime.Now.ToString("dd-MM-yyyy"));

                using (FileStream PacketStream = new FileStream(PacketFile, FileMode.Append, FileAccess.Write))
                {
                    using (StreamWriter Writer = new StreamWriter(PacketStream))
                    {
                        String Line = String.Format("Time: {0}{1}IP: {2}{1}Packet: {3}{1}Length: {4}{1}", DateTime.Now.ToString("HH:mm:ss"), Environment.NewLine, IP, Packet, Length);

                        Writer.WriteLine(Line);
                        Writer.Close();
                    }
                    PacketStream.Close();
                }
            }
            catch { }
        }

        private Dictionary<String, String> LoadConfigValues()
        {
            Dictionary<String, String> Values = new Dictionary<String, String>();

            try
            {
                using (StreamReader Reader = new StreamReader(Program.ConfigFile))
                {
                    String CurrentLine;

                    while ((CurrentLine = Reader.ReadLine()) != null)
                    {
                        if (CurrentLine == "") { }
                        else if (CurrentLine.StartsWith("#") || CurrentLine.StartsWith("//")) { }
                        else
                        {
                            String[] Split = CurrentLine.Split(':');
                            Values.Add(Split[0], Split[1].TrimStart());
                        }
                    }
                }
            }
            catch { }

            return Values;
        }
    }
}
