using System;
using System.Linq;
using System.Collections.Generic;

using FilterAPI.Networking;

using FilterZone.ZoneNetworking;

namespace FilterZone.ZoneHandlers
{
    internal class Zone7Handle
    {
        [ZonePacketHandler(7, 6)]
        public static void HandleSpawnSinglePlayer(ZoneClient ClientSocket, Packet ServerPacket)
        {
            if (ClientSocket.ShowPlayers)
            {
                ClientSocket.SendPacketClient(ServerPacket);

                UInt16 MapObjectID = ServerPacket.PacketReader.ReadUInt16();
                String sID = ServerPacket.ReadString(16);

                ClientSocket.MapObjects.RemoveAll(MOID => MOID.MapObjectID == MapObjectID);

                ClientSocket.MapObjects.Add(new MapObject
                {
                    MapObjectID = MapObjectID,
                    sID = sID
                });
            }
        }

        [ZonePacketHandler(7, 7)]
        public static void HandleSpawnMultiPlayer(ZoneClient ClientSocket, Packet ServerPacket)
        {
            if (ClientSocket.ShowPlayers)
            {
                ClientSocket.SendPacketClient(ServerPacket);

                Byte Total = ServerPacket.PacketReader.ReadByte();

                for (Int32 Counter = 0; Counter < Total; Counter++)
                {
                    UInt16 MapObjectID = ServerPacket.PacketReader.ReadUInt16();
                    String sID = ServerPacket.ReadString(16);
                    ServerPacket.PacketReader.ReadBytes(129);

                    ClientSocket.MapObjects.RemoveAll(MOID => MOID.MapObjectID == MapObjectID);

                    ClientSocket.MapObjects.Add(new MapObject
                    {
                        MapObjectID = MapObjectID,
                        sID = sID
                    });
                }
            }
        }

        [ZonePacketHandler(7, 8)]
        public static void HandleSpawnSingleObject(ZoneClient ClientSocket, Packet ServerPacket)
        {
            ClientSocket.SendPacketClient(ServerPacket);

            UInt16 MapObjectID = ServerPacket.PacketReader.ReadUInt16();
            ServerPacket.PacketReader.ReadByte();
            UInt16 MobID = ServerPacket.PacketReader.ReadUInt16();

            ClientSocket.MapObjects.RemoveAll(MOID => MOID.MapObjectID == MapObjectID);

            ClientSocket.MapObjects.Add(new MapObject
            {
                MapObjectID = MapObjectID,
                MobID = MobID
            });
        }

        [ZonePacketHandler(7, 9)]
        public static void HandleSpawnMultiObject(ZoneClient ClientSocket, Packet ServerPacket)
        {
            ClientSocket.SendPacketClient(ServerPacket);

            Byte MapObjectIDs = ServerPacket.PacketReader.ReadByte();

            for (Int32 Counter = 0; Counter < MapObjectIDs; Counter++)
            {
                UInt16 MapObjectID = ServerPacket.PacketReader.ReadUInt16();
                ServerPacket.PacketReader.ReadByte();
                UInt16 MobID = ServerPacket.PacketReader.ReadUInt16();
                Int32 X = ServerPacket.PacketReader.ReadInt32();
                Int32 Y = ServerPacket.PacketReader.ReadInt32();
                ServerPacket.PacketReader.ReadBytes(54);

                ClientSocket.MapObjects.RemoveAll(MOID => MOID.MapObjectID == MapObjectID);

                ClientSocket.MapObjects.Add(new MapObject
                {
                    MapObjectID = MapObjectID,
                    MobID = MobID,
                    X = X,
                    Y = Y
                });
            }
        }

        [ZonePacketHandler(7, 10)]
        public static void HandleShowDrop(ZoneClient ClientSocket, Packet ServerPacket)
        {
            ClientSocket.SendPacketClient(ServerPacket);

            UInt16 MapObjectID = ServerPacket.PacketReader.ReadUInt16();
            UInt16 ItemID = ServerPacket.PacketReader.ReadUInt16();

            ClientSocket.MapObjects.RemoveAll(MOID => MOID.MapObjectID == MapObjectID);

            ClientSocket.MapObjects.Add(new MapObject
            {
                MapObjectID = MapObjectID,
                ItemID = ItemID
            });
        }

        [ZonePacketHandler(7, 11)]
        public static void HandleShowDrops(ZoneClient ClientSocket, Packet ServerPacket)
        {
            ClientSocket.SendPacketClient(ServerPacket);

            Byte ItemAmount = ServerPacket.PacketReader.ReadByte();

            for (Int32 Counter = 0; Counter < ItemAmount; Counter++)
            {
                UInt16 MapObjectID = ServerPacket.PacketReader.ReadUInt16();
                UInt16 ItemID = ServerPacket.PacketReader.ReadUInt16();
                ServerPacket.PacketReader.ReadBytes(11);

                ClientSocket.MapObjects.RemoveAll(MOID => MOID.MapObjectID == MapObjectID);

                ClientSocket.MapObjects.Add(new MapObject
                {
                    MapObjectID = MapObjectID,
                    ItemID = ItemID
                });
            }
        }
    }
}
