package Controller;

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

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import Model.Meaning;
import Model.Pair;
import Model.Parameters;
import Model.Situation;
import Model.Word;


/**
 * This class provides an basic implementation of the LogReader functionality, which is to be upgraded in the coming increments
 *
 */
public class LogReader {
	
	/**
	 * Attributes
	 */
	private SAXBuilder builder_;
	private Document document_;
	private File logFile_;
	
	/**
	 * Explicit constructor
	 * 	
	 * @param f
	 */
	public LogReader(File f){
		logFile_ = f;
	}
	
	//Mehtoden
	
	/**
	 * creates jdom document from logfile
	 */
	public Document buildDocument() throws JDOMException, IOException{
		document_ = builder_.build(logFile_);
		return document_;
	}
	
	/**
	 * This method implements the basic conversion provided by this class. It builds the objects needed for the learning algorithm from the in buildDocument() derived jdom tree.
	 * 
	 * TODO in coming increments add image to meaning
	 * 
	 * only works when pathnames in log file are correct
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	public Pair<Parameters, LinkedList<Situation>> buildSituations() throws IOException{
		//create new instance of utterance file in logfile
		File utteranceFile = new File(document_.getRootElement().getChild("PARAMETERS").getChild("UTTERANCE_FILE").getAttributeValue("utteranceFile_"));
		
		//create new instance of image file in logfile
		File imageFile = new File(document_.getRootElement().getChild("PARAMETERS").getChild("IMAGE_DATA_BASE").getAttributeValue("imageDB_"));
		
		//create new instance of logfile
		File logFile = new File(document_.getRootElement().getChild("PARAMETERS").getChild("LOG_FILE").getAttributeValue("logFile_")+"(REBUILT)");
		
		//get other parameter
		double addNoise  = Double.parseDouble(document_.getRootElement().getChild("PARAMETERS").getChild("ADDITIVE_NOISE").getAttributeValue("additiveNoise_"));
		double subNoise  = Double.parseDouble(document_.getRootElement().getChild("PARAMETERS").getChild("SUBTRACTIVE_NOISE").getAttributeValue("subtractiveNoise_"));
		double threshold = Double.parseDouble(document_.getRootElement().getChild("PARAMETERS").getChild("THRESHOLD").getAttributeValue("threshold_"));
	
		//create corresponding Parametes object
		Parameters param = new Parameters(utteranceFile, imageFile, logFile, addNoise, subNoise, threshold);
				
		//List in which read Situations will be stored
		LinkedList<Situation> situations = new LinkedList<Situation>();
		
		Element sits = document_.getRootElement().getChild("SITUATIONS");
		
		//iterate over all situations
		for (Element e : (LinkedList<Element>)sits.getChildren()){
		
			//in this block create utterances from log file
			Element ut = e.getChild("UTTERANCE");
			LinkedList<Word> u = new LinkedList<Word>();
			
			//iterate over all words in utterance
			for (Element word : (LinkedList<Element>)ut.getChildren()){
				Word ww = new Word(word.getAttributeValue("STRING_REPRESENTATION"));
				u.add(ww);
			}
			
			//in this block create scenes from logfile
			Element sc = e.getChild("SCENE");
			LinkedList<Meaning> mes = new LinkedList<Meaning>();
			for (Element mean : (LinkedList<Element>)sc.getChildren()){
				String strgrp = mean.getChild("STRING_REPRESENTATION_MEANING").getAttributeValue("StringRepresentation");
			
				//TODO stays empty in this increment
				Meaning m = new Meaning(strgrp);
				mes.add(m);				
			}
			
			//build Situation with utterance and meanings
			Situation sit = new Situation(u, mes, null);
			situations.add(sit);
		}
		
		Pair<Parameters, LinkedList<Situation>> ret = new Pair<Parameters, LinkedList<Situation>>(param, situations);
		return ret;

	}
}
