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

using FilterAPI;
using FilterAPI.Networking;

using FilterManager.ManagerNetworking;

namespace FilterManager.ManagerHandlers
{
    internal class Manager29Handle
    {
        [ManagerPacketHandler(29, 115)]
        public static void HandleGuildMessage(ManagerClient ClientSocket, Packet ClientPacket)
        {
            String Message = String.Empty;

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

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

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

                        SendMessage = false;
                    }
                }
				// guild chat log
                if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeatureGCL"])) { Program.SQL.InserttGuild(ClientSocket.sID, Message); }
				// guild slander filter
                if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeatureGSF"]))
                {
                    foreach (DataRow Row in Program.SlanderFilter.Table.Rows)
                    {
                        if (Convert.ToBoolean(Row.ItemArray[5]) && 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); }
            }
        }
    }
}
