using System;
using System.Text;
using System.Reflection;
using System.ServiceProcess;
using System.Collections.Generic;
using System.Collections.Concurrent;

using FilterAPI;
using FilterAPI.Networking;

using FilterManager.LocalHandlers;
using FilterManager.LocalNetworking;

using FilterManager.ManagerHandlers;
using FilterManager.ManagerNetworking;

using CS;
using SHN;
using System.Diagnostics;
using System.Threading;

namespace FilterManager
{
    internal class Program
    {
        public static String ServiceName = "Manager";

        public static Authentication Auth;
        public static Database SQL;

        public static SHNFile CharacterCreate;
        public static SHNFile AcademyMoneyRewards;
        public static SHNFile SlanderFilter;

        public static LocalClient LocalClient;
        public static LocalHandlerLoader LocalHandlers;

        public static ManagerHandlerLoader ManagerHandlers;

        public static ConcurrentDictionary<Guid, ManagerClient> ManagerClients = new ConcurrentDictionary<Guid, ManagerClient>();
        public static Dictionary<String, Int32> GrabednUserNos = new Dictionary<String, Int32>();

        private static void Main()
        {
			if (Environment.UserInteractive)
			{
				Service s = new Service();
				s.Init(); // Init() is pretty much any code you would have in OnStart().
				while (true) { Thread.Sleep(1); }
			}
			else
			{
				ServiceBase[] ServicesToRun = new ServiceBase[] { new Service() };
				ServiceBase.Run(ServicesToRun);
				Debugger.Break();
			}
		}

        public static void LoadSHNs()
        {
            MethodInfo CryptoMethod = CSFile.LoadCode(Program.Auth.ConfigValues["SHNCryptoFile"], "SHN.SHNCrypto", Program.Auth.ConfigValues["SHNCryptoName"]);

            Program.CharacterCreate = new SHNFile(String.Format("{0}CharacterCreate.shn", Program.Auth.ConfigValues["FilterSHNPath"]), CryptoMethod);

            if (Program.CharacterCreate.Type == SHNType.TextData) { Program.CharacterCreate.SHNEncoding = Encoding.ASCII; }
            else { Program.CharacterCreate.SHNEncoding = Encoding.GetEncoding("ISO-8859-1"); }

            if (Program.CharacterCreate.Type != SHNType.QuestData) { Program.CharacterCreate.Read(); }
            else { Program.CharacterCreate.ReadQuest(); }

            CharacterCreate.DisallowRowChanges();

            Program.AcademyMoneyRewards = new SHNFile(String.Format("{0}AcademyMoneyRewards.shn", Program.Auth.ConfigValues["FilterSHNPath"]), CryptoMethod);

            if (Program.AcademyMoneyRewards.Type == SHNType.TextData) { Program.AcademyMoneyRewards.SHNEncoding = Encoding.ASCII; }
            else { Program.AcademyMoneyRewards.SHNEncoding = Encoding.GetEncoding("ISO-8859-1"); }

            if (Program.AcademyMoneyRewards.Type != SHNType.QuestData) { Program.AcademyMoneyRewards.Read(); }
            else { Program.AcademyMoneyRewards.ReadQuest(); }

            AcademyMoneyRewards.DisallowRowChanges();

            Program.SlanderFilter = new SHNFile(String.Format("{0}SlanderFilter.shn", Program.Auth.ConfigValues["FilterSHNPath"]), CryptoMethod);

            if (Program.SlanderFilter.Type == SHNType.TextData) { Program.SlanderFilter.SHNEncoding = Encoding.ASCII; }
            else { Program.SlanderFilter.SHNEncoding = Encoding.GetEncoding("ISO-8859-1"); }

            if (Program.SlanderFilter.Type != SHNType.QuestData) { Program.SlanderFilter.Read(); }
            else { Program.SlanderFilter.ReadQuest(); }

            SlanderFilter.DisallowRowChanges();
        }

        public static void SendConsoleText(ConsoleColor WriteColor, String Text, params Object[] Args)
        {
            String FormattedText = String.Format(Text, Args);

            using (var ClientPacket = new Packet(2, 3))
            {
                ClientPacket.PacketWriter.Write(ConsoleColorConverter.ConsoleColorToID(WriteColor));
                ClientPacket.PacketWriter.Write(ServiceName);
                ClientPacket.PacketWriter.Write(FormattedText);

                if (LocalClient != null) { LocalClient.SendPacket(ClientPacket); }
            }
        }
    }
}
