using System;

using FilterAPI;
using FilterAPI.Networking;

using FilterCentral.LocalNetworking;

namespace FilterCentral.LocalHandlers
{
    internal class Local2Handle
    {
        [LocalPacketHandler(2, 1)]
        public static void HandleName(LocalClient ClientSocket, Packet ClientPacket)
        {
            String Name = ClientPacket.PacketReader.ReadString();

            if (Name == "FilterLogin")
            {
                Program.LoginClient = ClientSocket;

                Program.WriteLine(ConsoleColor.Magenta, "[Central]: Assigned login connection.");
			}
            else if (Name == "FilterManager")
            {
                Program.ManagerClient = ClientSocket;

                Program.WriteLine(ConsoleColor.Magenta, "[Central]: Assigned manager connection.");
			}
            else if (Name.StartsWith("FilterZone"))
            {
                String ZoneID = Name.TrimStart("FilterZone".ToCharArray());

                if (Program.ZoneClients.ContainsKey(ZoneID))
                {
                    Program.ZoneClients[ZoneID].Dispose();
                    Program.ZoneClients.Remove(ZoneID);
                }

                Program.ZoneClients.Add(ZoneID, ClientSocket);

                Program.WriteLine(ConsoleColor.Magenta, "[Central]: Assigned zone ({0}) connection.", ZoneID);
			}
			else if (Name == "FilterLauncher")
			{
				Program.LauncherClient = ClientSocket;

				Program.WriteLine(ConsoleColor.Magenta, "[Central]: Assigned launcher connection.");
			}

            using (var ClientPacketRewrite = new Packet(2, 2)) { ClientSocket.SendPacket(ClientPacketRewrite); }
        }

        [LocalPacketHandler(2, 3)]
        public static void HandleConsoleText(LocalClient ClientSocket, Packet ClientPacket)
        {
            Byte ConsoleColorID = ClientPacket.PacketReader.ReadByte();
            String ServiceName = ClientPacket.PacketReader.ReadString();
            String Text = ClientPacket.PacketReader.ReadString();
            
            Console.ForegroundColor = ConsoleColorConverter.IDToConsoleColor(ConsoleColorID);
            Console.WriteLine(String.Format("[{0}]: {1}", ServiceName, Text));
			Console.ResetColor();
		}
	}
}
