/**
 * 
 */
package de.tudarmstadt.ukp.experiments.pythagoras.featureExtractors.overviewsAnalysis;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.resource.ResourceSpecifier;

import de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData;
import de.tudarmstadt.ukp.dkpro.tc.api.features.FeatureExtractorResource_ImplBase;

/**
 * Base class for all the Overviews based feature extractors in the {@link pythagoras.featureExtractors.overviewsAnalysis} package  
 * @author Tahir Sousa
 * @version Jun 24, 2014 [Sousa]
 */
public class OverviewBaseFeatureExtractor 
extends FeatureExtractorResource_ImplBase {

	public static final String OVERVIEW_RESOURCE_DIR = "src/main/resources/LessonOverviews/";
	
	//Social Form Codes																						//Group name (startIndex, endIndex)
	public static final String[] SF_Codes = {"EA","PA","GA",												//sAP-Schülerarbeitsphasen (0,2)
			"OEU","MIX"};																					//kU-Klassenunterricht (3,4)
	public static final String[] SF_Groups = {"sAP", "kU"};

	//Content Codes
	public static final String[] Cont_Codes = {"IP","IPf","CPSU1","CPSU2","CPSU3","CPSW","CPSWf","CPCW",	//aBB-Aufgabenbearbeitung(0,7)
			"HI","S","Sf","SH","SK","B1", "B2", "B2f","B1,2f","B3", "B4", "B","Bf","BH", 					//theo-Theorie: Satzgruppe des Pythagoras(8,21)
			"ZIP", "ZIPf","HIP","HIPf","HIPKf","BIP","BIPf","BIPKf","FIP","FIPH","FIPf",					//theoPr-Theorieprobleme:Satzgruppe des Pythagoras(22,32)
			"LS",																							//lS-Lösungsstrategien(33)
			"HK","O","A2"};																					//rK-Restkategorien(34,36)
	public static final String[] Cont_Groups = {"aBB","theo","theoPr","lS","rK"};
	
	//Function Codes
	public static final String[] Func_Codes = {"HKK","HKPL","HKVL",											//hA-Hausaufgaben (0,2)
			"ET","ETf","ES",																				//eOP-Erarbeiten in	öffentlichen Phasen (3,5)
			"An","V","Ex","Exf","Ü","ÜÜ","ÜÜf","A","Ü+A","Ü+Af","Ü/A",										//aSAP-Problem- bzw.	Aufgabenbearbeitung	in Schülerarbeitsphasen(6,16)
			"öÜ","EP","EPf",																				//prABB-Problem- bzw.	Aufgabenbearbeitung	in öffentlichen Phasen(17,19)
			"LAL","LAS","LAE",																				//lAB-Lösungsansätze aufzeigen/besprechen(20,22)
			"K","PL","PLf","PLS","VL","VLS",																//prABS-Problem- bzw.	Aufgabenbesprechung(23,28)
			"SiL","SiLf","SiS",																				//sich-Sichern(29,31)
			"Sit","VW","R","P",																				//wFL-Weitere Funktionen im Lernprozess(32,35)
			"O"};																							//rK-Restliche Kategorien(36)
	public static final String[] Func_Groups = {"hA","eOP","aSAP","prABB","lAB","prABS","sich","wFL","rK"};
	
	public static List<String> overviewDataLines;

	/**
	 * Builds a list of strings from the Overview data resource of a lesson
	 * @param jcas the JCas
	 * @return list of strings containing the correpsonding lesson's Overview data
	 * @throws IOException
	 */
	protected List<String> loadOverviewData(JCas jcas) throws IOException {

		DocumentMetaData dmd = DocumentMetaData.get(jcas);
		String lessonPath = dmd.getDocumentUri();
		String lessonFileName = "Ubersicht_" + lessonPath.substring(lessonPath.lastIndexOf("/")+ 1, lessonPath.lastIndexOf("/") + 1 + 8) + ".txt";
		String overviewFilePath = OVERVIEW_RESOURCE_DIR + lessonFileName;
		
		List<String> overviewData = FileUtils.readLines(new File(overviewFilePath));
		
		return overviewData;
	}
	
	/**
	 * Extracts the 5 constituent parts of a typical Overview data line
	 * @param overviewDataLine string containing lesson Overview annotations
	 * @return overviewParts array of strings representing the 5 constituent parts of an Overview data line
	 */
	protected static String[] extractOverviewParts(String overviewDataLine)	{
		
		String[] overviewParts = overviewDataLine.split(";",5);
		return overviewParts;
		
	}
	
	/**
	 * Converts time from a time stamp to seconds
	 * @param timeStamp time stamp found in Pythagoras annotations
	 * @return time in seconds
	 */
	protected static int getSeconds(String timeStamp)	{
		
		int timeInSeconds = 0;
		
		int hr = 0;
		int min = 0;
		int sec = 0;
		
		if (timeStamp.equals(""))	{
			return -1;
		}
		else	{
			String[] splitTimeStamp = timeStamp.split(":",3);
			hr = Integer.parseInt(splitTimeStamp[0]);
			min = Integer.parseInt(splitTimeStamp[1]);
			sec = Integer.parseInt(splitTimeStamp[2]);
			
			timeInSeconds = sec + 60*(min + 60*hr);
			return timeInSeconds;
		}
		
	}

	/**
	 * Get Social Form Group given the Social Form code
	 * @param sfCode Social Form code
	 * @return Social Form Group
	 */
	protected static String getSfGroup(String sfCode) {

		String sfGroup = "";

		if (sfCode.equals(""))	{
			return sfGroup;
		}
		
		int codeIndex = 0;
		for (int i=0; i< SF_Codes.length; i++)	{
			if (SF_Codes[i].equals(sfCode))	{
				codeIndex = i;
				break;
			}
		}
		
		if (codeIndex>= 0 && codeIndex<= 2)	{
			sfGroup = SF_Groups[0];
		}
		else if (codeIndex>= 3 && codeIndex<= 4)	{
			sfGroup = SF_Groups[1];
		} 
		else
			System.out.println("Error! This is not possible!");
		
		return sfGroup;
	}
	
	/**
	 * Get Content Group given the Content code
	 * @param contCode Content code
	 * @return Content Group
	 */
	protected static String getContGroup(String contCode) {

		String contGroup = "";

		if (contCode.equals(""))	{
			return contGroup;
		}
		
		int codeIndex = 0;
		for (int i=0; i< Cont_Codes.length; i++)	{
			if (Cont_Codes[i].equals(contCode))	{
				codeIndex = i;
				break;
			}
		}
		
		if (codeIndex>= 0 && codeIndex<= 7)	{
			contGroup = Cont_Groups[0];
		}
		else if (codeIndex>= 8 && codeIndex<= 21)	{
			contGroup = Cont_Groups[1];
		}
		else if (codeIndex>= 22 && codeIndex<= 32)	{
			contGroup = Cont_Groups[2];
		}
		else if (codeIndex== 33)	{
			contGroup = Cont_Groups[3];
		}
		else if (codeIndex>= 34 && codeIndex<= 36)	{
			contGroup = Cont_Groups[4];
		}
		else
			System.out.println("Error! This is not possible!");
		
		return contGroup;
	}
	
	/**
	 * Get Function Group given the Function code
	 * @param funcCode Function code
	 * @return Function Group
	 */
	protected static String getFuncGroup(String funcCode) {

		String funcGroup = "";
		
		if (funcCode.equals(""))	{
			return funcGroup;
		}
		
		int codeIndex = 0;
		for (int i=0; i< Func_Codes.length; i++)	{
			if (Func_Codes[i].equals(funcCode))	{
				codeIndex = i;
				break;
			}
		}
		
		if (codeIndex>= 0 && codeIndex<= 2)	{
			funcGroup = Func_Groups[0];
		}
		else if (codeIndex>= 3 && codeIndex<= 5)	{
			funcGroup = Func_Groups[1];
		}
		else if (codeIndex>= 6 && codeIndex<= 16)	{
			funcGroup = Func_Groups[2];
		}
		else if (codeIndex>= 17 && codeIndex<= 19)	{
			funcGroup = Func_Groups[3];
		}
		else if (codeIndex>= 20 && codeIndex<= 22)	{
			funcGroup = Func_Groups[4];
		}
		else if (codeIndex>= 23 && codeIndex<= 28)	{
			funcGroup = Func_Groups[5];
		}
		else if (codeIndex>= 29 && codeIndex<= 31)	{
			funcGroup = Func_Groups[6];
		}
		else if (codeIndex>= 32 && codeIndex<= 35)	{
			funcGroup = Func_Groups[7];
		}
		else if (codeIndex== 36)	{
			funcGroup = Func_Groups[8];
		}
		else
			System.out.println("Error! This is not possible!");
		
		return funcGroup;
	}
}
