package akane.command.commands.music;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import akane.command.core.CommandInterface;
import akane.listeners.PlListener;
import akane.player.GuildMusicManager;
import akane.player.MusicPlayer;
import akane.api.Youtube;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;


public class QCommand implements CommandInterface {
	@Override
	public boolean isSafe(String[] args, MessageReceivedEvent event) {
		MessageChannel channel = event.getChannel();

		if (!event.isFromType(ChannelType.TEXT)) {
			messageIsServerCommand(channel);
			return false;
		}

		if (args.length == 0) {
			messageNoArgs(channel);
			return false;
		}

		if (!event.getMember().getVoiceState().inVoiceChannel()) {
			messageOutsideChannel(channel);
			return false;
		}
		return true;
	}

	@Override
	public void main(String[] args, MessageReceivedEvent event) {
		GuildMusicManager mng = MusicPlayer.getMusicManager(event.getGuild(), event.getTextChannel());
		TextChannel userChannel = event.getTextChannel();

		if (!event.getGuild().getAudioManager().isConnected()) {
			joinVoiceChannel(event, event.getGuild(), mng);
		}

		if (mng.player.isPaused()) {
			mng.player.setPaused(false);
		}

		if (mng.scheduler.channel == null)
			mng.scheduler.channel = userChannel;

		if (mng.scheduler.radioQueue.size() > 0 && mng.scheduler.radio)
			mng.scheduler.radioQueue.clear();

		if (args.length == 0) {
			try {
				URL url = new URL(args[0]);
				if (url.getHost().equalsIgnoreCase("youtube.com") || url.getHost().equalsIgnoreCase("youtu.be") || url.getHost().equalsIgnoreCase("soundcloud.com")) {
					if ((url.getPath().contains("&list") || url.getPath().contains("playlist?list")) || (args[0].contains("soundcloud") && args[0].contains("/sets/"))) {
						MusicPlayer.loadAndPlay(mng, userChannel, args[0], true, false);
					} else {
						MusicPlayer.loadAndPlay(mng, userChannel, args[0], false, false);
					}
				} else {
					event.getChannel().sendMessage(":no_entry: Aceito apenas links do Youtube ou Soundcloud.").queue(s ->
									s.delete().queueAfter(5, TimeUnit.SECONDS));
					try {
						event.getMessage().delete().queueAfter(5, TimeUnit.SECONDS);
					} catch (Exception ignored) {
					}
				}
			} catch (MalformedURLException ignored) {
				youtubeQueue(args, event, mng);
			}
		} else {
			youtubeQueue(args, event, mng);
		}

		try {
			event.getMessage().delete().completeAfter(5, TimeUnit.SECONDS);
		} catch (Exception ignored) {
		}

	}

	private void joinVoiceChannel(MessageReceivedEvent event, Guild guild, GuildMusicManager mng) {
		guild.getAudioManager().setSendingHandler(mng.sendHandler);
		guild.getAudioManager().openAudioConnection(event.getMember().getVoiceState().getChannel());
	}

	private void messageOutsideChannel(MessageChannel channel) {
		channel.sendMessage(":no_entry: Voce não esta em um canal de voz"
						+ "(ou eu não possuo acesso ao seu canal de voz).").queue();
	}

	private void messageIOException(MessageChannel channel) {
		channel.sendMessage(":no_entry: Não foi possível achar sua música.").queue();
	}

	private void messageNoArgs(MessageChannel channel) {
		channel.sendMessage(":no_entry: Você precisa colocar o link ou nome.").queue();
	}

	private void messageIsServerCommand(MessageChannel channel) {
		channel.sendMessage(":no_entry: Só uso esse comando em servidores e não no privado.").queue();
	}

	private void youtubeQueue(String[] args, MessageReceivedEvent event, GuildMusicManager mng) {
		Youtube youtube = new Youtube(null, String.join(" ", args));
		try {
			URL url = youtube.getFirstResult().getUrl();

			if (url.getPath().contains("&list") || url.getPath().contains("playlist?list")) {
				event.getJDA().addEventListener(new PlListener(event.getGuild(), event.getTextChannel(), url.toString()));
				event.getChannel().sendMessage(":notes: Uma playlist foi detectada, deseja adicionar a playlist inteira?(s ou n)").queue();
			} else {
				MusicPlayer.loadAndPlay(mng, event.getTextChannel(), url.toString(), false, false);
			}

		} catch (IOException i) {
			messageIOException(event.getChannel());
		}
	}
}
@@ -1542,25 +1542,25 @@
 s.length == 
-0
+1
 ) %7B%0A%09%09%09try %7B
@@ -1628,24 +1628,28 @@
 IgnoreCase(%22
+www.
 youtube.com%22
@@ -1658,32 +1658,41 @@
 %7C%7C url.getHost()
+%0A%09%09%09%09%09%09%09%09
 .equalsIgnoreCas
@@ -1713,32 +1713,41 @@
 %7C%7C url.getHost()
+%0A%09%09%09%09%09%09%09%09
 .equalsIgnoreCas
@@ -1749,16 +1749,20 @@
 reCase(%22
+www.
 soundclo
@@ -1773,35 +1773,148 @@
 om%22)
-) %7B%0A%09%09%09%09%09if ((url.getPath()
+ %7C%7C url.getHost()%0A%09%09%09%09%09%09%09%09.equalsIgnoreCase(%22youtube.com%22) %7C%7C url.getHost()%0A%09%09%09%09%09%09%09%09.equalsIgnoreCase(%22soundcloud.com%22)) %7B%0A%09%09%09%09%09if ((args%5B0%5D
 .con
@@ -1927,37 +1927,31 @@
 %22&list%22) %7C%7C 
-url.getPath()
+args%5B0%5D
 .contains(%22p
@@ -2377,128 +2377,8 @@
 ));%0A
-%09%09%09%09%09try %7B%0A%09%09%09%09%09%09event.getMessage().delete().queueAfter(5, TimeUnit.SECONDS);%0A%09%09%09%09%09%7D catch (Exception ignored) %7B%0A%09%09%09%09%09%7D%0A
 %09%09%09%09
@@ -2409,31 +2409,25 @@
 RLException 
-ignored
+y
 ) %7B%0A%09%09%09%09yout

fix playlist detection