using System;
using System.Data;

using FilterAPI;
using FilterAPI.Networking;

using FilterManager.ManagerNetworking;

namespace FilterManager.ManagerHandlers
{
    internal class Manager5Handle
    {
        [ManagerPacketHandler(5, 1)]
        public static void HandleCreateCharacter(ManagerClient ClientSocket, Packet ClientPacket)
        {
            Int16 Slot = -1;
            String sID = String.Empty;
            Int16 JobGender = -1;
            Int16 Hair = -1;
            Int16 Color = -1;
            Int16 Style = -1;

            if ((Slot = ClientPacket.PacketReader.ReadByte()) != -1 && (sID = ClientPacket.ReadString(16)) != String.Empty && (JobGender = ClientPacket.PacketReader.ReadByte()) != -1 && (Hair = ClientPacket.PacketReader.ReadByte()) != -1 && (Color = ClientPacket.PacketReader.ReadByte()) != -1 && (Style = ClientPacket.PacketReader.ReadByte()) != -1)
            {
                Boolean IsMale = Convert.ToBoolean((Byte)((JobGender >> 7) & 0x01));
                Byte ClassID = (Byte)((JobGender >> 2) & 0x1F);
				// Character Name Protection
                if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeatureCNP"]) && !Injection.ContainsOnlyLetterOrDigit(sID)) { GlobalPacket.SendCharacterNameTaken(ClientSocket); }
                else if (Program.SQL.sIDTaken(sID)) { GlobalPacket.SendCharacterNameTaken(ClientSocket); }
                else
                {
                    ClientSocket.NewCharacter = new Character()
                    {
                        Slot = Slot,
                        sID = sID,
                        JobGender = JobGender,
                        Hair = Hair,
                        Color = Color,
                        Style = Style,
                        IsMale = IsMale,
                        ClassID = ClassID
                    };

                    ClientSocket.SendPacketServer(ClientPacket);
                }
            }
        }

        [ManagerPacketHandler(5, 6)]
        public static void HandleCharacterCreated(ManagerClient ClientSocket, Packet ServerPacket)
        {	// Character Creation override (?)
            if (Convert.ToBoolean(Program.Auth.ConfigValues["ManagerFeatureCCC"]))
            {
                Int32 nCharNo = Program.SQL.GetnCharNoFromsID(ClientSocket.NewCharacter.sID);

                foreach (DataRow Row in Program.CharacterCreate.Table.Rows)
                {
                    Byte ClassID = Convert.ToByte(Row.ItemArray[0]);
                    String Type = Convert.ToString(Row.ItemArray[1]);
                    Int32 ID = Convert.ToUInt16(Row.ItemArray[2]);
                    UInt32 Option1 = Convert.ToUInt32(Row.ItemArray[3]);
                    UInt32 Option2 = Convert.ToUInt32(Row.ItemArray[4]);
                    String Message = Convert.ToString(Row.ItemArray[5]);

                    if (ClassID == 0 || ClientSocket.NewCharacter.ClassID == ClassID)
                    {
                        switch (Type)
                        {
                            case "MASTER":
                                {
                                    Program.SQL.InserttHolyPromise(nCharNo, ID);

                                    break;
                                }
                            case "FRIEND":
                                {
                                    Program.SQL.InserttFriend(nCharNo, ID);

                                    break;
                                }
                            case "FAME":
                                {
                                    Program.SQL.AddnFameFornCharNo(nCharNo, Convert.ToInt32(Option1));

                                    break;
                                }
                            case "MONEY":
                                {
                                    Program.SQL.AddnMoneyFornCharNo(nCharNo, Option1 * Option2);

                                    break;
                                }
                            case "LEVEL":
                                {
                                    Program.SQL.SetnLevelFornCharNo(nCharNo, Convert.ToByte(Option1));

                                    break;
                                }
                            case "EXP":
                                {
                                    Program.SQL.AddnExpFornCharNo(nCharNo, Option1 * Option2);

                                    break;
                                }
                            case "ADMINLEVEL":
                                {
                                    Program.SQL.SetnAdminLevelFornCharNo(nCharNo, Convert.ToByte(Option1));

                                    break;
                                }
                            case "PRODUCTION":
                                {
                                    Program.SQL.InserttSkill(nCharNo, Convert.ToUInt16(Option1));

                                    break;
                                }
                        }
                    }
                }
            }

            ClientSocket.SendPacketClient(ServerPacket);
        }
    }
}
