package zero1hd.rhythmbullet.audio;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;

public class SongList {
	private Array<FileHandle> songList;
	private String searchPath;
	private boolean searched;
	public SongList() {
		songList = new Array<>();
	}
	
	public void refresh() {
		searched = true;
		songList.addAll(Gdx.files.absolute(searchPath).list((dir, name) -> {
			if (name.endsWith("mp3") || name.endsWith("wav")) {
				return true;
			}
			return false;
		}));
	}
	
	public void setSearchPath(String searchPath) {
		this.searchPath = searchPath;
	}
	
	public MusicDataPack getAudioData(FileHandle file) {
		if (file.extension().equalsIgnoreCase("wav")) {
			return new WavAudioData(file);
		} else if (file.extension().equalsIgnoreCase("mp3")) {
			return new Mp3AudioData(file);
		}
		return null;
	}
	
	public MusicDataPack getMusicInfoFromIndex(int index) {
		if (index > songList.size) {
			return null;
		}
		return getAudioData(songList.get(index));
	}
	
	public int getAmountOfSongs() {
		return songList.size;
	}
	
	public FileHandle getSongFileHandleFromIndex(int index) {
		if (!searched) Gdx.app.debug("SongList", "Warning, this list hasn't even searched yet...");
		if (index > songList.size) {
			return null;
		}
		return songList.get(index);
	}
	
	public Array<FileHandle> getSongList() {
		return songList;
	}
	
	public boolean isSearched() {
		return searched;
	}
}
