using System;
using System.Linq;
using System.Data;

using FilterAPI.Networking;

using FilterZone.LocalNetworking;

using FilterZone.ZoneNetworking;

namespace FilterZone.LocalHandlers
{
    internal class Local5Handle
    {
        [LocalPacketHandler(5, 3)]
        public static void HandleSpawnMobAll(LocalClient ClientSocket, Packet ClientPacket)
        {
            String MobIndex = ClientPacket.PacketReader.ReadString();
            Int32 MobAmount = ClientPacket.PacketReader.ReadInt32();
            Boolean ShowMessage = ClientPacket.PacketReader.ReadBoolean();
            String sID = ClientPacket.PacketReader.ReadString();

            String MobName = Convert.ToString(Program.MobInfo.Table.Rows.Cast<DataRow>().Where(ItemArray => ItemArray.ItemArray[1].ToString() == MobIndex).FirstOrDefault().ItemArray[2]);

            foreach (ZoneClient ZClient in Program.ZoneClients.Values)
            {
                for (Int32 Counter = 0; Counter < MobAmount; Counter++) { Command.Mobbreed(ZClient, MobIndex, 1, 0, false, 1); }

                if (ShowMessage) { GlobalPacket.SendShoutMessageServer(ZClient, "Server", "{0} has spawned {1}x {2} at your location.", sID, MobAmount, MobName); }
            }
        }

        [LocalPacketHandler(5, 4)]
        public static void HandleSpawnMobPlayer(LocalClient ClientSocket, Packet ClientPacket)
        {
            String SpawnsID = ClientPacket.PacketReader.ReadString();
            String MobIndex = ClientPacket.PacketReader.ReadString();
            Int32 MobAmount = ClientPacket.PacketReader.ReadInt32();
            Boolean ShowMessage = ClientPacket.PacketReader.ReadBoolean();
            String sID = ClientPacket.PacketReader.ReadString();

            String MobName = Convert.ToString(Program.MobInfo.Table.Rows.Cast<DataRow>().Where(ItemArray => ItemArray.ItemArray[1].ToString() == MobIndex).FirstOrDefault().ItemArray[2]);

            ZoneClient ZClient;

            if ((ZClient = Program.ZoneClients.Values.Where(Value => Value.sID == SpawnsID).FirstOrDefault()) != null)
            {
                for (Int32 Counter = 0; Counter < MobAmount; Counter++) { Command.Mobbreed(ZClient, MobIndex, 1, 0, false, 1); }

                if (ShowMessage) { GlobalPacket.SendShoutMessageServer(ZClient, "Server", "{0} has spawned {1}x {2} at your location.", sID, MobAmount, MobName); }
            }
        }

        [LocalPacketHandler(5, 5)]
        public static void HandleItemAll(LocalClient ClientSocket, Packet ClientPacket)
        {
            String ItemIndex = ClientPacket.PacketReader.ReadString();
            Boolean HasAmount = ClientPacket.PacketReader.ReadBoolean();
            Byte Amount = ClientPacket.PacketReader.ReadByte();
            Boolean HasUpgrade = ClientPacket.PacketReader.ReadBoolean();
            Byte Upgrade = ClientPacket.PacketReader.ReadByte();
            Boolean ShowMessage = ClientPacket.PacketReader.ReadBoolean();
            String sID = ClientPacket.PacketReader.ReadString();

            String ItemName = Convert.ToString(Program.ItemInfo.Table.Rows.Cast<DataRow>().Where(ItemArray => ItemArray.ItemArray[1].ToString() == ItemIndex).FirstOrDefault().ItemArray[2]);

            foreach (ZoneClient ZClient in Program.ZoneClients.Values)
            {
                if (!HasAmount && !HasUpgrade) { Command.Makeitem(ZClient, ItemIndex); }
                else if (HasAmount && !HasUpgrade) { Command.Makeitem(ZClient, ItemIndex, Amount); }
                else if (!HasAmount && HasUpgrade) { Command.Makeitem(ZClient, ItemIndex, true, Upgrade); }
                else { Command.Makeitem(ZClient, ItemIndex, Amount, Upgrade); }

                if (ShowMessage)
                {
                    if (!HasAmount) { Amount = Convert.ToByte(Program.ItemInfo.Table.Rows.Cast<DataRow>().Where(ItemArray => ItemArray.ItemArray[1].ToString() == ItemIndex).FirstOrDefault().ItemArray[5]); }

                    if (ShowMessage && HasUpgrade) { GlobalPacket.SendShoutMessageServer(ZClient, "Server", "{0} has given you {1}x {2} [+{3}].", sID, Amount, ItemName, Upgrade); }
                    else if (ShowMessage) { GlobalPacket.SendShoutMessageServer(ZClient, "Server", "{0} has given you {1}x {2}.", sID, Amount, ItemName); }
                }
            }
        }

        [LocalPacketHandler(5, 6)]
        public static void HandleGiveItem(LocalClient ClientSocket, Packet ClientPacket)
        {
            String SpawnsID = ClientPacket.PacketReader.ReadString();
            String ItemIndex = ClientPacket.PacketReader.ReadString();
            Boolean HasAmount = ClientPacket.PacketReader.ReadBoolean();
            Byte Amount = ClientPacket.PacketReader.ReadByte();
            Boolean HasUpgrade = ClientPacket.PacketReader.ReadBoolean();
            Byte Upgrade = ClientPacket.PacketReader.ReadByte();
            Byte RunAmount = ClientPacket.PacketReader.ReadByte();
            Boolean ShowMessage = ClientPacket.PacketReader.ReadBoolean();
            String sID = ClientPacket.PacketReader.ReadString();

            String ItemName = Convert.ToString(Program.ItemInfo.Table.Rows.Cast<DataRow>().Where(ItemArray => ItemArray.ItemArray[1].ToString() == ItemIndex).FirstOrDefault().ItemArray[2]);

            ZoneClient ZClient;

            if ((ZClient = Program.ZoneClients.Values.Where(Value => Value.sID == SpawnsID).FirstOrDefault()) != null)
            {
                if (!HasAmount && !HasUpgrade)
                {
                    for (Byte Counter = 0; Counter < RunAmount; Counter++) { Command.Makeitem(ZClient, ItemIndex); }
                }
                else if (HasAmount && !HasUpgrade)
                {
                    for (Byte Counter = 0; Counter < RunAmount; Counter++) { Command.Makeitem(ZClient, ItemIndex, Amount); }
                }
                else if (!HasAmount && HasUpgrade)
                {
                    for (Byte Counter = 0; Counter < RunAmount; Counter++) { Command.Makeitem(ZClient, ItemIndex, true, Upgrade); }
                }
                else
                {
                    for (Byte Counter = 0; Counter < RunAmount; Counter++) { Command.Makeitem(ZClient, ItemIndex, Amount, Upgrade); }
                }

                if (ShowMessage)
                {
                    if (!HasAmount) { Amount = Convert.ToByte(Program.ItemInfo.Table.Rows.Cast<DataRow>().Where(ItemArray => ItemArray.ItemArray[1].ToString() == ItemIndex).FirstOrDefault().ItemArray[5]); }

                    if (ShowMessage && HasUpgrade) { GlobalPacket.SendShoutMessageServer(ZClient, "Server", "{0} has given you {1}x\"{2}\" [+{3}].", sID, Amount * RunAmount, ItemName, Upgrade); }
                    else if (ShowMessage) { GlobalPacket.SendShoutMessageServer(ZClient, "Server", "{0} has given you {1}x\"{2}\".", sID, Amount * RunAmount, ItemName); }
                }
            }
        }

        [LocalPacketHandler(5, 7)]
        public static void HandleTp(LocalClient ClientSocket, Packet ClientPacket)
        {
            String sID = ClientPacket.PacketReader.ReadString();
            String TosID = ClientPacket.PacketReader.ReadString();

            ZoneClient ZClient;

            if ((ZClient = Program.ZoneClients.Values.Where(Value => Value.sID == sID).FirstOrDefault()) != null) { Command.Pfind(ZClient, TosID); }
        }

        [LocalPacketHandler(5, 8)]
        public static void HandleTpAll(LocalClient ClientSocket, Packet ClientPacket)
        {
            String sID = ClientPacket.PacketReader.ReadString(); 

            foreach (ZoneClient ZClient in Program.ZoneClients.Values)
            {
                if (ZClient.sID != sID) { Command.Pfind(ZClient, sID); }
            }
        }

        [LocalPacketHandler(5, 9)]
        public static void HandleMap(LocalClient ClientSocket, Packet ClientPacket)
        {
            String MapIndex = ClientPacket.PacketReader.ReadString();
            Boolean HasXY = ClientPacket.PacketReader.ReadBoolean();
            Int32 X = ClientPacket.PacketReader.ReadInt32();
            Int32 Y = ClientPacket.PacketReader.ReadInt32();
            String sID = ClientPacket.PacketReader.ReadString();

            ZoneClient ZClient;

            if ((ZClient = Program.ZoneClients.Values.Where(Value => Value.sID == sID).FirstOrDefault()) != null)
            {
                if (HasXY) { Command.Linkto(ZClient, MapIndex, X, Y); }
                else { Command.Linkto(ZClient, MapIndex); }
            }
        }

        [LocalPacketHandler(5, 10)]
        public static void HandleSetLevel(LocalClient ClientSocket, Packet ClientPacket)
        {
            String sID = ClientPacket.PacketReader.ReadString();
            Byte SetnLevel = ClientPacket.PacketReader.ReadByte();

            ZoneClient ZClient;

            if ((ZClient = Program.ZoneClients.Values.Where(Value => Value.sID == sID).FirstOrDefault()) != null)
            {
                Byte nLevel = Program.SQL.GetnLevelFromsID(sID);

                if (SetnLevel > nLevel) { Command.Levelup(ZClient, Convert.ToByte(SetnLevel - nLevel)); }
                else
                {
                    Command.Levelup(ZClient, Convert.ToByte(256 - nLevel));
                    Command.Levelup(ZClient, SetnLevel);
                }
            }
        }

        [LocalPacketHandler(5, 11)]
        public static void HandleAbstate(LocalClient ClientSocket, Packet ClientPacket)
        {
            String AbstateIndex = ClientPacket.PacketReader.ReadString();
            Int32 Duration = ClientPacket.PacketReader.ReadInt32();
            Byte Effect = ClientPacket.PacketReader.ReadByte();
            String sID = ClientPacket.PacketReader.ReadString();

            ZoneClient ZClient;

            if ((ZClient = Program.ZoneClients.Values.Where(Value => Value.sID == sID).FirstOrDefault()) != null)
            {
                if (Duration == 0 && Effect == 0) { Command.SetAbstate(ZClient, AbstateIndex); }
                else if (Duration != 0 && Effect == 0) { Command.SetAbstate(ZClient, AbstateIndex, Duration); }
                else { Command.SetAbstate(ZClient, AbstateIndex, Duration, Effect); }
            }
        }

        [LocalPacketHandler(5, 12)]
        public static void HandleKQReward(LocalClient ClientSocket, Packet ClientPacket)
        {
            String sID = ClientPacket.PacketReader.ReadString();
            Byte KQID = ClientPacket.PacketReader.ReadByte();

            ZoneClient ZClient;

            if ((ZClient = Program.ZoneClients.Values.Where(Value => Value.sID == sID).FirstOrDefault()) != null) { Command.KQReward(ZClient, KQID); }
        }

        [LocalPacketHandler(5, 13)]
        public static void HandleRunCommand(LocalClient ClientSocket, Packet ClientPacket)
        {
            String sID = ClientPacket.PacketReader.ReadString();
            String sCommand = ClientPacket.PacketReader.ReadString();

            ZoneClient ZClient;

            if ((ZClient = Program.ZoneClients.Values.Where(Value => Value.sID == sID).FirstOrDefault()) != null) { Command.HandleCommand(ZClient, sCommand, true); }
        }

        [LocalPacketHandler(5, 14)]
        public static void HandleCanWalk(LocalClient ClientSocket, Packet ClientPacket)
        {
            String sID = ClientPacket.PacketReader.ReadString();
            Boolean CanWalk = ClientPacket.PacketReader.ReadBoolean();

            ZoneClient ZClient;

            if ((ZClient = Program.ZoneClients.Values.Where(Value => Value.sID == sID).FirstOrDefault()) != null) { ZClient.CanWalk = CanWalk; }
        }

        [LocalPacketHandler(5, 15)]
        public static void HandleLearnSkill(LocalClient ClientSocket, Packet ClientPacket)
        {
            String sID = ClientPacket.PacketReader.ReadString();
            Int32 nSkillNo = ClientPacket.PacketReader.ReadInt32();

            ZoneClient ZClient;

            if ((ZClient = Program.ZoneClients.Values.Where(Value => Value.sID == sID).FirstOrDefault()) != null) { Command.LearnSkill(ZClient, nSkillNo); }
        }

        [LocalPacketHandler(5, 16)]
        public static void HandleLearnPassive(LocalClient ClientSocket, Packet ClientPacket)
        {
            String sID = ClientPacket.PacketReader.ReadString();
            Int32 nSkillNo = ClientPacket.PacketReader.ReadInt32();

            ZoneClient ZClient;

            if ((ZClient = Program.ZoneClients.Values.Where(Value => Value.sID == sID).FirstOrDefault()) != null) { Command.LearnPassive(ZClient, nSkillNo); }
        }

        [LocalPacketHandler(5, 17)]
        public static void HandleSetRGB(LocalClient ClientSocket, Packet ClientPacket)
        {
            Byte R = ClientPacket.PacketReader.ReadByte();
            Byte G = ClientPacket.PacketReader.ReadByte();
            Byte B = ClientPacket.PacketReader.ReadByte();

            Program.R = R;
            Program.G = G;
            Program.B = B;

            foreach (ZoneClient ZClient in Program.ZoneClients.Values) { GlobalPacket.SendRGB(ZClient, R, G, B); }
        }
    }
}
 