﻿#if USING_MRTK

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Microsoft.MixedReality.Toolkit;
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.UI;


namespace SimplifyXR
{

    [CustomEditor(typeof(MRTKSpeechInitiator))]
    public class MRTKSpeechInitiatorEditor : SimpleAREditor
    {
        string[] GetVoiceKeywords()
        {
            string[] keywords = null;
            try
            {
#if UNITY_EDITOR_WIN
                SpeechCommands[] commands =
                Microsoft.MixedReality.Toolkit.CoreServices.InputSystem.InputSystemProfile.SpeechCommandsProfile?.SpeechCommands;

               keywords =
                    new string[commands.Length];

                for (int i = 0; i < commands.Length; i++)
                {
                    keywords[i] = commands[i].Keyword;
                }
#endif
            }
            catch(System.Exception e) { };
           
            return keywords;
        }


        public override void OnInspectorGUI()
        {
            MRTKSpeechInitiator speechInitiator = (MRTKSpeechInitiator)target;

            string[] keywords = GetVoiceKeywords();           
           

            base.OnInspectorGUI();

            if (keywords != null)
            {
                speechInitiator.selectedKeyword =
              EditorGUILayout.Popup(
                  new GUIContent("MRTK Keyword",
                  "The MRTK voice command keyword to use. " +
                  "These can be updated in the MRTK Voice Commands input profile."),
              speechInitiator.selectedKeyword, keywords);
            }
            else
            {               
                GUILayout.Label(
                  "No supported MRTK Speech Commands Profile found.",
                   EditorStyles.helpBox);
                  //return;                
            }

            if (GUI.changed) speechInitiator.Keyword = GetVoiceKeywords()[speechInitiator.selectedKeyword];

            if (speechInitiator.useSpeechInputHandler)
            {
              
                speechInitiator.speechInputHandler =
                    (SpeechInputHandler) EditorGUILayout.ObjectField(
                        "Speech Input Handler",
                        speechInitiator.speechInputHandler,
                        typeof(SpeechInputHandler), true);

                speechInitiator.speechConfirmationTooltip =
                     (SpeechConfirmationTooltip)EditorGUILayout.ObjectField(
                         "Speech Confirmation Tool",
                        speechInitiator.speechConfirmationTooltip,
                        typeof(SpeechConfirmationTooltip), true);

            }

            if(GUI.changed) EditorUtility.SetDirty(speechInitiator);

        }

    }

}

#endif
