var voiceBtn = document.getElementById("voiceBtn");
var voiceDiv = document.getElementById("voiceDiv");
var voiceSpan = document.getElementById("voiceSpan");
var heardSpan = document.getElementById("heardSpan");

var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
var SpeechGrammarList = SpeechGrammarList || webkitSpeechGrammarList;
var SpeechRecognitionEvent = SpeechRecognitionEvent || webkitSpeechRecognitionEvent;

voiceBtn.addEventListener("click", startListening);

function startListening(){
    voiceBtn.disabled = true;
    voiceSpan.innerText = 'Dites "Cristo !" pour commencer a donner votre requete';
    voiceSpan.style.display = "";
    heardSpan.innerText = "";
    heardSpan.style.display = "none";
    voiceBtn.innerText = "Ecoute en cours";

    listenForCristo();
}

function listenForCristo(){
    var cristo = [
        'Cristo',
        'cristo',
        'crist',
        'risto'
    ];

    var grammar = '#JSGF V1.0; grammar phrase; public <phrase> = cristaux;';
    var recognition = new SpeechRecognition();
    var speechRecognitionList = new SpeechGrammarList();
    speechRecognitionList.addFromString(grammar, 1);
    recognition.grammars = speechRecognitionList;
    recognition.lang = 'fr-FR';
    recognition.interimResults = false;
    recognition.maxAlternatives = 1;

    recognition.start();
    console.log('Started listening');
    recognition.onresult = function(event) {
        console.log('Listened');
        /* if(heard.toLowerCase().includes("cristo")){
            
        } */
        var speechResult = event.results[0][0].transcript.toLowerCase();
        console.log(speechResult);
        heardSpan.innerText += " A entendu = " + speechResult;
        heardSpan.style.display = "";
        if(speechResult.toLowerCase().includes("cristaux")){
            console.log('Heard Cristo ! (or Cristaux)');
        } else {
            console.log('Heard nothing');
        }
        voiceSpan.style.display = "none";
        console.log('Confidence: ' + event.results[0][0].confidence);
    }

    recognition.onspeechend = function() {
        recognition.stop();
        voiceBtn.disabled = false;
        voiceBtn.textContent = 'Ecouter a nouveau';
        voiceSpan.style.display = "none";
    }
    
    recognition.onerror = function(event) {
        voiceBtn.disabled = false;
        voiceBtn.textContent = 'Ecouter a nouveau';
        diagnosticPara.textContent = 'Error occurred in recognition: ' + event.error;
        voiceSpan.style.display = "none";
        heardSpan.innerText += " Erreur ";
        heardSpan.style.display = "";
    }
      
    recognition.onaudiostart = function(event) {
        //Fired when the user agent has started to capture audio.
        console.log('SpeechRecognition.onaudiostart');
     }
      
    recognition.onaudioend = function(event) {
        //Fired when the user agent has finished capturing audio.
        console.log('SpeechRecognition.onaudioend');
    }
      
    recognition.onend = function(event) {
        //Fired when the speech recognition service has disconnected.
        console.log('SpeechRecognition.onend');
        console.log("A entendu = " + event.results);
        voiceSpan.style.display = "none";
    }
      
    recognition.onnomatch = function(event) {
        //Fired when the speech recognition service returns a final result with no significant recognition. This may involve some degree of recognition, which doesn't meet or exceed the confidence threshold.
        console.log('SpeechRecognition.onnomatch');
    }
      
    recognition.onsoundstart = function(event) {
        //Fired when any sound — recognisable speech or not — has been detected.
        console.log('SpeechRecognition.onsoundstart');
    }
      
    recognition.onsoundend = function(event) {
        //Fired when any sound — recognisable speech or not — has stopped being detected.
        console.log('SpeechRecognition.onsoundend');
    }
      
    recognition.onspeechstart = function (event) {
        //Fired when sound that is recognised by the speech recognition service as speech has been detected.
        console.log('SpeechRecognition.onspeechstart');
    }
    recognition.onstart = function(event) {
        //Fired when the speech recognition service has begun listening to incoming audio with intent to recognize grammars associated with the current SpeechRecognition.
        console.log('SpeechRecognition.onstart');
    }
}