﻿#if USING_MRTK

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Microsoft.MixedReality.Toolkit.UI;
using TMPro;


namespace SimplifyXR
{

    [DirectiveCategory(DirectiveCategories.Initiator, DirectiveSubCategory.UI, 
        DirectiveLibrary.MRTK, "Button Interaction", 
        directiveInfo ="This initiator starts a sequence when an <b>MRTK Pressable Button</b> interaction " +
        "is <b>detected.</b>")]
    public class MRTKButtonInteraction : Initiator
    {
        public enum InteractionType
        {
            Pressed,
            Released,
            TouchBegin,
            TouchEnd
        }

        /// <summary>
        /// The MRTK button to listen to
        /// </summary>
#if UNITY_EDITOR
        [Tooltip("The MRTK button to listen to."), Required]
#endif
        public PressableButton MRTKButton;

        /// <summary>
        /// The Interaction type to listen to
        /// </summary>
#if UNITY_EDITOR
        [Tooltip("The MRTK button interaction type."), 
            Conditional("MRTKButton", null, ComparisonType.NotEqual)]
#endif
        public InteractionType interactionType;

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

        public override List<KnobKeywords> SendKeywords()
        {
            return new List<KnobKeywords>(){
                new KnobKeywords("ButtonGameObject", typeof(GameObject)),
                new KnobKeywords("ButtonName", typeof(string)),
            };
        }

        protected void Start()
        {
            if (MRTKButton != null)
            {
                MRTKButtonHandler();
                return;
            }
            SimplifyXRDebug.SimplifyXRLog(SimplifyXRDebug.Type.AuthorError, "There is no button on this Directive {0}.", SimplifyXRDebug.Args(this));
        }

        protected new void OnDestroy()
        {
            base.OnDestroy();

            if(MRTKButton != null)
            {
                MRTKButtonHandler(false);
            }
        }

        void MRTKButtonHandler(bool subscribe=true)
        {
            switch (interactionType)
            {
                case InteractionType.Released:
                    if (subscribe)
                    {
                        MRTKButton.ButtonReleased.AddListener(Initiate);
                    }
                    else
                    {
                        MRTKButton.ButtonReleased.RemoveListener(Initiate);
                    }
                    break;
                case InteractionType.Pressed:
                    if (subscribe)
                    {
                        MRTKButton.ButtonPressed.AddListener(Initiate);
                    }
                    else
                    {
                        MRTKButton.ButtonPressed.RemoveListener(Initiate);
                    }
                    break;
                case InteractionType.TouchBegin:
                    if (subscribe)
                    {
                        MRTKButton.TouchBegin.AddListener(Initiate);
                    }
                    else
                    {
                        MRTKButton.TouchBegin.RemoveListener(Initiate);
                    }
                    break;
                case InteractionType.TouchEnd:
                    if (subscribe)
                    {
                        MRTKButton.TouchEnd.AddListener(Initiate);
                    }
                    else
                    {
                        MRTKButton.TouchEnd.RemoveListener(Initiate);
                    }
                    break;            
            }
        }

        public override void Initiate()
        {
            SendData();
            base.Initiate();
            SimplifyXRDebug.SimplifyXRLog(SimplifyXRDebug.Type.AuthorDebug, "This button {0} has been pressed.", SimplifyXRDebug.Args(MRTKButton));
        }

        void SendData()
        {
            string buttonText = "Default";
            buttonText = MRTKButton.name;
            var thisData = new List<object> { MRTKButton.gameObject, buttonText };
            var thisKeywords = new List<string> { "ButtonGameObject", "ButtonName" };
            AddPassableData(thisKeywords, thisData);
        }
    }
}

#endif







