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

using FilterAPI;
using FilterAPI.Networking;

using FilterBot.LocalHandlers;
using FilterBot.LocalNetworking;

using FilterBot.LoginHandlers;
using FilterBot.LoginNetworking;

using FilterBot.ManagerNetworking;
using FilterBot.ManagerHandlers;

using FilterBot.ZoneHandlers;

using CS;
using SHN;

namespace FilterBot
{
    internal class Program
    {
        public static String ServiceName = "Bot";

        public static Authentication Auth;
        public static Database SQL;

        public static SHNFile MobInfo;
        public static SHNFile MapInfo;

        public static SHNFile RareMob;

        public static LocalClient LocalClient;
        public static LocalHandlerLoader LocalHandlers;

        public static LoginHandlerLoader LoginHandlers;

        public static ManagerHandlerLoader ManagerHandlers;

        public static ZoneHandlerLoader ZoneHandlers;

        public static Dictionary<String, ManagerServer> BotManagers = new Dictionary<String, ManagerServer>();

        public static object LoginClient { get; private set; }

        private static void Main()
        {
            ServiceBase[] ServicesToRun = new ServiceBase[] { new Service() };
            ServiceBase.Run(ServicesToRun);
        }

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

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

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

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

            RareMob.DisallowRowChanges();

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

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

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

            MobInfo.DisallowRowChanges();

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

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

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

            MapInfo.DisallowRowChanges();
        }

        public static void CreateBot(String sID)
        {
            if (!Program.SQL.sUsernameExists(sID)) { Program.SQL.InsertNewtAccount(sID, Program.Auth.ConfigValues["BotPassword"]); }

            Int32 nEMID = Program.SQL.GetnEMIDFromsUsername(sID);

            String sToken = Program.SQL.CreateAndInsertsTokenFornEMID(nEMID);

            LoginServer BotLogin = new LoginServer(nEMID, sID, sToken);
        }

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