package de.yannicpunktdee.yoshibot.command.commands;

import de.yannicpunktdee.yoshibot.command.YoshiCommand;
import de.yannicpunktdee.yoshibot.command.YoshiCommandContext;
import de.yannicpunktdee.yoshibot.main.YoshiBot;
import de.yannicpunktdee.yoshibot.utils.Logger;
import de.yannicpunktdee.yoshibot.utils.Resources;
import net.dv8tion.jda.api.EmbedBuilder;

import java.awt.*;
import java.io.File;

public class PlayCommand extends YoshiCommand {
	
	public PlayCommand(YoshiCommandContext context) {
		super(context);
	}


	@Override
	public boolean execute() {
		if (!super.execute()) return false;
		
		if (context.containsArguments(new String[]{"add", "name"})) {
			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 if (context.containsArguments(new String[]{"name"})) {
			File file = new File(Resources.getPathToAudioFile(context.getArgument("name")));
			if(!file.isFile()){
				sendMessage(String.format("Konnte keine Audiodatei namens '%s.opus' finden!",
						context.getArgument("name")));
				return false;
			}

			Logger.logDebug(getVoiceChannelByParam().getName());
			YoshiBot.getInstance().playSound(file, getVoiceChannelByParam());
		}else {
			sendMessage("Unzureichende Parameter.");
		}
		
		return true;
	}
	
}
