package com.avairebot.orion.commands.music;

import com.avairebot.orion.Orion;
import com.avairebot.orion.audio.AudioHandler;
import com.avairebot.orion.audio.GuildMusicManager;
import com.avairebot.orion.contracts.commands.AbstractCommand;
import com.avairebot.orion.factories.MessageFactory;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.exceptions.InsufficientPermissionException;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class MoveHereCommand extends AbstractCommand {

    public MoveHereCommand(Orion orion) {
        super(orion, false);
    }

    @Override
    public String getName() {
        return "Move Music Here Command";
    }

    @Override
    public String getDescription() {
        return "Moves the bot to your current voice channel.\n**Must be connected to a voice channel to use this command**";
    }

    @Override
    public List<String> getUsageInstructions() {
        return Collections.singletonList("`!movehere` - Moves the bot to your voice channel");
    }

    @Override
    public String getExampleUsage() {
        return "`!movehere`";
    }

    @Override
    public List<String> getTriggers() {
        return Arrays.asList("movehere", "moveh");
    }

    @Override
    public boolean onCommand(Message message, String[] args) {
        GuildMusicManager musicManager = AudioHandler.getGuildAudioPlayer(message.getGuild());

        if (musicManager.getPlayer().getPlayingTrack() == null) {
            return sendErrorMessage(message, "Not connected to voice, request music first with `!play`");
        }

        VoiceChannel channel = message.getMember().getVoiceState().getChannel();
        if (channel == null) {
            return sendErrorMessage(message, "You must be connected to a voice channel to use this command!");
        }

        List<Permission> permissions = message.getGuild().getMember(message.getJDA().getSelfUser()).getPermissions(
                message.getMember().getVoiceState().getChannel()
        );

        if (!permissions.contains(Permission.VOICE_CONNECT)) {
            MessageFactory.makeWarning(message, "Unable to connect to the voice channel you're in due to missing permissions!").queue();
            return false;
        }

        try {
            message.getGuild().getAudioManager().openAudioConnection(channel);
            MessageFactory.makeSuccess(message, "I am now streaming music in **%s**",
                    channel.getName()
            ).queue();
        } catch (InsufficientPermissionException ex) {
            MessageFactory.makeWarning(message, "Unable to connect to the voice channel you're in due to userlimit!").queue();
            orion.logger.exception(ex);
            return false;
        }

        return true;
    }
}

@@ -167,24 +167,77 @@
 sicManager;%0A
+import com.avairebot.orion.audio.VoiceConnectStatus;%0A
 import com.a
@@ -287,16 +287,16 @@
 ommand;%0A
-
 import c
@@ -344,48 +344,8 @@
 ry;%0A
-import net.dv8tion.jda.core.Permission;%0A
 impo
@@ -386,16 +386,16 @@
 essage;%0A
+
 import n
@@ -440,80 +440,8 @@
 nel;
-%0Aimport net.dv8tion.jda.core.exceptions.InsufficientPermissionException;
 %0A%0Aim
@@ -1895,527 +1895,279 @@
     
-List%3CPermission%3E permissions = message.getGuild().getMember(message.getJDA().getSelfUser()).getPermissions(%0A                message.getMember().getVoiceState().getChannel()%0A        );%0A%0A        if (!permissions.contains(Permission.VOICE_CONNECT)) %7B%0A            MessageFactory.makeWarning(message, %22Unable to connect to the voice channel you're in due to missing permissions!%22).queue();%0A            return false;%0A        %7D%0A%0A        try %7B%0A            message.getGuild().getAudioManager().openAudioConnection(channel);%0A    
+VoiceConnectStatus voiceConnectStatus = AudioHandler.connectToVoiceChannel(message, true);%0A%0A        if (!voiceConnectStatus.isSuccess()) %7B%0A            MessageFactory.makeWarning(message, voiceConnectStatus.getErrorMessage()).queue();%0A            return false;%0A        %7D%0A%0A
     
@@ -2260,20 +2260,16 @@
         
-    
 channel.
@@ -2278,20 +2278,16 @@
 tName()%0A
-    
         
@@ -2309,267 +2309,8 @@
     
-%7D catch (InsufficientPermissionException ex) %7B%0A            MessageFactory.makeWarning(message, %22Unable to connect to the voice channel you're in due to userlimit!%22).queue();%0A            orion.logger.exception(ex);%0A            return false;%0A        %7D%0A%0A        
 retu
@@ -2317,16 +2317,17 @@
 rn true;
+%0A
 %0A    %7D%0A%7D

Refactor music move here command to use audio handle connect method