// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        void speechRecognititionHandler(object sender, SpeechRecognizedEventArgs e)
        {
            //ui.log("Recognizer.speechRecognititionHandler()");
            //ui.setInputVolumeMetere(e.AudioLevel);
            //ui.log(e.Result.Confidence.ToString());

            String percent = Math.Round(Math.Round(e.Result.Confidence * 100)).ToString();
            Int16  p       = Int16.Parse(percent);


            //Console.WriteLine("speechRecognititionHandler(): e.Result.Grammar.Name = " + e.Result.Grammar.Name);
            //Console.WriteLine("speechRecognititionHandler(): e.Result.Text = " + e.Result.Text);

            Int16 conf = ui.getConfidenceLevel();

            if (p < conf)
            {
                ui.clearStatus();
                String msg = "Command: \"" + e.Result.Text + "\"\n";
                msg += "REJECTED with a " + percent + "% confidence level (minimum acceptance level is " + conf + "%)";
                ui.setStatus(msg);
            }
            else
            {
                ui.clearStatus();
                String msg = "Command \"" + e.Result.Text + "\"\n";
                msg += "ACCEPTED with a " + percent + "% confidence level (minimum acceptance level is " + conf + "%)";
                ui.setStatus(msg);

                commandHandler.processCommand(e.Result.Text);
            }
        }
        public void loadCommands(XmlNodeList arg, String profileName)
        {
            ui.log("GrammarHandler.loadCommands(xmlNodeList)");
            xmlCommands = arg;

            if (xmlCommands.Count == 0)
            {
                //MessageBox.Show("Please add some commands")
                lvCommands.Clear();
                ui.setStatus("Profile is empty, please createa some commands!");
                return;
            }

            if (commands != null)
            {
                commands.Clear();
            }



            // Reset
            //ui.recognizer.sre.Dispose();
            clear();


            // Configure ListView
            lvCommands.Clear();
            lvCommands.View      = View.Details;
            lvCommands.GridLines = true;
            lvCommands.Sorting   = SortOrder.Ascending;
            lvCommands.Columns.Add("Command", 500, HorizontalAlignment.Left);
            lvCommands.Columns.Add("Type", 146, HorizontalAlignment.Left);


            int cnt = 0;


            //Console.WriteLine("##### " + xmlCommands.Count);

            ui.log("---------------");
            foreach (XmlNode node in xmlCommands)
            {
                String speech = node.ChildNodes[0].InnerText.ToString();
                if (speech != "")
                {
                    cnt++;
                    speechCommands.Add(node.ChildNodes[0].InnerText.ToString());

                    //Console.WriteLine(command.InnerXml.ToString());
                    //Console.WriteLine(node.ChildNodes[0].InnerText);
                    //ui.log(node.ChildNodes[0].InnerText.ToString());

                    CommandVO command = new CommandVO();

                    command.method          = node.Attributes[0].InnerText.ToString();
                    command.haltOnAppChange = intStringToBoolean(node.Attributes[1].InnerText.ToString());
                    command.duration        = float.Parse(node.Attributes[2].InnerText.ToString());

                    command.speech    = node.ChildNodes[0].InnerText;
                    command.window    = node.ChildNodes[1].InnerText;
                    command.autoType  = node.ChildNodes[2].InnerText;
                    command.clipboard = node.ChildNodes[3].InnerText;
                    //Console.WriteLine("node.ChildNodes[3].Attributes[0].ToString() = " + node.ChildNodes[3].Attributes[0].Value.ToString());
                    command.clipboardAutoPaste = intStringToBoolean(node.ChildNodes[3].Attributes[0].Value.ToString());
                    command.multiKey           = node.ChildNodes[4].InnerText;
                    command.multiKeyCtrl       = intStringToBoolean(node.ChildNodes[4].Attributes[0].Value.ToString());
                    command.multiKeyAlt        = intStringToBoolean(node.ChildNodes[4].Attributes[1].Value.ToString());
                    command.multiKeyShift      = intStringToBoolean(node.ChildNodes[4].Attributes[2].Value.ToString());

                    ListViewItem listViewItem = new ListViewItem(command.speech);
                    listViewItem.SubItems.Add(command.method);
                    lvCommands.Items.Add(listViewItem);

                    commands.Add(command);

                    //ui.log(command.speech + ": " + "\t" + "method:" + command.method.ToString() + "\t" + "haltOnAppChange:" + command.haltOnAppChange.ToString() + "\t" + "+delay:" + command.delay.ToString());
                    ui.log("Command added: [" + command.speech + "]");
                    //ui.log("command.clipboard: [\n" + command.clipboard + "\n]");

                    //Server.UrlDecode
                }
            }
            ui.log("---------------");


            ui.stopListening();

            gb = new GrammarBuilder(speechCommands);
            g  = new Grammar(gb);
            ui.recognizer.sre.LoadGrammar(g);
            ui.recognizer.sre.RecognizeAsync(RecognizeMode.Multiple);


            ui.setStatus(cnt.ToString() + " commands loaded and ready!");


            EventHandler handler = grammarLoaded;

            if (null != handler)
            {
                handler(this, EventArgs.Empty);
            }
        }