package de.yannicpunktdee.yoshibot.command.commands;

import de.yannicpunktdee.yoshibot.audio.AudioLoadResultHandlerImpl;
import de.yannicpunktdee.yoshibot.command.YoshiCommand;
import de.yannicpunktdee.yoshibot.command.YoshiCommandContext;
import de.yannicpunktdee.yoshibot.main.YoshiBot;
import de.yannicpunktdee.yoshibot.utils.Resources;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.VoiceChannel;

import java.awt.*;
import java.io.File;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class PlayCommand extends YoshiCommand {
	
	protected final String[] requiredArguments = new String[]{"name"};
	
	
	public PlayCommand(YoshiCommandContext context) {
		super(context);
	}
	
	public static boolean play(String fileName, VoiceChannel vc) {
		if (!new File(fileName).isFile()) return false;
		
		YoshiBot yoshiBot = YoshiBot.getInstance();
		
		vc.getGuild().getAudioManager().openAudioConnection(vc);
		yoshiBot.audioPlayerManager.loadItem(fileName, new AudioLoadResultHandlerImpl());
		
		return true;
	}
	
	@Override
	public boolean execute() {
		if (!super.execute()) return false;
		
		YoshiBot yoshiBot = YoshiBot.getInstance();
		
		if (context.containsArguments(new String[]{"add"})) {
			downloadAttachmentToFile(Resources.getAudioPath(), context.getArgument("name"));
		} else if (context.containsArguments(new String[]{"list"})) {
			File audioDirectory = new File(Resources.getAudioPath());
			StringBuilder sb = new StringBuilder();
			for (File f : audioDirectory.listFiles()) {
				String fName = f.getName();
				if (!fName.endsWith(".opus")) continue;
				sb.append(fName.substring(0, fName.lastIndexOf(".opus")));
				sb.append("\n");
			}
			EmbedBuilder eb = new EmbedBuilder();
			eb.setTitle("Es sind folgende Audios verf\u00fcgbar:");
			eb.setColor(Color.blue);
			eb.setDescription(sb.toString());
			sendMessage(eb.build());
		} else {
			VoiceChannel vc;
			if (context.getEvent().getMember() == null ||
				!context.getEvent().getMember().getVoiceState().inVoiceChannel()) {
				if (!context.containsArguments(new String[]{"channel"})) {
					context.getEvent().getTextChannel().sendMessage("Es wurde kein channel spezfiziert.").queue();
					return false;
				}
				List<VoiceChannel> channels = yoshiBot.jda.getVoiceChannelsByName(context.getArgument("channel"), true);
				if (!(channels.size() > 0)) {
					context.getEvent().getTextChannel().sendMessage("Der Kanalname konnte nicht gefunden werden.")
							.queue();
					return false;
				}
				vc = channels.get(0);
			} else {
				vc = context.getEvent().getMember().getVoiceState().getChannel();
			}
			
			String fileName = Resources.getPathToAudioFile(context.getArgument("name"));
			
			if (!play(fileName, vc)) {
				sendMessage(String.format("Konnte keine Audiodatei namens '%s.opus' finden!",
										  context.getArgument("name")));
			}
		}
		
		return true;
	}
	
}
