if (SpeechRecognition == null) {
    SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
}

if (typeof SpeechRecognition !== "undefined") {
    if (g_recognition == null) {
        g_recognition = new SpeechRecognition();
        g_recognition.continuous = true;//false
        g_recognition.lang = 'vi-VN';
        g_recognition.interimResults = true;//false
        //recognition.maxAlternatives = 1;
        /*
        const onResult = (event) => {
            var result = event.results[event.results.length - 1];
            if (result.isFinal) {
                const text = result[0].transcript.trim();
                if (text == "clear") {
                    setValById(g_speechIdInput, "");
                } else {
                    setValById(g_speechIdInput, text);
                    var obj = document.getElementById(g_speechIdInput);
                    if (!isEmpty(obj)) {
                        obj.oninput();
                    }
                }
            }
        };
        */
        g_recognition.addEventListener("result", onResult);
    }
}
function onResult(event) {
    var result = event.results[event.results.length - 1];
    if (result.isFinal) {
        const text = result[0].transcript.trim();
        if (text.substring(text.length - 1) == ".") text = text.substring(0, text.length - 1);
        if (text == "clear") {
            setValById(g_speechIdInput, "");
        } else {
            setValById(g_speechIdInput, text);
            var obj = document.getElementById(g_speechIdInput);
            if (!isEmpty(obj)) {
                obj.oninput();
            }
        }
    }
}
function onSpeechEnd() {

}
function onNoMatch() {

}
function onError(event) {
    alert(event.error);
}
function speechStart(idinput, idbuttonmic) {
    if (g_isRecording) {
        g_recognition.stop();
        g_isRecording = false;
        if (g_idButtonMic.length > 0) {
            var obj = document.getElementById(g_idButtonMic);
            if (!isEmpty(obj)) {
                obj.classList = ("inputex_button speech");
            }
        }
        return;
    }
    g_speechIdInput = idinput;
    g_idButtonMic = idbuttonmic;
    g_recognition.start();
    g_isRecording = true;
    var obj = document.getElementById(g_idButtonMic);
    if (!isEmpty(obj)) {
        obj.classList = ("inputex_button speech selected");
    }
    console.log('Ready to receive a command.');
}
/*
recognition.onresult = function (event) {
    // The SpeechRecognitionEvent results property returns a SpeechRecognitionResultList object
    // The SpeechRecognitionResultList object contains SpeechRecognitionResult objects.
    // It has a getter so it can be accessed like an array
    // The first [0] returns the SpeechRecognitionResult at the last position.
    // Each SpeechRecognitionResult object contains SpeechRecognitionAlternative objects that contain individual results.
    // These also have getters so they can be accessed like arrays.
    // The second [0] returns the SpeechRecognitionAlternative at position 0.
    // We then return the transcript property of the SpeechRecognitionAlternative object
    var color = event.results[0][0].transcript;
    setValById(g_speechIdInput, color);
    var id = document.getElementById(g_speechIdInput);
    id.oninput();
}

recognition.onspeechend = function () {
    recognition.stop();
    g_speechIdInput = "";
}

recognition.onnomatch = function (event) {
    //diagnostic.textContent = "I didn't recognise that color.";
    setValById(g_speechIdInput, "I didn't recognise that color.");
    g_speechIdInput = "";
}

recognition.onerror = function (event) {
    //diagnostic.textContent = 'Error occurred in recognition: ' + event.error;
    setValById(g_speechIdInput, 'Error occurred in recognition: ' + event.error);
    g_speechIdInput = "";
}
*/