using System;
using System.Threading;
using FilterAPI.Networking;

using DSharpPlus.EventArgs;

namespace FilterDiscord.Commands
{
    internal class DeleteSkill
    {
        public async void HandleCommand(String CommandPrefix, MessageCreateEventArgs Args, String[] MessageSplit)
        {
            if (MessageSplit.Length < 3 || MessageSplit.Length > 4) { await Args.Channel.SendMessageAsync("You appear to have used the command incorrectly.\nCorrect useage: &DeleteSkill/&DS [Required: Charname] [Required: SkillID] [Optional: DisconnectPlayer]"); }
            else if (MessageSplit.Length == 3)
            {
                if (!Program.SQL.sIDTaken(MessageSplit[1])) { await Args.Channel.SendMessageAsync(String.Format("'{0}' does not appear to be in the database.", MessageSplit[1])); }
                else
                {
                    Int32 nCharNo = Program.SQL.GetnCharNoFromsID(MessageSplit[1]);

                    if (!Program.SQL.HasSkill(nCharNo, Convert.ToInt32(MessageSplit[2]))) { await Args.Channel.SendMessageAsync(String.Format("'{0}' does not appear to have a skill with the ID '{1}'.", MessageSplit[1], MessageSplit[2])); }
                    else
                    {
                        Program.SQL.DeleteSkill(nCharNo, Convert.ToInt32(MessageSplit[2]));

                        await Args.Channel.SendMessageAsync(String.Format("'{0}' no longer has the skill with ID '{1}'. Please tell them to relog.", MessageSplit[1], MessageSplit[2]));
                    }
                }
            }
            else if (MessageSplit.Length == 4)
            {
                Boolean IsBoolean;

                try
                {
                    Convert.ToBoolean(MessageSplit[3]);

                    IsBoolean = true;
                }
                catch { IsBoolean = false; }

                if (IsBoolean)
                {
                    if (!Program.SQL.sIDTaken(MessageSplit[1])) { await Args.Channel.SendMessageAsync(String.Format("'{0}' does not appear to be in the database.", MessageSplit[1])); }
                    else
                    {
                        Int32 nCharNo = Program.SQL.GetnCharNoFromsID(MessageSplit[1]);

                        if (!Program.SQL.HasSkill(nCharNo, Convert.ToInt32(MessageSplit[2]))) { await Args.Channel.SendMessageAsync(String.Format("'{0}' does not appear to have a skill with the ID '{1}'.", MessageSplit[1], MessageSplit[2])); }
                        else
                        {
                            if (Convert.ToBoolean(MessageSplit[3]))
                            {
                                SendDisconnectPacket(MessageSplit[1], true, "You have been disconnected by the server to remove a skill.");

                                await Args.Channel.SendMessageAsync(String.Format("'{0}' has been disconnected from the server with the message 'You have been disconnected by the server to remove a skill.'", MessageSplit[1]));
                                await Args.Channel.SendMessageAsync(String.Format("Waiting 2 seconds before removing skill from '{0}'...", MessageSplit[1]));

                                Thread.Sleep(2000);

                                Program.SQL.DeleteSkill(nCharNo, Convert.ToInt32(MessageSplit[2]));

                                await Args.Channel.SendMessageAsync(String.Format("'{0}' no longer has the skill with ID '{1}'. They can now login to the server.", MessageSplit[1], MessageSplit[2]));
                            }
                            else
                            {
                                Program.SQL.DeleteSkill(nCharNo, Convert.ToInt32(MessageSplit[2]));

                                await Args.Channel.SendMessageAsync(String.Format("'{0}' no longer has the skill with ID '{1}'. Please tell them to relog.", MessageSplit[1], MessageSplit[2]));
                            }
                        }
                    }
                }
                else { await Args.Channel.SendMessageAsync("The value you entered for 'DisconnectPlayer' is not a valid Boolean. Booleans are True/False."); }
            }
        }

        private void SendDisconnectPacket(String sID, Boolean SendMessage, String Message)
        {
            using (var LocalPacket = new Packet(4, 7))
            {
                LocalPacket.PacketWriter.Write(sID);
                LocalPacket.PacketWriter.Write(SendMessage);
                LocalPacket.PacketWriter.Write("Server");
                LocalPacket.PacketWriter.Write(Message);

                Program.LocalClient.SendPacket(LocalPacket);
            }
        }
    }
}
