package iitb.cfilt.cpost.ma;

//import java.io.PrintStream;
//import java.io.UnsupportedEncodingException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

import iitb.cfilt.cpost.logger.Logger;
import iitb.cfilt.cpost.newstemmer.*;
/**
 * Starting with the stubs. Will fill up the processes later.
 * @author manshri
 *
 */

public class MorphologicalAnalyzer {


	public Vector<MorphologicallyAnalyzedToken> analyze(Vector<StemmedToken> tokens){
		MorphologicalAnalyzerRuleReader MAR = new MorphologicalAnalyzerRuleReader();
		Vector<MorphologicallyAnalyzedToken> retVal = new Vector<MorphologicallyAnalyzedToken>();
		Iterator iter = tokens.iterator();
		while(iter.hasNext()){
			StemmedToken token = (StemmedToken)iter.next();
			MorphologicallyAnalyzedToken mat = analyze(token,MAR);
			if(mat != null){
				retVal.add(mat);
			}
		}
		if(retVal.size()==0){
			retVal = null;
		}
		return retVal;
	}

	public MorphologicallyAnalyzedToken analyze(StemmedToken token, MorphologicalAnalyzerRuleReader MAR){

//		PrintStream  pout=null;
//		try {
//		pout = new PrintStream(System.out, true, "UTF8");
//		} catch (UnsupportedEncodingException e) {
//		// TODO Auto-generated catch block
//		e.printStackTrace();
//		}
		
		MorphologicallyAnalyzedToken retVal = new MorphologicallyAnalyzedToken(token);
		//MorphologicalAnalyzerRuleReader MAR = new MorphologicalAnalyzerRuleReader();
		Vector<MorphologicalAnalyzerRuleResult> marrv = new Vector<MorphologicalAnalyzerRuleResult>();

		Vector<String> flags=new Vector<String>();

		Vector<StemmerRuleResult> stemmerRuleResults = token.getStemmedOutputs();
		Iterator stemmerRuleResultsIter = stemmerRuleResults.iterator();
		while(stemmerRuleResultsIter.hasNext()){
			StemmerRuleResult currentStemmerRuleResult = (StemmerRuleResult)stemmerRuleResultsIter.next();

			LinkedList<String> suffixList = currentStemmerRuleResult.getSuffixList(); 
			String root = currentStemmerRuleResult.getRoot();
			String paradigm = currentStemmerRuleResult.getParadigm();
			String category = currentStemmerRuleResult.getCategory();
			Vector <String> analysis = new Vector<String>();
			Iterator<String> listItr = suffixList.iterator();
			
			
			if(suffixList.size() == 1)
			{
				//System.out.println("*");
				analysis = MAR.getSuffixAnalysis("-", paradigm);
			}
			
			

			while (listItr.hasNext()){
				String suffixString=listItr.next();
				Vector<String> tempAnalysis=MAR.getSuffixAnalysis(suffixString, paradigm);
				
				//System.out.println(tempAnalysis.size()+" "+analysis.size());
				
				analysis=joinVector(tempAnalysis, analysis);
				Vector<String> f = MAR.getSuffixFlag(suffixString, category);
				if (f!=null)
					flags.addAll(f);
			}
			
			if(analysis.size() == 0 && suffixList.size() > 1)
				analysis = MAR.getSuffixAnalysis("S", paradigm);
			
			Vector<String> rootAnalysis = MAR.getStemAnalysis(root, paradigm);
			Vector<String> stemflags=MAR.getStemFlag(root, category);
			if (stemflags!=null)
				flags.addAll(stemflags);

			if(rootAnalysis!=null){
				analysis=joinVector(rootAnalysis, analysis);
			}
			//	pout.println(analysis);
			
			
			if (!analysis.isEmpty())
				for(int i =0 ; i <analysis.size();i++){
					String analysisString = analysis.elementAt(i);
					marrv.add(new MorphologicalAnalyzerRuleResult(currentStemmerRuleResult,analysisString));
				}
			

			if(analysis.isEmpty()){
				marrv.add(new MorphologicalAnalyzerRuleResult(currentStemmerRuleResult));
			}

		}
		retVal = new MorphologicallyAnalyzedToken(token, marrv);
		//	pout.println("Token : "+token.getToken()+"Flags :"+flags);
		retVal.setFlags(new HashSet<String>(flags));
		for (MorphologicalAnalyzerRuleResult marr : retVal.getMorphologicalAnalyzerOutputs()){
			Logger.println(marr.toString());

			//		pout.println(marr.toString());
		}
		
		//System.out.println("Count of analysis : "+cnt);
		
		return retVal;
	}
	
	private Vector<String> joinVector(Vector<String> v1,Vector<String> v2){
		Vector<String> newAna=new Vector<String>();
		
		for(int i=0;i<v1.size();i++){
			for(int j=0;j<v2.size();j++){
				
				newAna.add(v1.elementAt(i).concat(",").concat(v2.elementAt(j)));
			}
		}
		if(newAna.isEmpty()){
			if(v1.size()>0){
				newAna=v1;
			}else{
				newAna=v2;
			}
		}
		return newAna;
	}

	/*
	private Vector<String> joinVector(Vector<String> v1,Vector<String> v2){
		//Vector<String> bigger,smaller;
		
		Vector<String> bigger = new Vector<String>();
		Vector<String> smaller = new Vector<String>();
		Vector<String> other = new Vector<String>();
		
		
		if (v1.size()>v2.size()){
			bigger=v1;
			smaller=v2;
		}else{
			smaller=v1;
			bigger=v2;
		}
		
		other = (Vector<String>)bigger.clone();
		
		for(int i=0;i<other.size();i++){
			for(int j=0;j<smaller.size();j++){
				String elematI=other.remove(i);
				other.add(i,elematI.concat(",").concat(smaller.elementAt(j)));

			}
		}
		
		
		
		return other;
	}
	 */

	public synchronized Vector<MorphologicallyAnalyzedToken> analyzeStub(Vector<StemmedToken> stv){
		Vector<MorphologicallyAnalyzedToken> retVal = new Vector<MorphologicallyAnalyzedToken>(stv.size());

		for(StemmedToken st : stv){
			MorphologicallyAnalyzedToken mat = new MorphologicallyAnalyzedToken(st);
			//System.out.println(st.getStemmedOutputs().toString());
			Vector<StemmerRuleResult> srrv = st.getStemmedOutputs();
			Iterator<StemmerRuleResult> srrvIte = srrv.iterator();
			while(srrvIte.hasNext())
			{
				StemmerRuleResult srresult = srrvIte.next();
				//System.out.println(srresult.toString());
				StemmerRuleResult tmp = new StemmerRuleResult(srresult);				
				mat.addMorphologicalAnalyzerOutput(new MorphologicalAnalyzerRuleResult(tmp));
	//			System.out.println(mat.getMorphologicalAnalyzerOutputs().toString()+"  Done..");
				//srresult=null;
			}
			if(mat != null){
				retVal.add(mat);
			}
		}
		if(retVal.size()==0){
			retVal = null;
		}
		return retVal;
	}
}
