package djmania.file.step.sm;

import java.io.FileInputStream;
import java.util.Scanner;
import java.util.ArrayList;

import javazoom.jl.player.Player;
import djmania.file.FileTools;
import djmania.file.step.StepClass;


/**
 * (Processer) Step processor for StepMania files.
 * <p>
 * Adds .sm file support.
 * <p>
 * Licensed under <A HREF="http://www.gnu.org/licenses/gpl.txt">GNU GPL (Version 3)</A>. 
 * <p>
 * @author	Philip Peng
 * @version	X.X 28-Oct-07
 * 
 */

public class SMProcesser {
	
	private FileInputStream stepFile;
	private StepClass stepObj;
	
	public int setStream(String file) {
		try {
			FileInputStream stream;
			stream = FileTools.convertStringToStream(file);
			stepFile = stream;
			return 1; // Success
		} catch ( java.io.FileNotFoundException e ) {
			return -1; // Fail: file not found
		} finally {
			stepObj = new StepClass(file);
		}
	}
	
	public void process() {
		Scanner input = new Scanner(stepFile);
		input.useDelimiter(";");
		String buffer;
		while (input.hasNext()) {
			//bufferLines.add(input.next());
			buffer = input.next().trim();
			// == Empty == //
			if (buffer.length()<=1) {
				System.err.println("Error: Unknown tag \"" + buffer + "\": unable to process.");
				System.out.println("Buffer: " + buffer);
			// == Strings == //
			} else if (buffer.indexOf("#TITLE:")!=-1) {
				stepObj.TITLE = FileTools.bufferTrim(buffer);
			} else if (buffer.indexOf("#SUBTITLE:")!=-1) {
				stepObj.SUBTITLE = FileTools.bufferTrim(buffer);
			} else if (buffer.indexOf("#ARTIST:")!=-1) {
				stepObj.ARTIST = FileTools.bufferTrim(buffer);
			} else if (buffer.indexOf("#TITLETRANSLIT:")!=-1) {
				stepObj.TITLETRANSLIT = FileTools.bufferTrim(buffer);
			} else if (buffer.indexOf("#SUBTITLETRANSLIT:")!=-1) {
				stepObj.SUBTITLETRANSLIT = FileTools.bufferTrim(buffer);
			} else if (buffer.indexOf("#ARTISTTRANSLIT:")!=-1) {
				stepObj.ARTISTTRANSLIT = FileTools.bufferTrim(buffer);
			} else if (buffer.indexOf("#CREDIT:")!=-1) {
				stepObj.CREDIT = FileTools.bufferTrim(buffer);
			} else if (buffer.indexOf("#BANNER:")!=-1) {
				stepObj.BANNER = FileTools.bufferTrim(buffer);
			} else if (buffer.indexOf("#BACKGROUND:")!=-1) {
				stepObj.BACKGROUND = FileTools.bufferTrim(buffer);
			} else if (buffer.indexOf("#LYRICSPATH:")!=-1) {
				stepObj.LYRICSPATH = FileTools.bufferTrim(buffer);
			} else if (buffer.indexOf("#CDTITLE:")!=-1) {
				stepObj.CDTITLE = FileTools.bufferTrim(buffer);
			} else if (buffer.indexOf("#MUSIC:")!=-1) {
				stepObj.MUSIC = FileTools.bufferTrim(buffer);
			// == Floats == //
			} else if (buffer.indexOf("#OFFSET:")!=-1) {
				stepObj.OFFSET = Float.valueOf(FileTools.bufferTrim(buffer));
			} else if (buffer.indexOf("#SAMPLESTART:")!=-1) {
				stepObj.SAMPLESTART = Float.valueOf(FileTools.bufferTrim(buffer));
			} else if (buffer.indexOf("#SAMPLELENGTH:")!=-1) {
				stepObj.SAMPLELENGTH = Float.valueOf(FileTools.bufferTrim(buffer));
			// == Int == //
			} else if (buffer.indexOf("#SELECTABLE:")!=-1) {
				if (FileTools.bufferTrim(buffer).equals("YES")) {
					stepObj.SELECTABLE = 1;
				} else if (FileTools.bufferTrim(buffer).equals("NO")) {
					stepObj.SELECTABLE = 2;
				} else if (FileTools.bufferTrim(buffer).equals("ROULETTE")) {
					stepObj.SELECTABLE = 3;
				}
			// == Floats[][] == // FIXME: Turn to Vectors
			} else if (buffer.indexOf("#BPMS:")!=-1) {
				//System.out.println("#BPMS: " + buffer);
			} else if (buffer.indexOf("#STOPS:")!=-1) {
				//System.out.println("#STOPS: " + buffer);
			// == String[][] == // FIXME: Turn to Vectors
			} else if (buffer.indexOf("#BGCHANGES:")!=-1) {
				//System.out.println("#BGCHANGES: " + buffer);
			// == String[] == // FIXME: Turn to Vectors
			} else if (buffer.indexOf("#NOTES:")!=-1) {
				//System.out.println("#NOTES: " + buffer);
			}
			/*
			} else if (buffer.length()>0) {
				System.err.println("Error: Unknown tag \"" + buffer + "\": unable to process.");
				System.out.println("Buffer: " + buffer);
			}
			*/
		}
		
		// Testing
		//	public String TITLE, SUBTITLE, ARTIST, TITLETRANSLIT, SUBTITLETRANSLIT, ARTISTTRANSLIT, CREDIT,
		//BANNER, BACKGROUND, LYRICSPATH, CDTITLE, MUSIC;	public float OFFSET, SAMPLESTART, SAMPLELENGTH;
		//public int SELECTABLE;
		// FIXME: Turn these into Vectors
		//public float[][] BPMS, STOPS;
		//public String[][] BGCHANGE;
		//public String[] NOTES_UNPROCESSED; // FIXME: Temporary til I finish the coding
		System.out.print(
				stepObj + "\n"
				+ stepObj.ARTIST + "\n"
				+ stepObj.TITLE + "\n"
				+ stepObj.SUBTITLE + "\n"
				+ stepObj.ARTIST + "\n"
				+ stepObj.TITLETRANSLIT+ "\n"
				+ stepObj.SUBTITLETRANSLIT + "\n"
				+ stepObj.ARTISTTRANSLIT + "\n"
				+ stepObj.CREDIT + "\n"
				+ stepObj.BANNER + "\n"
				+ stepObj.BACKGROUND + "\n"
				+ stepObj.LYRICSPATH + "\n"
				+ stepObj.CDTITLE + "\n"
				+ stepObj.MUSIC + "\n"
				+ stepObj.SELECTABLE// + "\n"
				//+ stepObj.BPMS + "\n"
				//+ stepObj.STOPS + "\n"
				//+ stepObj.BGCHANGE + "\n"
				//+ stepObj.NOTES_UNPROCESSED + "\n"
				);
		
	}
	
}
