package ml.duncte123.skybot.commands.music;

import ml.duncte123.skybot.objects.command.Command;
import ml.duncte123.skybot.utils.AirUtils;
import ml.duncte123.skybot.utils.Config;
import net.dv8tion.jda.core.EmbedBuilder;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;

import java.time.Instant;

public class LeaveCommand extends Command {

    public final static String help = "make the bot leave your channel.";

    /**
     * This is a check to see if the command is save to execute
     * @param args The command agruments
     * @param event a instance of {@link net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent GuildMessageReceivedEvent}
     * @return true if we are the command is safe to run
     */
    @Override
    public boolean called(String[] args, GuildMessageReceivedEvent event) {
        boolean botInChannel = false;

        if(event.getGuild().getAudioManager().isConnected()){
            botInChannel = true;

            if(!event.getGuild().getAudioManager().getConnectedChannel().getMembers().contains(event.getMember())){
                event.getChannel().sendMessage(AirUtils.embedField(AirUtils.au.embedTitle, "I'm sorry, but you have to be in the same channel as me to use any music related commands")).queue();
                return false;
            }

        }else{
            event.getChannel().sendMessage(AirUtils.embedField(AirUtils.au.embedTitle, "I'm not in a channel atm")).queue();
        }


        return botInChannel;
    }

    /**
     * This is the action of the command, the thing you want the command to to needs to be in here
     * @param args The command agruments
     * @param event a instance of {@link net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent GuildMessageReceivedEvent}
     */
    @Override
    public void action(String[] args, GuildMessageReceivedEvent event) {
        AirUtils.au.getMusicManager(event.getGuild()).player.stopTrack();
        event.getGuild().getAudioManager().setSendingHandler(null);
        event.getGuild().getAudioManager().closeAudioConnection();
        EmbedBuilder eb = new EmbedBuilder()
                .setColor(Config.defaultColour)
                .addField(AirUtils.au.embedTitle, "Leaving your channel", false)
                .setFooter(Config.defaultName, Config.defaultIcon)
                .setTimestamp(Instant.now());
        event.getChannel().sendMessage(eb.build()).queue();

    }

    /**
     * The usage instructions of the command
     * @return a String
     */
    @Override
    public String help() {
        // TODO Auto-generated method stub
        return help;
    }

    @Override
    public String getName() {
        return "leave";
    }

    @Override
    public String[] getAliases() {
        return new String[]{"disconnect"};
    }
}

@@ -137,91 +137,8 @@
 ls;%0A
-import ml.duncte123.skybot.utils.Config;%0Aimport net.dv8tion.jda.core.EmbedBuilder;%0A
 impo
@@ -214,35 +214,8 @@
 t;%0A%0A
-import java.time.Instant;%0A%0A
 publ
@@ -1004,40 +1004,23 @@
     
- event.getChannel().sendMessage(
+sendMsg(event, 
 AirU
@@ -1152,24 +1152,16 @@
 mands%22))
-.queue()
 ;%0A      
@@ -1230,39 +1230,23 @@
     
-event.getChannel().sendMessage(
+sendMsg(event, 
 AirU
@@ -1313,24 +1313,16 @@
 l atm%22))
-.queue()
 ;%0A      
@@ -1734,32 +1734,99 @@
 dEvent event) %7B%0A
+        if(event.getGuild().getAudioManager().isConnected()) %7B%0A    
         AirUtils
@@ -1883,32 +1883,36 @@
 rack();%0A        
+  
+  
 event.getGuild()
@@ -1955,16 +1955,20 @@
 (null);%0A
+    
         
@@ -2038,112 +2038,40 @@
     
-EmbedBuilder eb = new EmbedBuilder()%0A                .setColor(Config.defaultColour)%0A                .ad
+    sendMsg(event, AirUtils.embe
 dFie
@@ -2123,16 +2123,11 @@
 nel%22
-, false)
+));
 %0A   
@@ -2135,172 +2135,93 @@
     
-        .setFooter(Config.defaultName, Config.defaultIcon)%0A                .setTimestamp(Instant.now());%0A        event.getChannel().sendMessage(eb.build()).queue();
+%7D else %7B%0A            sendMsg(event, %22I'm not connected to any channels.%22);%0A        %7D%0A
 %0A%0A  

Implement better logic for LeaveCommand