package com.robothand.highqualitybot.command;

import com.robothand.highqualitybot.Bot;
import com.robothand.highqualitybot.music.GuildMusicPlayer;
import net.dv8tion.jda.core.entities.Guild;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.VoiceChannel;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;

/**
 * Created by ethan on 6/19/17.
 */
public class PlayCommand extends Command {

    @Override
    public String[] getNames() {
        return new String[] {"play"};
    }

    @Override
    public String getDescription() {
        return "With no arguments, resumes playback if the player is paused. Providing a valid audio source (YouTube" +
                "link, SoundCloud, etc.) as an argument will add it to the queue.\n" +
                "Usage: " + Bot.PREFIX + "play [link]";
    }

    @Override
    public String getShortDesc() {
        return "Resume the player, or provide a link to add a song to the queue!";
    }

    @Override
    public void onCommand(MessageReceivedEvent event, String[] args) {
        GuildMusicPlayer musicPlayer;
        Guild guild = event.getGuild();
        MessageChannel channel = event.getChannel();

        // Get player for this guild
        musicPlayer = GuildMusicPlayer.getPlayer(guild);

        // don't do anything if there's nothing in the queue
        if (musicPlayer.getPlayingTrack() == null && args.length < 2) {
            channel.sendMessage("There's nothing in the queue to resume!").queue();
            return;
        }

        // Auto-join channel if player is not already in one
        if (!musicPlayer.isInChannel()) {
            VoiceChannel voice;
            if (event.getMember().getVoiceState().inVoiceChannel()) {
                // first try sender's channel
                voice = event.getMember().getVoiceState().getChannel();
            } else {
                // otherwise we should just join the first one
                voice = guild.getVoiceChannels().get(0);
            }

            musicPlayer.joinChannel(voice);
        }

        if (args.length > 1) {
            String search = "";
            for (int i = 1; i < args.length; i++) {
                search = search.concat(args[i]);
            }

            musicPlayer.playTrack(search, channel);
        } else {
            if (musicPlayer.isPaused()) {
                    musicPlayer.setPaused(false);
                    channel.sendMessage("Player resumed.").queue();
            } else {
                channel.sendMessage("The player is already running!").queue();
            }
        }
    }
}
@@ -513,17 +513,16 @@
 String%5B%5D
- 
 %7B%22play%22%7D
@@ -697,16 +697,17 @@
 (YouTube
+ 
 %22 +%0A    
@@ -787,10 +787,42 @@
 eue.
-%5Cn
+ You can also provide a link to a 
 %22 +%0A
@@ -838,16 +838,29 @@
        %22
+playlist. %5Cn 
 Usage: %22
@@ -1125,46 +1125,8 @@
 ) %7B%0A
-        GuildMusicPlayer musicPlayer;%0A
     
@@ -1252,32 +1252,49 @@
 s guild%0A        
+GuildMusicPlayer 
 musicPlayer = Gu
@@ -1338,17 +1338,17 @@
      // 
-d
+D
 on't do 
@@ -1587,564 +1587,115 @@
     
-// Auto-join channel if player is not already in one%0A        if (!musicPlayer.isInChannel()) %7B%0A            VoiceChannel voice;%0A            if (event.getMember().getVoiceState().inVoiceChannel()) %7B%0A                // first try sender's channel%0A                voice = event.getMember().getVoiceState().getChannel();%0A            %7D else %7B%0A                // otherwise we should just join the first one%0A                voice = guild.getVoiceChannels().get(0);%0A            %7D%0A%0A            musicPlayer.joinChannel(voice);%0A        %7D%0A%0A        if (args.length %3E 1) %7B
+joinVoiceChannel(guild, musicPlayer, event);%0A%0A        if (args.length %3E 1) %7B // Attempt to play a new track
 %0A   
@@ -1899,32 +1899,59 @@
         %7D else %7B
+ // Resume the paused track
 %0A            if 
@@ -1985,36 +1985,32 @@
                 
-    
 musicPlayer.setP
@@ -2023,20 +2023,16 @@
 false);%0A
-    
         
@@ -2217,9 +2217,569 @@
 %7D%0A    %7D%0A
+%0A    private void joinVoiceChannel(Guild guild, GuildMusicPlayer musicPlayer, MessageReceivedEvent event) %7B%0A        if (musicPlayer.isInChannel()) %7B%0A            return;%0A        %7D%0A%0A        VoiceChannel voice;%0A        if (event.getMember().getVoiceState().inVoiceChannel()) %7B%0A            // Join sender's channel%0A            voice = event.getMember().getVoiceState().getChannel();%0A        %7D else %7B%0A            // Otherwise just join the first channel%0A            voice = guild.getVoiceChannels().get(0);%0A        %7D%0A%0A        musicPlayer.joinChannel(voice);%0A    %7D%0A%0A
 %7D

Update play command description