using System;
using System.IO;
using System.ServiceProcess;

using FilterAPI;
using FilterLogin.LocalHandlers;
using FilterLogin.LocalNetworking;

using FilterLogin.LoginHandlers;
using FilterLogin.LoginNetworking;

namespace FilterLogin
{
    public partial class Service : ServiceBase
    {
        public Service() { }

		internal void Init()
		{
			AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

			Program.Auth = new Authentication();
			Program.SQL = new Database(Program.Auth);

			Program.LocalHandlers = new LocalHandlerLoader();
			Program.LocalClient = new LocalClient();

			LoginListener LListen = new LoginListener();
			Program.LoginHandlers = new LoginHandlerLoader();
		}

        protected override void OnStart(String[] Args)
        {
			Init();
        }

        private void CurrentDomain_UnhandledException(Object Sender, UnhandledExceptionEventArgs Args)
        {
            File.WriteAllLines(String.Format("{0}{1}Crash.txt", AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.FriendlyName.Replace(".exe", "")), new String[] { ((Exception)Args.ExceptionObject).ToString() });

            Program.SendConsoleText(ConsoleColor.Red, ((Exception)Args.ExceptionObject).ToString());
        }

        protected override void OnStop() { }
    }
}
