
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Speech recognition functions
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//




// Put text into this element
var SpeechRecognitionInputElementId = null;
var bSpeechRecognitionInputSendCrLf = false;
var bSpeechRecognitionInputRemoveSpaces = false;

function OpenSpeechRecognitionDialog(InputElementId, bInputSendCrLf, bInputRemoveSpaces) {

    // Remember this for all operations
    SpeechRecognitionInputElementId = InputElementId;
    bSpeechRecognitionInputSendCrLf = bInputSendCrLf;
    bSpeechRecognitionInputRemoveSpaces = bInputRemoveSpaces;

    var nDlgWidth = 400;
    var nDlgHeight = 325;
    var nWindowWidth = $(window).width();
    var nWindowHeight = $(window).height();
    if (nWindowWidth < nDlgWidth)
        nDlgWidth = nWindowWidth * 0.95;
    if (nWindowHeight < nDlgHeight)
        nDlgHeight = nWindowHeight * 0.95;

    // Open the dialog
    // igniteui.com/help/igdialog-property-reference
    $("#igDialogSpeechRecognition").igDialog({ modal: true, headerText: "Speak Now!", width: nDlgWidth, height: nDlgHeight, resizable: true, closeOnEscape: true, openAnimation: "", closeAnimation: "fade", stateChanged: function (event, ui) { OnSpeechRecognitionStateChanged(event, ui); } });
    $("#igDialogSpeechRecognition").igDialog("open");

    CenterDialog("igDialogSpeechRecognition");

    // Start with no text
    ClearSpeechRecognitionDialogText();

    // Start recognizing speech
    StartSpeechRecognitionForDialog();
}


function ClearSpeechRecognitionDialogText() {

    $("#SpeechRecognitionTextArea").val('');
}

function AcceptSpeechRecognitionDialogText() {

    var SpeechText = $("#SpeechRecognitionTextArea").val();
    if (!SpeechText)
        return;

    ToastMessage("Speech Accepted", SpeechText);

    var InputElementObject = $("#" + SpeechRecognitionInputElementId);
    if (InputElementObject && InputElementObject.length > 0) {

        // Set the text into the input element
        InputElementObject.val(SpeechText);

        // Try both jQuery and Html focus functions
        InputElementObject.focus();
        InputElementObject[0].focus();

        if (bSpeechRecognitionInputSendCrLf) {

            var CrPress = jQuery.Event("keyup");
            CrPress.ctrlKey = false;
            CrPress.which = 13;
            CrPress.currentTarget = InputElementObject[0];

            var LfPress = jQuery.Event("keyup");
            LfPress.ctrlKey = false;
            LfPress.currentTarget = InputElementObject[0];
            LfPress.which = 10;

            var EnterPress = jQuery.Event("keyup");
            EnterPress.ctrlKey = false;
            EnterPress.which = 13;
            EnterPress.key = "Enter";
            EnterPress.currentTarget = InputElementObject[0];

            InputElementObject.trigger(CrPress);
            InputElementObject.trigger(LfPress);
            InputElementObject.trigger(EnterPress);
        }
    }


    CloseSpeechRecognitionDialog();
}



function OnSpeechRecognitionStateChanged(event, ui) {

    if (!ui)
        return;
    if (!ui.action)
        return;

    // Close event
    if (ui.action == 'close') {

        // Special processing when "X" button is pressed
        var bCloseButton = (ui.button == 'close');
        if (bCloseButton) {
        }

        StopSpeechRecognitionForDialog();
        CloseSpeechRecognitionDialog();
    }
}

function OnSpeechRecognitionDialogKeyUp(event) {

    if (!event)
        return;

    // Enter key
    if (event.key === "Enter") {
        // "Click" the OK button to close the dialog
        $("#igDialogSpeechRecognitionOkButton").click();
        return;
    }
}




//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Speech Recognition
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//


function StartSpeechRecognition(textboxid, micbtnid, callback) {

    // https://github.com/TalAter/annyang/blob/master/docs/README.md
    if (annyang) {
        annyang.addCallback('result', function (phrases) {
            if (callback && phrases && phrases.length > 0) {
                callback(textboxid, micbtnid, phrases);
            }
        });

        // Start listening. You can call this here, or attach this call to an event, button, etc.
        annyang.start({ autoRestart: true, continuous: true });
    }
}

function StopSpeechRecognition() {

    // https://github.com/TalAter/annyang/blob/master/docs/README.md
    if (annyang) {
        // Stop listening.
        annyang.abort();
    }
}



//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//


function StartSpeechRecognitionForDialog() {

    // https://github.com/TalAter/annyang/blob/master/docs/README.md
    if (annyang) {
        annyang.addCallback('result', function (phrases) {
            SpeechRecognitionDialogResultsCallback(phrases);
            //console.log(phrases);
        });

        // Start listening. You can call this here, or attach this call to an event, button, etc.
        annyang.start({ autoRestart: true, continuous: true });
    }
}

function StopSpeechRecognitionForDialog() {

    // https://github.com/TalAter/annyang/blob/master/docs/README.md
    if (annyang) {
        // Stop listening.
        annyang.abort();
    }
}


function SpeechRecognitionDialogResultsCallback(phrases) {

    if (!phrases)
        return;
    if (phrases.length == 0)
        return;

    var sBestPhrase = phrases[0];
    if (!sBestPhrase)
        return;

    var TestAlphaRegExp = /[a-zA-Z]/g;
    var bHasAlpha = TestAlphaRegExp.test(sBestPhrase);

    // Remove spaces if its not alpha (numeric strings often have spaces)
    if (bSpeechRecognitionInputRemoveSpaces && !bHasAlpha)
        sBestPhrase = sBestPhrase.replace(/\s/g, '');

    // Remove commas (1,000 becomes 1000)
    sBestPhrase = sBestPhrase.replace(/,/g, '');

    // Remove trailing period that appears on some browsers
    // Example: 1000.
    if (sBestPhrase[sBestPhrase.length - 1] == '.')
        sBestPhrase = sBestPhrase.substring(0, sBestPhrase.length-1);

    if (sBestPhrase) {
        // Set the text
        $("#SpeechRecognitionTextArea").val(sBestPhrase);
    }

    //console.log(phrases);
}



//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Close the dialog
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
function CloseSpeechRecognitionDialog() {
    $("#igDialogSpeechRecognition").igDialog("close");

    // Stop recognizing speech
    StopSpeechRecognitionForDialog();

    bSpeechRecognitionDialogIsOpen = false;
    SpeechRecognitionInputElementId = null;
}

function IsSpeechRecognitionDialogOpen() {
    return bSpeechRecognitionDialogIsOpen;
}


