using System;
using System.IO;
using System.ServiceProcess;
using System.Threading;
using FilterAPI;
using FilterAPI.Networking;

using FilterRest.LocalHandlers;
using FilterRest.LocalNetworking;

namespace FilterRest
{
    internal class Program
    {
        public static String ServiceName = "Rest";

        public static Authentication Auth;
        public static Database SQL;

        public static LocalClient LocalClient;
        public static LocalHandlerLoader LocalHandlers;
		
		//public static String LogPath = @"C:\Filter\RestLog.txt";

		//public static void Log(string msg)
		//{
		//	using (StreamWriter w = File.AppendText(LogPath))
		//	{
		//		w.WriteLine(String.Format("{0} || {1}", $"{DateTime.Now:hh:mm:ss}", msg));
		//	}
		//}

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

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