using System;
using System.Linq;
using System.Threading;

using FilterAPI.Networking;

using FilterManager.LocalNetworking;

using FilterManager.ManagerNetworking;

namespace FilterManager.LocalHandlers
{
    internal class Local4Handle
    {
        [LocalPacketHandler(4, 4)]
        public static void HandleGetnUserNo(LocalClient ClientSocket, Packet ClientPacket)
        {
            Boolean Exists = ClientPacket.PacketReader.ReadBoolean();
            String TransferKey = ClientPacket.PacketReader.ReadString();

            if (Exists) {
				Program.GrabednUserNos.Add(TransferKey, ClientPacket.PacketReader.ReadInt32()); }
            else {
				Program.GrabednUserNos.Add(TransferKey, 0); }
        }

        [LocalPacketHandler(4, 6)]
        public static void HandleDisconnectAll(LocalClient ClientSocket, Packet ClientPacket)
        {
            Boolean ShowMessage = ClientPacket.PacketReader.ReadBoolean();
            String From = ClientPacket.PacketReader.ReadString();
            String Message = ClientPacket.PacketReader.ReadString();

            foreach (ManagerClient MClient in Program.ManagerClients.Values)
            {
                if (ShowMessage) { GlobalPacket.SendWhisperMessageFromServer(MClient, From, Message); }

                Thread.Sleep(100);

                MClient.Dispose();
            }
        }

        [LocalPacketHandler(4, 7)]
        public static void HandleDisconnectPlayer(LocalClient ClientSocket, Packet ClientPacket)
        {
            String sID = ClientPacket.PacketReader.ReadString();
            Boolean ShowMessage = ClientPacket.PacketReader.ReadBoolean();
            String From = ClientPacket.PacketReader.ReadString();
            String Message = ClientPacket.PacketReader.ReadString();

            ManagerClient MClient;

            if ((MClient = Program.ManagerClients.Where(Value => Value.Value.sID == sID).FirstOrDefault().Value) != null)
            {
                if (ShowMessage) { GlobalPacket.SendWhisperMessageFromServer(MClient, From, Message); }

                Thread.Sleep(100);

                MClient.Dispose();
            }
        }

        [LocalPacketHandler(4, 8)]
        public static void HandleMessageAll(LocalClient ClientSocket, Packet ClientPacket)
        {
            Byte Type = ClientPacket.PacketReader.ReadByte();
            String From = ClientPacket.PacketReader.ReadString();
            String Message = ClientPacket.PacketReader.ReadString();

            foreach (ManagerClient MClient in Program.ManagerClients.Values)
            {
                if (Type == 0) { GlobalPacket.SendWhisperMessageFromServer(MClient, From, Message); }
                else { GlobalPacket.SendWorldMessageClient(MClient, Message); }
            }
        }

        [LocalPacketHandler(4, 9)]
        public static void HandleMessagePlayer(LocalClient ClientSocket, Packet ClientPacket)
        {
            String sID = ClientPacket.PacketReader.ReadString();
            Byte Type = ClientPacket.PacketReader.ReadByte();
            String From = ClientPacket.PacketReader.ReadString();
            String Message = ClientPacket.PacketReader.ReadString();

            ManagerClient MClient;

            if ((MClient = Program.ManagerClients.Where(Value => Value.Value.sID == sID).FirstOrDefault().Value) != null)
            {
                if (Type == 0) { GlobalPacket.SendWhisperMessageFromServer(MClient, From, Message); }
                else { GlobalPacket.SendWorldMessageClient(MClient, Message); }
            }
        }
    }
}
