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

using FilterAPI;
using FilterAPI.Networking;

using FilterManager.ManagerNetworking;

namespace FilterManager.ManagerHandlers
{
    internal class Manager8Handle
    {
        [ManagerPacketHandler(8, 12)]
        public static void HandleWhisperMessage(ManagerClient ClientSocket, Packet ClientPacket)
        {
            String Receiver = String.Empty;
            String Message = String.Empty;

            if ((Receiver = ClientPacket.ReadString(16)) != String.Empty && (Message = ClientPacket.PacketReader.ReadString()) != String.Empty)
            {
                if (Program.ManagerClients.Where(sID => sID.Value.sID == Receiver) == null) { GlobalPacket.SendWhisperMessageFromServer(ClientSocket, "Server", String.Format("{0} is not online.", Receiver)); }
                else
                {
                    Message = Message.Replace("?", "?");

                    Boolean SendMessage = true;
					// whisper chat protection
                    if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeatureWCBP"]))
                    {
                        Char InvalidSymbol;

                        if (Injection.ContainsInvalidSymbol(Message, out InvalidSymbol))
                        {
                            GlobalPacket.SendWhisperMessageFromServer(ClientSocket, "Server", "Please do not use \"{0}\" in your message.", InvalidSymbol);

                            SendMessage = false;
                        }
                    }
					// whisper chat log
                    if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeatureWCL"])) { Program.SQL.InserttWhisper(ClientSocket.sID, Receiver, Message); }
					// whisper slander filter
                    if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeatureWSF"]))
                    {
                        foreach (DataRow Row in Program.SlanderFilter.Table.Rows)
                        {
                            if (Convert.ToBoolean(Row.ItemArray[3]) && Message.ToLower().Contains(Convert.ToString(Row.ItemArray[0])))
                            {
                                if (Convert.ToBoolean(Row.ItemArray[2])) { Message = Message.Replace(Convert.ToString(Row.ItemArray[0]), Convert.ToString(Row.ItemArray[1])); }
                                else
                                {
                                    GlobalPacket.SendWhisperMessageFromServer(ClientSocket, "Server", "You are not allowed to say {0} in your message.", Row.ItemArray[0]);

                                    SendMessage = false;
                                }
                            }
                        }
                    }

                    if (SendMessage) { ClientSocket.SendPacketServer(ClientPacket); }
                }
            }
        }

        [ManagerPacketHandler(8, 20)]
        public static void HandlePartyMessage(ManagerClient ClientSocket, Packet ClientPacket)
        {
            String Message = String.Empty;

            if ((Message = ClientPacket.PacketReader.ReadString()) != String.Empty)
            {
                Message = Message.Replace("?", "?");

                Boolean SendMessage = true;
				// party chat protection
                if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeaturePCBP"]))
                {
                    Char InvalidSymbol;

                    if (Injection.ContainsInvalidSymbol(Message, out InvalidSymbol))
                    {
                        GlobalPacket.SendWhisperMessageFromServer(ClientSocket, "Server", "Please do not use \"{0}\" in your message.", InvalidSymbol);

                        SendMessage = false;
                    }
                }
				// party chat log
                if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeaturePCL"])) { Program.SQL.InserttParty(ClientSocket.sID, Message); }
				// party slander filter
                if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeaturePSF"]))
                {
                    foreach (DataRow Row in Program.SlanderFilter.Table.Rows)
                    {
                        if (Convert.ToBoolean(Row.ItemArray[4]) && Message.ToLower().Contains(Convert.ToString(Row.ItemArray[0])))
                        {
                            if (Convert.ToBoolean(Row.ItemArray[2])) { Message = Message.Replace(Convert.ToString(Row.ItemArray[0]), Convert.ToString(Row.ItemArray[1])); }
                            else
                            {
                                GlobalPacket.SendWhisperMessageFromServer(ClientSocket, "Server", "You are not allowed to say {0} in your message.", Row.ItemArray[0]);

                                SendMessage = false;
                            }
                        }
                    }
                }

                if (SendMessage) { ClientSocket.SendPacketServer(ClientPacket); }
            }
        }
    }
}
