package stream.flarebot.flarebot.commands.music;

import com.arsenarsen.lavaplayerbridge.PlayerManager;
import com.arsenarsen.lavaplayerbridge.player.Track;
import net.dv8tion.jda.core.entities.Member;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.entities.User;
import stream.flarebot.flarebot.FlareBot;
import stream.flarebot.flarebot.commands.Command;
import stream.flarebot.flarebot.commands.CommandType;
import stream.flarebot.flarebot.music.extractors.YouTubeExtractor;
import stream.flarebot.flarebot.util.MessageUtils;

public class SongCommand implements Command {

    private PlayerManager manager;

    public SongCommand(FlareBot bot) {
        this.manager = bot.getMusicManager();
    }

    @Override
    public void onCommand(User sender, TextChannel channel, Message message, String[] args, Member member) {
        if (manager.getPlayer(channel.getGuild().getId()).getPlayingTrack() != null) {
            Track track = manager.getPlayer(channel.getGuild().getId()).getPlayingTrack();
            channel.sendMessage(MessageUtils.getEmbed(sender)
                    .addField("Current song: ", getLink(track), false)
                    .addField("Amount Played: ",
                            (int) (100f / track.getTrack().getDuration() * track.getTrack()
                                    .getPosition()) + "% of "
                                    + formatDuration(track), true)
                    .addField("Requested by:", String
                            .format("<@!%s>", track.getMeta().get("requester")), false).build())
                    .queue();
        } else {
            channel.sendMessage(MessageUtils.getEmbed(sender)
                    .addField("Current song: ", "**No song playing right now!**", false)
                    .build()).queue();
        }
    }

    public static String getLink(Track track) {
        String name = String.valueOf(track.getTrack().getInfo().title);
        String link = YouTubeExtractor.WATCH_URL + track.getTrack().getIdentifier();
        return String.format("[`%s`](%s)", name, link);
    }

    @Override
    public String getCommand() {
        return "song";
    }

    @Override
    public String getDescription() {
        return "Get the current song playing.";
    }

    @Override
    public String getUsage() {
        return "{%}song";
    }

    @Override
    public CommandType getType() {
        return CommandType.MUSIC;
    }

    public static String formatDuration(Track track) {
        long totalSeconds = track.getTrack().getDuration() / 1000;
        long seconds = totalSeconds % 60;
        long minutes = (totalSeconds / 60) % 60;
        long hours = (totalSeconds / 3600);
        return (hours > 0 ? (hours < 10 ? "0" + hours : hours) + ":" : "")
                + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
    }
}

@@ -2949,10 +2949,575 @@
 ;%0A    %7D%0A
+%0A    public static String getProgressBar(Track track) %7B%0A        float percentage = (100f / track.getTrack().getDuration() * track.getTrack().getPosition());%0A        StringBuilder progress = new StringBuilder(%22%5B%22);%0A        progress.append(StringUtils.repeat(%22%E2%96%AC%22, (int) Math.round((double) percentage / 10)));%0A        progress.append(%22%5D()%22);%0A        progress.append(StringUtils.repeat(%22%E2%96%AC%22, 10 - (int) Math.round((double) percentage / 10)));%0A        progress.append(%22 %22 + new DecimalFormat(%22#.##%22).format(percentage) + %22%25%22);%0A        return progress.toString();%0A    %7D%0A%0A
 %7D%0A

Add method for generating progress bars