using System;
using System.Linq;

using FilterAPI.Networking;

using FilterManager.ManagerNetworking;

namespace FilterManager.ManagerHandlers
{
    internal class Manager14Handle
    {
        [ManagerPacketHandler(14, 2)]
        public static void HandlePartyInvite(ManagerClient ClientSocket, Packet ClientPacket)
        {
            if (ClientSocket.sID != String.Empty)
            {
                String sID = String.Empty;

                if ((sID = ClientPacket.ReadString(16)) != String.Empty)
                {
                    if (Program.SQL.HasPermission(Program.SQL.GetnCharNoFromsID(sID), "BypassChecks")) { ClientSocket.SendPacketServer(ClientPacket); }
                    else if (Program.SQL.sIDTaken(sID)) { ClientSocket.SendPacketServer(ClientPacket); }
                    else { ClientSocket.Dispose(); }
                }
            }
        }

        [ManagerPacketHandler(14, 4)]
        public static void HandleJoinParty(ManagerClient ClientSocket, Packet ClientPacket)
        {
            if (ClientSocket.sID != String.Empty)
            {
                String sID = String.Empty;

                if ((sID = ClientPacket.ReadString(16)) != String.Empty)
                {
                    if (Program.SQL.HasPermission(Program.SQL.GetnCharNoFromsID(sID), "BypassChecks")) { ClientSocket.SendPacketServer(ClientPacket); }
                    else if (Program.SQL.sIDTaken(sID)) { ClientSocket.SendPacketServer(ClientPacket); }
                    else { ClientSocket.Dispose(); }
                }
            }
        }

        [ManagerPacketHandler(14, 7)]
        public static void HandlePartyMemberJoined(ManagerClient ClientSocket, Packet ServerPacket)
        {
            String sID = ServerPacket.ReadString(16);

            ClientSocket.SendPacketClient(ServerPacket);

            if (ClientSocket.PartyMaster == String.Empty)
            {
                ClientSocket.PartyMaster = ClientSocket.sID;
                ClientSocket.PartyMembers.Add(sID);

                Program.SQL.DeletetParties(ClientSocket.nCharNo);
                Program.SQL.InserttParties(ClientSocket.nCharNo, ClientSocket.PartyMaster, ClientSocket.PartyMembers);
            }
            else if (!ClientSocket.PartyMembers.Contains(sID))
            {
                ClientSocket.PartyMembers.Add(sID);

                Program.SQL.DeletetParties(ClientSocket.nCharNo);
                Program.SQL.InserttParties(ClientSocket.nCharNo, ClientSocket.PartyMaster, ClientSocket.PartyMembers);
            }
        }

        [ManagerPacketHandler(14, 9)]
        public static void HandlePartyInfo(ManagerClient ClientSocket, Packet ServerPacket)
        {
            if (ClientSocket.sID != String.Empty)
            {
                ClientSocket.PartyMaster = String.Empty;
                ClientSocket.PartyMembers.Clear();

                Byte PartyMembers = ServerPacket.PacketReader.ReadByte();

                for (Byte Counter = 0; Counter < PartyMembers; Counter++)
                {
                    String sID = ServerPacket.ReadString(16);
                    ServerPacket.PacketReader.ReadByte();

                    if (Counter == 0) { ClientSocket.PartyMaster = sID; }
                    else { ClientSocket.PartyMembers.Add(sID); }
                }

                Program.SQL.DeletetParties(ClientSocket.nCharNo);
                Program.SQL.InserttParties(ClientSocket.nCharNo, ClientSocket.PartyMaster, ClientSocket.PartyMembers);

                ClientSocket.SendPacketClient(ServerPacket);
            }
        }

        [ManagerPacketHandler(14, 11)]
        public static void HandlePartyMemberLeave(ManagerClient ClientSocket, Packet ServerPacket)
        {
            String sID = ServerPacket.ReadString(16);

            if (ClientSocket.sID == sID || ClientSocket.PartyMembers.Count == 1)
            {
                ClientSocket.PartyMaster = String.Empty;
                ClientSocket.PartyMembers.Clear();

                Program.SQL.DeletetParties(ClientSocket.nCharNo);
            }
            else if (ClientSocket.PartyMaster == sID)
            {
                ClientSocket.PartyMaster = String.Empty;

                Program.SQL.DeletetParties(ClientSocket.nCharNo);
                Program.SQL.InserttParties(ClientSocket.nCharNo, ClientSocket.PartyMaster, ClientSocket.PartyMembers);
            }
            else
            {
                ClientSocket.PartyMembers.Remove(sID);

                Program.SQL.DeletetParties(ClientSocket.nCharNo);
                Program.SQL.InserttParties(ClientSocket.nCharNo, ClientSocket.PartyMaster, ClientSocket.PartyMembers);
            }

            ClientSocket.SendPacketClient(ServerPacket);
        }

        [ManagerPacketHandler(14, 21)]
        public static void HandlePartyMemberKicked(ManagerClient ClientSocket, Packet ServerPacket)
        {
            String sID = ServerPacket.ReadString(16);

            if (ClientSocket.sID == sID || ClientSocket.PartyMembers.Count == 1)
            {
                ClientSocket.PartyMaster = String.Empty;
                ClientSocket.PartyMembers.Clear();

                Program.SQL.DeletetParties(ClientSocket.nCharNo);
            }
            else
            {
                ClientSocket.PartyMembers.Remove(sID);

                Program.SQL.DeletetParties(ClientSocket.nCharNo);
                Program.SQL.InserttParties(ClientSocket.nCharNo, ClientSocket.PartyMaster, ClientSocket.PartyMembers);
            }

            ClientSocket.SendPacketClient(ServerPacket);
        }

        [ManagerPacketHandler(14, 20)]
        public static void HandleBanMember(ManagerClient ClientSocket, Packet ClientPacket)
        {
            if (ClientSocket.sID != String.Empty)
            {
                String sID = String.Empty;

                if ((sID = ClientPacket.ReadString(16)) != String.Empty)
                {
                    if (Program.SQL.HasPermission(Program.SQL.GetnCharNoFromsID(sID), "BypassChecks")) { ClientSocket.SendPacketServer(ClientPacket); }
                    else if (Program.SQL.sIDTaken(sID)) { ClientSocket.SendPacketServer(ClientPacket); }
                    else { ClientSocket.Dispose(); }
                }
            }
        }

        [ManagerPacketHandler(14, 40)]
        public static void HandleChangeRole(ManagerClient ClientSocket, Packet ClientPacket)
        {
            if (ClientSocket.sID != String.Empty)
            {
                String sID = String.Empty;

                if ((sID = ClientPacket.ReadString(16)) != String.Empty)
                {
                    if (Program.SQL.HasPermission(Program.SQL.GetnCharNoFromsID(sID), "BypassChecks")) { ClientSocket.SendPacketServer(ClientPacket); }
                    else if (Program.SQL.sIDTaken(sID)) { ClientSocket.SendPacketServer(ClientPacket); }
                    else { ClientSocket.Dispose(); }
                }
            }
        }

        [ManagerPacketHandler(14, 41)]
        public static void HandlePartyNewMaster(ManagerClient ClientSocket, Packet ServerPacket)
        {
            String sID = ServerPacket.ReadString(16);

            if (ClientSocket.PartyMaster != String.Empty) { ClientSocket.PartyMembers.Add(ClientSocket.PartyMaster); }

            ClientSocket.PartyMembers.Remove(sID);
            ClientSocket.PartyMaster = sID;

            Program.SQL.DeletetParties(ClientSocket.nCharNo);
            Program.SQL.InserttParties(ClientSocket.nCharNo, ClientSocket.PartyMaster, ClientSocket.PartyMembers);

            ClientSocket.SendPacketClient(ServerPacket);
        }

        [ManagerPacketHandler(14, 75)]
        public static void HandleSetAllocation(ManagerClient ClientSocket, Packet ClientPacket)
        { // no clue
            if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeatureDA"])) { GlobalPacket.SendAllocation(ClientSocket); }
            else { ClientSocket.SendPacketServer(ClientPacket); }
        }
    }
}
