﻿#if USING_MRTK

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


namespace SimplifyXR
{
    /// <summary>
    /// Associates an MRTK voice command to this Initiator so any Actions following can be executed.
    /// </summary>
    [DirectiveCategory(DirectiveCategories.Initiator, DirectiveSubCategory.UserInteraction, 
        DirectiveLibrary.MRTK, "Speech Recognition", 
        directiveInfo ="This initiator starts a sequence when a <b>MRTK Keyword</b> is <b>recognized.</b>")]
    public class MRTKSpeechInitiator : Initiator, IMixedRealitySpeechHandler
    {

        public bool useSpeechInputHandler;
        [HideInInspector]
        public SpeechInputHandler speechInputHandler;
        [HideInInspector]
        public SpeechConfirmationTooltip speechConfirmationTooltip;


   
        [HideInInspector]
        public int selectedKeyword;

        /// <summary>
        /// The keyword for the voice command
        /// </summary>
        [HideInInspector]
        public string Keyword;     

        public override List<KnobKeywords> ReceiveKeywords()
        {
            return new List<KnobKeywords>();
        }

        public override List<KnobKeywords> SendKeywords()
        {
            return new List<KnobKeywords>();
        }

        void IMixedRealitySpeechHandler.OnSpeechKeywordRecognized(SpeechEventData eventData)
        {            
            if (eventData.Command.Keyword == Keyword)
            {
                Initiate();
            }
        }     


        protected void Start()
        {            
            //Debug.Log("Setting up listener");
            if (!useSpeechInputHandler)
            { 
                CoreServices.InputSystem?.RegisterHandler<IMixedRealitySpeechHandler>(this);
            }
            else
            {
                //Debug.Log("Adding response to speech input");
                if (useSpeechInputHandler && speechInputHandler != null)
                {
                    speechInputHandler.AddResponse(Keyword, Initiate);
                    speechInputHandler.SpeechConfirmationTooltipPrefab 
                        = speechConfirmationTooltip;
                }          
            }
        }

        protected new void OnDestroy()
        {
            base.OnDestroy();
            if(!useSpeechInputHandler)
            {
                CoreServices.InputSystem?.UnregisterHandler<IMixedRealitySpeechHandler>(this);
            }
            else if (speechInputHandler != null)
            {
                speechInputHandler.RemoveResponse(Keyword, Initiate);
            }    
        }

        public override void Initiate()
        {
            Debug.Log("Initiating");
            // Could put additional action here to add and extend functionality for other Voice Command initiators
            base.Initiate();
        }
    }
}

#endif
