package generator;

import util.State;

public class BigFstGenerator {

    private StringBuffer strBuff;

    public BigFstGenerator() {
    }

    public StringBuffer compute(State initState, String className) {
        strBuff = new StringBuffer();
        System.out.println("[FstGenerator] Transforming Fst to a java class");
        append("package generated;");

        append("public class " + className + " {");

        appendWithTab("public static float compute(int[] token) {", 1);

        appendWithTab("int pos=0;", 2);
        appendWithTab("float result=0f;", 2);
        appendWithTab("return state_0(token, pos, result);", 2);

        appendWithTab("}", 1);

        generateCases(initState, 2);

        append("}");
        System.out.println("[FstGenerator] Successfully transformed fst to " + className + ".java");
        return strBuff;
    }


    private void generateCases(State currentState, int tab) {
        append("\n\tprivate static float state_" + currentState.getId() +
                "(int[] token, int pos, float result) {");
       
        if( currentState.getNumArcs() > 0) { 
            generateTokenLengthTest(tab);
            appendWithTab("switch(token[pos++]) {", tab);
            for (int i = 0; i < currentState.getNumArcs(); i++) {
                appendWithTab("case " + currentState.getArc(i).getIlabel()  + ":", tab+1); 
                if (currentState.getArc(i).getWeight() != 0f) {
                    appendWithTab("result+=" + currentState.getArc(i).getWeight() + "f;", tab+2);
                }
                generateCases(currentState.getArc(i).getNextState(), tab+2);
            }
            appendWithTab("default:", tab+1);
            appendWithTab("return -1;", tab+2);
            appendWithTab("}", tab);
        } else {
            appendWithTab("return (pos!=token.length) ? -1 : result;", tab);
        }
        appendWithTab("}", 1);
    }

    private void generateTokenLengthTest(int tab) {
        appendWithTab("if(pos>=token.length) {return -1;}", tab);
    }

    private void append(String strToAppend) {
        strBuff.append(strToAppend);
        strBuff.append("\n");
    }

    private void appendWithTab(String strToAppend, int numberOfTab) {
        for (int i = 0; i < numberOfTab; i++) {
            strBuff.append("\t");
        }
        strBuff.append(strToAppend);
        strBuff.append("\n");
    }

}

@@ -704,19 +704,16 @@
 nitState
-, 2
 );%0A%0A    
@@ -915,17 +915,8 @@
 tate
-, int tab
 ) %7B%0A
@@ -1138,19 +1138,17 @@
 gthTest(
-tab
+2
 );%0A     
@@ -1194,19 +1194,17 @@
 +%5D) %7B%22, 
-tab
+2
 );%0A     
@@ -1347,21 +1347,17 @@
  + %22:%22, 
-tab+1
+3
 ); %0A    
@@ -1502,29 +1502,25 @@
 t() + %22f;%22, 
-tab+2
+4
 );%0A         
@@ -1597,21 +1597,17 @@
 tate(), 
-tab+2
+4
 );%0A     
@@ -1657,13 +1657,9 @@
 :%22, 
-tab+1
+3
 );%0A 
@@ -1701,13 +1701,9 @@
 ;%22, 
-tab+2
+4
 );%0A 
@@ -1728,27 +1728,25 @@
 ithTab(%22%7D%22, 
-tab
+2
 );%0A        %7D
@@ -1824,19 +1824,17 @@
 sult;%22, 
-tab
+2
 );%0A     

Remove tab number handler