package fr.Skydust.JdrBot.cmds.playmusic;

import fr.Skydust.JdrBot.bases.command.Command;
import fr.Skydust.JdrBot.bases.command.CommandSpec;
import fr.Skydust.JdrBot.bases.command.HideCommand;
import fr.Skydust.JdrBot.enums.MessageType;
import fr.Skydust.JdrBot.utils.Utils;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

@HideCommand
@CommandSpec(
        name = "floadloop",
        description = "/!\\ Commande test /!\\ Permet de charger une musique de force",
        usage = "[URL/Recherche]"
)
public class FLoadLoop extends Command {
    @Override
    public void call(MessageReceivedEvent e) {
        if(e.getMember().getVoiceState().getChannel() != null) {
            String args = StringUtils.join(Arrays.copyOfRange(e.getMessage().getContentRaw().split(" "), 1, e.getMessage().getContentRaw().split(" ").length)," ");

            e.getGuild().getAudioManager().openAudioConnection(e.getMember().getVoiceState().getChannel());

            if(!args.matches(".*(https?)://.*")) {
                args = "ytsearch:"+args;
            }
            PlayMusic.loadAndPlay(e.getGuild(), (TextChannel) e.getChannel(), StringUtils.join(args," "), true, false);
        } else {
            Utils.sendMessage(e.getChannel(), MessageType.ERROR, "Vous n'êtes dans un salon vocal");
        }
    }
}