package djmania.file;

import djmania.file.audio.*;
import djmania.file.FileTools;

/**
 * (Reader) Audio file reader.
 * <p>
 * Provides a controller for reading audio files 
 * by creating the correct {@link AudioPlayerThread} class.
 * <p>
 * Licensed under <A HREF="http://www.gnu.org/licenses/gpl.txt">GNU GPL (Version 3)</A>. 
 * <p>
 * @author	Philip Peng
 * @version	1.0 27-Oct-07
 * 
 */

public class AudioReader {

	private String audioFile;
	private String audioType;
	private AudioPlayerThread playerThread;
	
	/**
	 * Sets the input audio file based on file extension.
	 * @param	file	Path to audio file.
	 * @return	-1 if file not found, 0 if failed, 1 if successful.
	 */
	public int setAudioFile(String file) {
		
		// Cheap hack assuming that the file extension is correctly assigned. 
		audioFile = file;
		audioType = FileTools.getFileStringExtension(audioFile);
		if (audioType.equals("MP3")) {
			playerThread = new MP3PlayerThread();
		} else if (audioType.equals("FOOBAR")) {
			// More formats to come...
		} else {
			// Unhandled or "UNKNOWN" file extension
			playerThread = new MP3PlayerThread();
		}
		return playerThread.setStream(audioFile);
		
	} // End public int setAudioFile(String file)
	
	/**
	 * Returns the path to the set audio file.
	 * @return	Path to audio file.
	 */
	public String getAudioFile() {
		return audioFile;
	}
	
	/**
	 * Returns the file extension of the set audio file.
	 * @return	File extension of audio file.
	 */
	public String getAudioType() {
		return audioType.toUpperCase();
	}
	
	/**
	 * Returns the current position of playback.
	 * @return	Current position of playback in milliseconds.
	 */
	public int getPosition() {
		return playerThread.getPosition();
	}
	
	/**
	 * Returns the playback status.
	 * @return	True if playback is done, false otherwise.
	 */
	public boolean isDone() {
		return !playerThread.isAlive();
	}
	
	/**
	 * Starts the playback thread.
	 */
	public void play() {
		playerThread.start();
	}
	
	/**
	 * Pauses playback.
	 */	
	public void pause() {
		playerThread.pausePlayback();
	}
	
	/**
	 * Resumes playback.
	 */	
	public void resume() {
		playerThread.resumePlayback();
	}
	
	/**
	 * Force stops the playback and closes the player.
	 */	
	public void forceStop() {
		playerThread.close();
	}
	
}
