using System;

using FilterAPI.Networking;

using DSharpPlus.EventArgs;

namespace FilterDiscord.Commands
{
    internal class DisconnectPlayer
    {
        public async void HandleCommand(String CommandPrefix, MessageCreateEventArgs Args, String[] MessageSplit)
        {
            if (MessageSplit.Length == 1) { await Args.Channel.SendMessageAsync("You appear to have used the command incorrectly.\nCorrect useage: &DisconnectPlayer/&DP [Required: Charname] [Optional: ShowMessage (Default: False)] [Optional: Message (Default: You have been disconnected by server.)]"); }
            else if (MessageSplit.Length == 2)
            {
                SendDisconnectPacket(MessageSplit[1], false, String.Empty);

                await Args.Channel.SendMessageAsync(String.Format("I have sent a disconnect packet to the 'Filter API' for the sID '{0}'.", MessageSplit[1]));
            }
            else if (MessageSplit.Length == 3)
            {
                Boolean IsBoolean;

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

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

                if (IsBoolean)
                {
                    SendDisconnectPacket(MessageSplit[1], Convert.ToBoolean(MessageSplit[2]), "You have been disconnected by server.");

                    if (Convert.ToBoolean(MessageSplit[2])) { await Args.Channel.SendMessageAsync(String.Format("I have sent a disconnect packet to the 'Filter API' for the sID '{0}', with the message 'You have disconnected by server.'.", MessageSplit[1])); }
                    else { await Args.Channel.SendMessageAsync(String.Format("I have sent a disconnect packet to the 'Filter API' for the sID '{0}'.", MessageSplit[1])); }
                }
                else { await Args.Channel.SendMessageAsync("The value you entered for 'SendMessage' is not a valid Boolean. Booleans are True/False."); }
            }
            else
            {
                Boolean IsBoolean;

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

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

                if (IsBoolean)
                {
                    String DisconnectMessage = Args.Message.Content.Replace(String.Format("{0}{1} {2} {3} ", CommandPrefix, MessageSplit[0], MessageSplit[1], MessageSplit[2]), String.Empty);

                    SendDisconnectPacket(MessageSplit[1], Convert.ToBoolean(MessageSplit[2]), DisconnectMessage);

                    if (Convert.ToBoolean(MessageSplit[2])) { await Args.Channel.SendMessageAsync(String.Format("I have sent a disconnect packet to the 'Filter API' for the sID '{0}', with the message '{1}'", MessageSplit[1], DisconnectMessage)); }
                    else { await Args.Channel.SendMessageAsync(String.Format("I have sent a disconnect packet to the 'Filter API' for the sID '{0}'.", MessageSplit[1])); }
                }
                else { await Args.Channel.SendMessageAsync("The value you entered for 'SendMessage' 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);
            }
        }
    }
}
