/** Run jQuery scripts */
( function ( $ ) {
    "use strict";

    /** Document Ready. */
    $( document ).ready( function () {

        if(qc_voice_search_form.qc_search_allow_control == 'on'){
        // Speech To Text Variables
         let SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition,
            recognition,
            recording = false;

            var qc_voice_search = 'input[name=s], input[name=search]';

            var qc_voice_search_array = qc_voice_search.split(",").map(item => item.trim());

            $.each(qc_voice_search_array,function(i, e){
                //console.log( e );
                var qc_voice_search_name = e;

                var qc_voice_search_currentDom = $( qc_voice_search_name );
                var stt_unique_check_class = qc_voice_search_currentDom.length;

                if( typeof stt_unique_check_class !== 'undefined' && stt_unique_check_class > 0  ){

                    qc_voice_search_currentDom.after(qc_voice_search_form.qc_search_data);

                    qc_voice_search_currentDom.parent().css("position", 'relative' );

                    //console.log( qc_voice_search_currentDom.parent().attr('width') );
                    //console.log( qc_voice_search_currentDom.parent().innerHeight() );

                    //var innerHeight = qc_voice_search_currentDom.closest('form').find('.qc_voice_search_input').innerHeight();

                    //qc_voice_search_currentDom.parent().find('.qc_voice_search_wrapper').css("top",  ( parseInt(innerHeight) - 20 ) / 2 );


                    if( qc_voice_search_form.qc_search_remove_submit == 'on' ){
                        qc_voice_search_currentDom.closest('form').find('button[type=submit]').hide();
                    }

                    // setTimeout( function () {


                    //     qc_voice_search_currentDom.closest('form').addClass('qc_voice_search_form');
                    //     qc_voice_search_currentDom.closest('form').find('input').addClass('qc_voice_search_input');

                    //     var ss = document.querySelector("form[role=search]");
                    //     var s = ss.getBoundingClientRect();
                    //     //var n = qc_voice_search_currentDom;
                    //    // var n = $('.qc_voice_search_input');
                    //     var n = document.querySelector(e);
                    //     var u = n.getBoundingClientRect();

                    //     var m = $('.qc_voice_search_wrapper');
                    //     var mm = document.querySelector('.qc_voice_search_wrapper');
                    //     var ii = mm.getBoundingClientRect();
                    //     var a = { top: parseInt(u.top) - parseInt(u.height), right: u.right - s.right, bottom: u.bottom - s.bottom, left: u.left - s.left };

                    //     console.log(  )

                    //     var innerHeight = qc_voice_search_currentDom.closest('form').find('.qc_voice_search_input').innerHeight();
                    //     var innerWidth = qc_voice_search_currentDom.closest('form').find('.qc_voice_search_input').innerWidth();

                    //     //console.log( innerHeight );

                    //     $( qc_voice_search_name ).parent().css("position", 'relative' );
                    //     m.css("position", 'absolute' );
                    //     m.css("right", Math.abs(a.right) );
                    //     m.css("top",  ( parseInt(innerHeight) - 20 ) / 2 );
                        
                    // }, 1000 );
                }


            });

            var result = '';
            

            // Speech To Text Function
            function qc_voice_search_speechToText(currentDoom) {
              try {
                var inputLang = currentDoom.parent().parent().find('.qc_voice_search_language').find(":selected").val();
                //console.log(inputLang)
                recognition = new SpeechRecognition();
                recognition.lang = inputLang ? inputLang : qc_voice_search_form.qc_search_lang_code;
                recognition.interimResults = true;
                recognition.start();
                recognition.onresult = (event) => {
                  const speechResult = event.results[0][0].transcript;
                  if (event.results[0].isFinal) {
                    // console.log( speechResult)
                    var check_input_field =  currentDoom.parent().parent().find('input');
                    if(check_input_field.length){
                        check_input_field.val( check_input_field.val() +" "+ speechResult );
                    }

                    var check_textarea_field =  currentDoom.parent().parent().find('textarea');
                    if(check_textarea_field.length){
                        check_textarea_field.val( check_textarea_field.val() +" "+ speechResult )
                    }
                  } 

                };
                recognition.onspeechend = () => {
                  qc_voice_search_speechToText(currentDoom);
                };
                recognition.onerror = (event) => {
                  qc_voice_search_stopRecording(currentDoom);
                };
              } catch (error) {
                recording = false;
                console.log(error);
              }
            }

            // Stop Converting Button Function
            function qc_voice_search_stopRecording(currentDoom) {
              recognition.stop();
              recording = false;
            }

            $(document).on('click', '.qc_voice_search_audio_record', function (e) {
                e.preventDefault();
                var currentDoom = $(this);
                //console.log(currentDoom)

                if($('.qc_voice_search_audio_record').hasClass("qc_voice_search_pulse")){
                    $('.qc_voice_search_audio_record').removeClass('qc_voice_search_pulse');
                    $('.qc_voice_search_audio_record').find('.fa-microphone-slash').addClass('fa-microphone').removeClass('fa-microphone-slash');
                    qc_voice_search_stopRecording(currentDoom);
                }


                currentDoom.addClass('qc_voice_search_pulse');
                currentDoom.find('.fa-microphone').addClass('fa-microphone-slash').removeClass('fa-microphone');

                if (!recording) {
                    qc_voice_search_speechToText(currentDoom);
                    recording = true;
                } else {
                    qc_voice_search_stopRecording(currentDoom);
                }
        
            });

            $(document).on('click', '.qc_voice_search_pulse', function (e) {
                e.preventDefault();
                var currentDoom = $(this);
                currentDoom.removeClass('qc_voice_search_pulse');
                currentDoom.find('.fa-microphone-slash').addClass('fa-microphone').removeClass('fa-microphone-slash');
                
                recognition.stop();
                recording = false;

                if(qc_voice_search_form.qc_search_form_auto_submit == 'on'){
                    setTimeout( function () {
                        currentDoom.closest("form").find( "button[type=submit]" ).trigger( "click" );
                    }, 1000 );
                }

            });

            // Getting Languages Option Function 
            var inputLanguage = $(".qc_voice_search_language").length;
            if(inputLanguage){

                qc_voice_search_languages.forEach((lang) => {
                    const option = document.createElement("option");
                    option.value = lang.code;
                    option.innerHTML = lang.code;
                    $(".qc_voice_search_language").append(option);
                });

            }


        }

    });

} ( jQuery ) );