import java.io.FileWriter;
import java.io.IOException;

/**
 * 
 */

/**
 * The purpose of ARFFWriter is to generate ARFF files from 
 * image templates (expressed as one dimensional arrays)
 */
public class ARFFWriter {
	private FileWriter fileWriter; 
	
	public ARFFWriter(String filename, int numPixels, boolean useGreylevelClass) throws IOException
	{
		fileWriter = new FileWriter(filename);
		fileWriter.write("@relation faces\r\n");
		String pixelAttributeRange;
		if (useGreylevelClass) { 
			//generate the String for the range (0-255 greyscale) for all pixel attributes
			pixelAttributeRange = " {";
			for (int i=0; i<255; i++) {
				pixelAttributeRange += i + ","; 
			}
			pixelAttributeRange += "255}"; 
		}
		else {
			pixelAttributeRange = " INTEGER"; 
		}
		//write one attribute for each pixel (one for each of numPixels)
		for (int i=0; i<numPixels; i++) {
			fileWriter.write("@attribute pixel" + i + pixelAttributeRange + "\r\n"); 
		}
		fileWriter.write("@attribute person {0,1,2,3}\r\n"); 
		
		//write data header
		fileWriter.write("@DATA\r\n"); 
		fileWriter.flush(); 
	}
	
	public void writeFace(int pixels[], int personNum) {
		try {
			for (int i=0; i<pixels.length; i++) {
				fileWriter.write(pixels[i] + ",");
			}
			fileWriter.write(personNum + "\r\n"); 
		//	fileWriter.flush(); 
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	public void closeFile() { 
		try {
			fileWriter.flush();
			fileWriter.close(); 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
