using System;
using FilterAPI.Networking;
using System.Collections.Generic;

using FilterZone.ZoneNetworking;

namespace FilterZone
{
    internal class GlobalPacket
    {
        public static void SendNormalMessageClient(ZoneClient ClientSocket, String Message, params Object[] Args)
        {
            String FormattedMessage = String.Format(Message, Args);

            using (var ClientPacket = new Packet(8, 1))
            {
                ClientPacket.PacketWriter.Write(FormattedMessage);

                ClientSocket.SendPacketServer(ClientPacket);
            }
        }

        public static void SendNormalMessageServer(ZoneClient ClientSocket, String Message, params Object[] Args)
        {
            String FormattedMessage = String.Format(Message, Args);

            using (var ServerPacket = new Packet(8, 2))
            {
                ServerPacket.PacketWriter.Write((UInt16)ClientSocket.MapObjectID);
                ServerPacket.PacketWriter.Write((Byte)FormattedMessage.Length);
                ServerPacket.PacketWriter.Write((Byte)2);
                ServerPacket.WriteString(FormattedMessage);

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        public static void SendGMMessageServer(ZoneClient ClientSocket, String Message, params object[] Args)
        {
            String FormattedMessage = String.Format(Message, Args);

            using (var ServerPacket = new Packet(8, 2))
            {
                ServerPacket.PacketWriter.Write((UInt16)ClientSocket.MapObjectID);
                ServerPacket.PacketWriter.Write((Byte)FormattedMessage.Length);
                ServerPacket.PacketWriter.Write((Byte)3);
                ServerPacket.WriteString(FormattedMessage);

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        public static void SendShoutMessageClient(ZoneClient ClientSocket, String Message, params Object[] Args)
        {
            String FormattedMessage = String.Format(Message, Args);

            using (var ServerPacket = new Packet(8, 30))
            {
                ServerPacket.PacketWriter.Write(Message);

                ClientSocket.SendPacketServer(ServerPacket);
            }
        }

        public static void SendShoutMessageServer(ZoneClient ClientSocket, String sID, String Message, params Object[] Args)
        {
            String FormattedMessage = String.Format(Message, Args);

            using (var ServerPacket = new Packet(8, 31))
            {
                ServerPacket.WriteString(sID, 16);
                ServerPacket.PacketWriter.Write((Byte)0);
                ServerPacket.PacketWriter.Write(FormattedMessage);

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        public static void SendRoarMessageClient(ZoneClient ClientSocket, Byte SlotID, String Message, params Object[] Args)
        {
            String FormattedMessage = String.Format(Message, Args);

            using (var ServerPacket = new Packet(8, 75))
            {
                ServerPacket.PacketWriter.Write(SlotID);
                ServerPacket.PacketWriter.Write(Message);

                ClientSocket.SendPacketServer(ServerPacket);
            }
        }

        public static void MoveSlot(ZoneClient ClientSocket, Byte FromSlotID, Byte ToSlotID)
        {
            using (var ServerPacket = new Packet(12, 11))
            {
                ServerPacket.PacketWriter.Write(FromSlotID);
                ServerPacket.PacketWriter.Write((Byte)36);
                ServerPacket.PacketWriter.Write(ToSlotID);
                ServerPacket.PacketWriter.Write((Byte)36);

                ClientSocket.SendPacketServer(ServerPacket);
            }
        }

        public static void EquipSlot(ZoneClient ClientSocket, Byte SlotID)
        {
            using (var ServerPacket = new Packet(12, 15))
            {
                ServerPacket.PacketWriter.Write(SlotID);

                ClientSocket.SendPacketServer(ServerPacket);
            }
        }

        public static void UnEquipSlot(ZoneClient ClientSocket, Byte FromSlotID, Byte ToSlotID)
        {
            using (var ServerPacket = new Packet(12, 18))
            {
                ServerPacket.PacketWriter.Write(FromSlotID);
                ServerPacket.PacketWriter.Write(ToSlotID);

                ClientSocket.SendPacketServer(ServerPacket);
            }
        }

        public static void SendQuestion(ZoneClient ClientSocket, String Text, List<String> Responses)
        {
            using (var ServerPacket = new Packet(15, 1))
            {
                ServerPacket.WriteString(Text, 129);
                ServerPacket.PacketWriter.Write((UInt16)ClientSocket.MapObjectID);
                ServerPacket.PacketWriter.Write(ClientSocket.X);
                ServerPacket.PacketWriter.Write(ClientSocket.Y);
                ServerPacket.PacketWriter.Write((UInt16)100);
                ServerPacket.PacketWriter.Write((Byte)Responses.Count);

                for (Byte Counter = 0; Counter < Responses.Count; Counter++)
                {
                    ServerPacket.PacketWriter.Write(Counter);
                    ServerPacket.WriteString(Responses[Counter], 32);
                }

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        public static void SendCannotUseSkill(ZoneClient ClientSocket)
        {
            using (var ServerPacket = new Packet(9, 52))
            {
                ServerPacket.PacketWriter.Write((UInt16)4033);

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        public static void SendCannotVendNearNPC(ZoneClient ClientSocket)
        {
            using (var ServerPacket = new Packet(26, 2))
            {
                ServerPacket.PacketWriter.Write((UInt16)2643);

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        public static void SendCannotWalkHere(ZoneClient ClientSocket, Int32 X, Int32 Y)
        {
            using (var ServerPacket = new Packet(8, 27))
            {
                ServerPacket.PacketWriter.Write(X);
                ServerPacket.PacketWriter.Write(Y);

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        public static void SendNPCText(ZoneClient ClientSocket, UInt16 NPCID, String Message)
        {
            using (var ServerPacket = new Packet(27, 12))
            {
                ServerPacket.PacketWriter.Write(NPCID);
                ServerPacket.PacketWriter.Write(Message);

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        public static void SendMobChat(ZoneClient ClientSocket, UInt16 MapObjectID, String Message)
        {
            using (var ServerPacket = new Packet(8, 2))
            {
                ServerPacket.PacketWriter.Write(MapObjectID);
                ServerPacket.PacketWriter.Write((Byte)Message.Length);
                ServerPacket.PacketWriter.Write((Byte)0);
                ServerPacket.WriteString(Message);

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        public static void SendRejectTrade(ZoneClient ClientSocket)
        {
            using (var ServerPacket = new Packet(19, 3)) { ClientSocket.SendPacketServer(ServerPacket); }
        }

        public static void SendCloseTrade(ZoneClient ClientSocket)
        {
            using (var ServerPacket = new Packet(19, 10)) { ClientSocket.SendPacketServer(ServerPacket); }
        }

        public static void SendClickNPC(ZoneClient ClientSocket, UInt16 MapObjectID)
        {
            using (var ServerPacket = new Packet(8, 10))
            {
                ServerPacket.PacketWriter.Write(MapObjectID);

                ClientSocket.SendPacketServer(ServerPacket);
            }
        }

        public static void SendPortalYes(ZoneClient ClientSocket)
        {
            using (var ServerPacket = new Packet(15, 2))
            {
                ServerPacket.PacketWriter.Write((Byte)0);

                ClientSocket.SendPacketServer(ServerPacket);
            }
        }

        public static void SendNearNPCVendor(ZoneClient ClientSocket)
        {
            using (var ServerPacket = new Packet(26, 2))
            {
                ServerPacket.PacketWriter.Write((Int16)2643);

                ClientSocket.SendPacketServer(ServerPacket);
            }
        }

        public static void SendCloseNPCWindow(ZoneClient ClientSocket)
        {
            using (var ClientPacket = new Packet(8, 11)) { ClientSocket.SendPacketServer(ClientPacket); }
        }

        public static void SendCloseGuildStorage(ZoneClient ClientSocket)
        {
            using (var ClientPacket = new Packet(8, 111))
            {
                ClientPacket.PacketWriter.Write((Byte)0);

                ClientSocket.SendPacketServer(ClientPacket);
            }
        }

        public static void SendUseItem(ZoneClient ClientSocket, Byte SlotID)
        {
            using (var ClientPacket = new Packet(12, 21))
            {
                ClientPacket.PacketWriter.Write(SlotID);

                ClientSocket.SendPacketServer(ClientPacket);
            }
        }

        public static void SendRemoveItem(ZoneClient ClientSocket, Byte SlotID)
        {
            using (var ServerPacket = new Packet(12, 1))
            {
                ServerPacket.PacketWriter.Write(SlotID);
                ServerPacket.PacketWriter.Write((Byte)36);
                ServerPacket.PacketWriter.Write(SlotID);
                ServerPacket.PacketWriter.Write((Byte)36);
                ServerPacket.PacketWriter.Write(UInt16.MaxValue);

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        public static void SendRGB(ZoneClient ClientSocket, Byte R, Byte G, Byte B)
        {
            using (var ServerPacket = new Packet(27, 10))
            {
                ServerPacket.PacketWriter.Write(R);
                ServerPacket.PacketWriter.Write(G);
                ServerPacket.PacketWriter.Write(B);

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        public static void Send1TabNPC(ZoneClient ClientSocket, UInt16 MapobjectID, Dictionary<Byte, UInt16> ItemIDs)
        {
            using (var ServerPacket = new Packet(15, 11))
            {
                ServerPacket.PacketWriter.Write((UInt16)ItemIDs.Count);
                ServerPacket.PacketWriter.Write(MapobjectID);

                foreach (Byte ID in ItemIDs.Keys)
                {
                    ServerPacket.PacketWriter.Write(ID);
                    ServerPacket.PacketWriter.Write(ItemIDs[ID]);
                }

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }
    }
}
