package fr.Skydust.JdrBot.audio;

import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;

import net.dv8tion.jda.api.managers.AudioManager;

public class TrackScheduler extends AudioEventAdapter {
	private final AudioPlayer player;
	private boolean loop;
	private AudioManager am;
	
	public TrackScheduler(AudioPlayer player, AudioManager am) {
		this.player = player;
		loop = true;
		this.am = am;
	}
	
	public void play(AudioTrack track, boolean loop) {
		player.startTrack(track, false);
		this.loop = loop;
	}
	
	public void stop() {
		player.stopTrack();
	}
	
	@Override
	public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
		if (endReason.mayStartNext) {
			if(loop) {
				player.startTrack(track.makeClone(), false);
				System.out.println("Launching track:"+track.getInfo().title);
			} else {
				stop();
			}
		}
	}
}