package Controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;

import Model.Model;


/**
 * 
 *
 */
public class Preprocessor {

	/**
	 * this method is called from the processor to preprocess the selected input data
	 * 
	 * @param model
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	public void preprocess(Model model) throws FileNotFoundException, IOException {
		Parser parser = new Parser();
		SituationFactory situationFactory = new SituationFactory();
		
		LinkedList<LinkedList<String>> parsedUtteranceWords = parser.parse(model.getParameters().getUtteranceFile());
		LinkedList<String> utteranceSentences = parser.removeClassification(model.getParameters().getUtteranceFile());
		HashMap<String, File> imageMap = preprocessImageDb(model.getParameters().getImageDB(), parser);
		situationFactory.createSituations(parsedUtteranceWords, utteranceSentences, model, imageMap);
	}

	/**
	 * this method preprocesses the image-db to a map of stemmed filenames and files
	 * @param directory
	 * @param parser
	 * @return the image-map
	 * @throws IOException 
	 */
	private HashMap<String, File> preprocessImageDb(File directory, Parser parser) throws IOException {
		HashMap<String, File> imageMap = new HashMap<String, File>();
		
		File[] imageFiles = directory.listFiles();
		for(int i=0; i < imageFiles.length; i++) {
			if(imageFiles[i] != null && imageFiles[i].exists()) {
				String parsedFileName = parser.stem(parser.splitStrToStrLst(new String(imageFiles[i].getName().toCharArray())).getFirst());
				imageMap.put(parsedFileName, imageFiles[i]);
			}
		}
		
		return imageMap;
	}
	
}
