package iitb.cfilt.cpost.newstemmer;

import iitb.cfilt.cpost.lexicon.Wordlist;

public class StemmerRule {
	
	String category;
	String paradigm;
	String ultimateInsertion;
	String ultimateDeletion;
	String penultimateInsertion;
	String penultimateDeletion;
	
	int nopud = 0;
	int noud = 0;
	//String stem;
	String suffix;
	int priority;
	int depth = 0;
	
	public static final String DELIMITER = ",";
	public static final boolean MULTIPLE_POSSIBLE = false;
	
	public boolean equals(Object o){
		if(this == o)
			return true;
		if(!(o instanceof StemmerRule))
			return false;
		StemmerRule that = (StemmerRule)o;
		return 
			this.paradigm.equals(that.paradigm) && 
			this.ultimateInsertion.equals(that.ultimateInsertion) &&
			this.ultimateDeletion.equals(that.ultimateDeletion) &&
			this.penultimateInsertion.equals(that.penultimateInsertion) &&
			this.penultimateDeletion.equals(that.penultimateDeletion) &&
			this.category.equals(that.category) &&
			this.priority == that.priority;
	}
	
	public String getParadigm() {
		return paradigm;
	}
	public void setParadigm(String paradigm) {
		this.paradigm = paradigm;
	}
	
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	
	
	public String getPenultimateDeletion() {
		return penultimateDeletion;
	}
	public void setPenultimateDeletion(String penultimateDeletion) {
		this.penultimateDeletion = penultimateDeletion;
	}
	public String getPenultimateInsertion() {
		return penultimateInsertion;
	}
	public void setPenultimateInsertion(String penultimateInsertion) {
		this.penultimateInsertion = penultimateInsertion;
	}
	public int getDepth(){
		return this.depth;
	}
	public void setDepth(int depth){
		this.depth = depth;
	}
	public int getPriority() {
		return priority;
	}
	public void setPriority(int priority) {
		this.priority = priority;
	}
	public String getUltimateDeletion() {
		return ultimateDeletion;
	}
	public void setUltimateDeletion(String ultimateDeletion) {
		this.ultimateDeletion = ultimateDeletion;
	}
	public String getUltimateInsertion() {
		return ultimateInsertion;
	}
	public void setUltimateInsertion(String ultimateInsertion) {
		this.ultimateInsertion = ultimateInsertion;
	}
	
	public StemmerRule(String paradigm, String category, String ultimateInsertion, String ultimateDeletion, String penultimateInsertion, String penultimateDeletion, String suffix, int priority) {
		this.paradigm = paradigm;
		this.category = category;
		this.ultimateInsertion = ultimateInsertion;
		this.ultimateDeletion = ultimateDeletion;
		this.penultimateInsertion = penultimateInsertion;
		this.penultimateDeletion = penultimateDeletion;
		this.suffix = suffix;
		this.priority = priority;
	}

	public StemmerRule() {
		// TODO Auto-generated constructor stub
	}
	public StemmerRuleResult applyOn(String token, String suffix) {
		// TODO Set paradigm etc too here.
		//System.out.println("Working for : " + token + " and " + suffix);
		StemmerRuleResult retVal = null;
		if(this.ultimateDeletion.length() > 0 && !token.endsWith(this.ultimateDeletion))
			return(retVal);
		if(this.suffix != null && !(suffix.startsWith(this.suffix) || suffix.equals(this.suffix)))
			return(retVal);
		String result = token;
		noud = 0;
		nopud = 0;
		try {
			result = this.performUltimateDeletionOn(result);
			if(noud == 0)
			{
				result = this.performPenultimateDeletionOn(result);
				if(nopud == 0)
				{
					result = this.performPenultimateInsertionOn(result);
					result = this.performUltimateInsertionOn(result);
				}
				else
					return(null);
			}
			else
			{
				return(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		retVal = new StemmerRuleResult(result, this.paradigm, Wordlist.getCategory(this.paradigm), this.ultimateDeletion, suffix);
		System.out.println("Rule formed for " + token + " and " + suffix + " : " + retVal.toString());
		return retVal;
	}
	
	private String performUltimateInsertionOn(String result) {
		String retVal = result;
		if(ultimateInsertion != null && !ultimateInsertion.equals(""))
			retVal += ultimateInsertion; 
		return retVal;
	}
	private String performPenultimateInsertionOn(String result) {
		String retVal = result;
		if(penultimateInsertion != null && !penultimateInsertion.equals("")){
			if(result.length() > 0)
				retVal = retVal.substring(0,retVal.length()-1) + penultimateInsertion + retVal.substring(retVal.length() - 1);
			else
				retVal = penultimateInsertion;
		}
		return retVal;
	}
	private String performPenultimateDeletionOn(String result) throws Exception {
		String retVal = result;
		if(penultimateDeletion != null && !penultimateDeletion.equals("")){
			int lastIndexOfpud = retVal.lastIndexOf(penultimateDeletion);
			if(!retVal.endsWith(penultimateDeletion)){
				if(lastIndexOfpud != -1){
					retVal = retVal.substring(0,lastIndexOfpud) + retVal.substring(lastIndexOfpud).replaceFirst(penultimateDeletion, "");					
				}
				else{
//					throw new Exception("Token does not have the given penultimate deletion");
					nopud = 1;
				}
			}
			else{ 
				String tempRetVal = retVal.replaceAll(penultimateDeletion + "$","");
				retVal = this.performPenultimateDeletionOn(tempRetVal) + penultimateDeletion;
			}
		} 
		return retVal;
	}
	public String string(){
		String ret = "";
		ret = category + ", " + paradigm + ", " + ultimateInsertion + ", " + ultimateDeletion  + ", " + penultimateInsertion  + ", " + penultimateDeletion;
		return ret;
	}
	private String performUltimateDeletionOn(String result) throws Exception {
		String retVal = result;
		if(ultimateDeletion != null && !ultimateDeletion.equals("")){
			if(paradigm.equals("nst"))
				System.out.println("nst");
			if(retVal.endsWith(ultimateDeletion)){
				retVal = retVal.replaceAll(ultimateDeletion + "$", "");
			}
			else{
//				throw new Exception("Token does not end with given ultimate deletion");
				noud = 1;
			}
		} 
		return retVal;
	}
	
	public static void main(String args[]){
		String test = "Dinesh,Gadge,,,hey,hello,,,1";
		String[] tests = test.split(StemmerRule.DELIMITER);
		for(int i = 0; i < tests.length; i++){
			System.out.println("'"+tests[i]+"'" + " " + tests[i].equals(""));
		}
		String t = "Dinesh";
		System.out.println(t);
		System.out.println(t.replaceAll("esh$",""));
		System.out.println(t);
		String retVal = "Dineshesh";
		System.out.println(retVal.substring(0, retVal.length()-1));
	}
	
	
}
