/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sphinx;

import CommonFunctions.debugPrinter;
import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;
import java.net.URL;
import mdot.Main;
import structures.Constants;

/**
 *
 * @author Student
 */
public class Sphinx implements Runnable {

    private boolean running;

    public void Sphinx()
    {
        running = false;
        //this.run();
        

    }

    @Override
    public void run() {
        //Get path to sphinx.config.xml file
        URL url = null;

        try {
            url = new URL(Main.sphinx);
        } catch (Exception e) {
            System.out.println(e.toString());
        }

        //Setup the Configuration manager/Recognizer/Microphone
        ConfigurationManager cm = new ConfigurationManager(url);

        Recognizer recognizer = (Recognizer) cm.lookup("recognizer");
        recognizer.allocate();

        Microphone microphone = (Microphone) cm.lookup("microphone");

        System.out.println("SPHINX SETUP");

        //Main loop of thread
        //Only enter inner loop when sphinx start button clicked
        while (true) {
            while (isRunning()) {

                debugPrinter.printLine("Say any word: (fine | course | application | call | voice | end | quit).");
                debugPrinter.printLine("Start speaking.\n");

                //Set sphinx flag in context
                Main.getMe().setObjectAsString(Constants.HASH_SPHINX, "YES");
                Result result = null;
                try{
                //Get voice from microphone
                microphone.clear();
                microphone.startRecording();

                //Determine the spoken word
                result = recognizer.recognize();
                microphone.stopRecording();
                }
                catch(Exception ex){

                }

                if (result != null) {

                    //Get the word.
                    String resultText = result.getBestResultNoFiller();
                    //String resultText = result.getBestFinalResultNoFiller();



                    //Map the word to the corresponding scene
                    //if (resultText.length() != 0)
                    if (resultText != null && resultText.length() != 0)
                    {
                        String scene = "";
                        if (resultText.equals("course")) {
                            scene = "CarContextCourse";

                        } else if (resultText.equals("fine")) {
                            scene = "CarContextFine";

                        } else if (resultText.equals("apps")) {
                            scene = "Applications";

                        } else if (resultText.equals("voice")) {
                            scene = "Sphinx";

                        } else if (resultText.equals("call")) {
                            scene = "Voip";
                        }

                        //Put active scene in ME context
                        if(Main.getMe().GetObjectAsString(Constants.HASH_SPHINX) != null)
                            Main.getMe().setObjectAsString(Constants.HASH_SPHINX_ACTIVE_SCENE, scene);

                    }

                    debugPrinter.printLine("You said: " + resultText + "\n");

                } else {
                    debugPrinter.printLine("I can't hear what you said.\n");
                }
            }
        }
    }

    public void stop() {

        //Turn sphinx off
        setRunning(false);

        //Remove from ME
        Main.getMe().removeObject(Constants.HASH_SPHINX);
        Main.getMe().removeObject(Constants.HASH_SPHINX_ACTIVE_SCENE);

    }

    /**
     * @return the running
     */
    public boolean isRunning() {
        return running;
    }

    /**
     * @param running the running to set
     */
    public void setRunning(boolean running) {
        this.running = running;
    }
}
