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

using FilterAPI;
using FilterAPI.Networking;

using FilterManager.ManagerNetworking;

namespace FilterManager.ManagerHandlers
{
    internal class Manager38Handle
    {
        //[ManagerPacketHandler(38, 31)]
        //public static void HandleAcademyWarp(ManagerClient ClientSocket, Packet ClientPacket)
        //{
        //    String sID = String.Empty;

        //    if ((sID = ClientPacket.ReadString(16)) != String.Empty)
        //    {
        //        if (!Program.SQL.sIDTaken(sID)) { ClientSocket.Dispose(); }
        //        else if (Program.SQL.GettLoggedInChar(sID)) { GlobalPacket.SendWhisperMessageFromServer(ClientSocket, "Server", "That player is not online so you cannot warp to them."); }
        //        {

        //        }
        //    }
        //}

        [ManagerPacketHandler(38, 57)]
        public static void HandleSetAcademyRewards(ManagerClient ClientSocket, Packet ClientPacket)
        { // Academy reward management
            if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeatureARM"]))
            {
                Byte TotalChanges = 255;

                if ((TotalChanges = ClientPacket.PacketReader.ReadByte()) != 255)
                {
                    Boolean SendPacket = true;

                    for (Byte Counter = 0; Counter < TotalChanges; Counter++)
                    {
                        Byte StartLevel = 255;
                        Int64 Amount = -1;

                        if ((StartLevel = ClientPacket.PacketReader.ReadByte()) != 255 && (Amount = ClientPacket.PacketReader.ReadInt64()) != -1)
                        {
                            DataRow AcademyMoneyRewardRow = Program.AcademyMoneyRewards.Table.Rows.Cast<DataRow>().Where(ItemArray => Convert.ToByte(ItemArray.ItemArray[0]) == StartLevel).FirstOrDefault();

                            if (Amount > Convert.ToInt64(AcademyMoneyRewardRow.ItemArray[1]))
                            {
                                GlobalPacket.SendWhisperMessageFromServer(ClientSocket, "Server", AcademyMoneyRewardRow.ItemArray[2].ToString());

                                SendPacket = false;
                            }
                        }

                        if (SendPacket) { ClientSocket.SendPacketServer(ClientPacket); }
                    }
                }
                else { ClientSocket.Dispose(); }
            }
            else { ClientSocket.SendPacketServer(ClientPacket); }
        }

        [ManagerPacketHandler(38, 104)]
        public static void HandleAcademyMessage(ManagerClient ClientSocket, Packet ClientPacket)
        {
            String Message = String.Empty;

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

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

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

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