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

using FilterAPI.Networking;

using FilterZone.ZoneNetworking;
using System.Collections.Generic;

namespace FilterZone.ZoneHandlers
{
    internal class Zone26Handle
    {
        [ZonePacketHandler(26, 1)]
        public static void HandleOpenVendor(ZoneClient ClientSocket, Packet ClientPacket)
        {
            Boolean CanVend = true;
			// zone custom vend area something???
            if (Convert.ToBoolean(Program.Auth.ConfigValues["ZoneFeatureACAS"]))
            {
                foreach (DataRow Row in Program.CustomVendorAreas.Table.Rows.Cast<DataRow>().Where(ItemArray => ItemArray.ItemArray[0].ToString() == ClientSocket.sMap))
                {
                    if (!(ClientSocket.X >= Convert.ToInt32(Row.ItemArray[1])) && ClientSocket.X >= Convert.ToInt32(Row.ItemArray[3]) && !(ClientSocket.Y >= Convert.ToInt32(Row.ItemArray[2])) && ClientSocket.Y >= Convert.ToInt32(Row.ItemArray[4]))
                    {
                        CanVend = true;

                        break;
                    }
                    else { CanVend = false; }
                }
            }

            if (ClientSocket.OpenDateTime > DateTime.Now)
            {
                TimeSpan EditSpan = ClientSocket.OpenDateTime - DateTime.Now;

                if (EditSpan.TotalSeconds > 60)
                {
                    ClientSocket.Dispose();

                    return;
                }

                ClientSocket.EditDateTime = ClientSocket.OpenDateTime.AddSeconds(3);

                GlobalPacket.SendShoutMessageServer(ClientSocket, "Server", "Please wait 3 seconds before trying to open your vendor again.");
            }
            else if (!Program.SQL.HasPermission(ClientSocket.nCharNo, "CanVend"))
            {
                GlobalPacket.SendNearNPCVendor(ClientSocket);
                GlobalPacket.SendShoutMessageServer(ClientSocket, "Server", "Sorry \"{0}\", you are not allowed to open your vendor.", ClientSocket.sID);
            }
            else if (CanVend)
            { // zone vendor something something????
                if (Convert.ToBoolean(Program.Auth.ConfigValues["ZoneFeatureVIC"]))
                {
                    String ShopName = "hWUIQMurh5XTYUSgCbIlrtVjC2tNHC";
                    Byte[] Unk00;

                    if ((ShopName = ClientPacket.ReadString(29)) != "hWUIQMurh5XTYUSgCbIlrtVjC2tNHC" && (Unk00 = ClientPacket.PacketReader.ReadBytes(2)) != null)
                    {
                        Boolean DuplicatedItems = false;

                        List<Byte> Slots = new List<Byte>();

                        for (Int32 Counter = 0; Counter < 25; Counter++)
                        {
                            if ((ClientPacket.PacketReader.BaseStream.Length - ClientPacket.PacketReader.BaseStream.Position) == 0) { break; }

                            UInt16 InventorySlotID = UInt16.MaxValue;
                            UInt16 StoreSlotID = UInt16.MaxValue;
                            Int64 SellCost = -1;
                            Byte[] Unk01;

                            if ((InventorySlotID = ClientPacket.PacketReader.ReadByte()) != UInt16.MaxValue && (StoreSlotID = ClientPacket.PacketReader.ReadByte()) != UInt16.MaxValue && (SellCost = ClientPacket.PacketReader.ReadInt64()) != -1 && (Unk01 = ClientPacket.PacketReader.ReadBytes(2)) != null)
                            {
                                Boolean HasItem = Program.SQL.HasItemInSlot(ClientSocket.nCharNo, InventorySlotID);

                                if (!HasItem)
                                {
                                    ClientSocket.Dispose();

                                    return;
                                }

                                if (!Slots.Contains(Convert.ToByte(InventorySlotID))) { Slots.Add(Convert.ToByte(InventorySlotID)); }
                                else { DuplicatedItems = true; }
                            }
                            else { ClientSocket.Dispose(); }
                        }

                        if (DuplicatedItems)
                        {
                            GlobalPacket.SendNearNPCVendor(ClientSocket);
                            GlobalPacket.SendShoutMessageServer(ClientSocket, "Server", "Same item detected in vendor please correct this.");
                        }
                        else
                        {
                            ClientSocket.OpenDateTime = DateTime.Now.AddSeconds(3);
                            ClientSocket.SendPacketServer(ClientPacket);
                        }
                    }
                    else { ClientSocket.Dispose(); }
                }
                else { ClientSocket.SendPacketServer(ClientPacket); }
            }
            else { GlobalPacket.SendShoutMessageServer(ClientSocket, "Server", "Sorry you cannot open a vendor at your current location, please go to a vendable location."); }
        }

        [ZonePacketHandler(26, 2)]
        public static void HandleVendorOpened(ZoneClient ClientSocket, Packet ServerPacket)
        {
            ClientSocket.SendPacketClient(ServerPacket);
			// zone vendor immortality?
            if (Convert.ToBoolean(Program.Auth.ConfigValues["ZoneFeatureVOI"]))
            {
                UInt16 ID = ServerPacket.PacketReader.ReadUInt16();

                if (ID == 2625)
                {
                    Command.SetAbstate(ClientSocket, "StaImmortal", 7500);

                    ClientSocket.IsVending = true;
                }
            }
        }

        [ZonePacketHandler(26, 4)]
        public static void HandleCloseVendor(ZoneClient ClientSocket, Packet ClientPacket)
        {
            ClientSocket.SendPacketServer(ClientPacket);
            ClientSocket.IsVending = false;
        }

        [ZonePacketHandler(26, 16)]
        public static void HandleEditVendor(ZoneClient ClientSocket, Packet ClientPacket)
        { // zone vendor edit (something) protection
            if (Convert.ToBoolean(Program.Auth.ConfigValues["ZoneFeatureVEBP"]))
            {
                if (ClientSocket.EditDateTime > DateTime.Now)
                {
                    TimeSpan EditSpan = ClientSocket.EditDateTime - DateTime.Now;

                    if (EditSpan.TotalSeconds > 60)
                    {
                        ClientSocket.Dispose();

                        return;
                    }

                    ClientSocket.EditDateTime = ClientSocket.EditDateTime.AddSeconds(5);

                    GlobalPacket.SendShoutMessageServer(ClientSocket, "Server", "Please wait 5 seconds before pressing the edit button again.");
                }
                else
                {
                    ClientSocket.EditDateTime = DateTime.Now.AddSeconds(5);
                    ClientSocket.SendPacketServer(ClientPacket);
                }
            }
            else { ClientSocket.SendPacketServer(ClientPacket); }
        }
    }
}
