﻿#if USING_MRTK2

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


namespace SimplifyXR
{
    /// <summary>
    /// A Simple Initiator used to Execute actions for a UI Slider Pressed.
    /// </summary>
    [DirectiveCategory(DirectiveCategories.Initiator,
        DirectiveSubCategory.UI, DirectiveLibrary.MRTK2, "Slider Interaction",
        "This <b>initiator</b> begins when an <b>MRTK slider</b> is interacted with")]
    public class MRTKSliderInitiator : Initiator
    {
        /// <summary>
        /// The Slider to Listen to.
        /// </summary>
#if UNITY_EDITOR
        [Tooltip("The Slider to Listen to."), Required, SceneTip]
#endif
        public PinchSlider SliderToPress;
        /// <summary>
        /// This is the float value that will allow you to interact with the Slider UI Component.
        /// </summary>
#if UNITY_EDITOR
        [Tooltip("This is the float value that will allow you to interact with the Slider UI Component."), HideInInspector]
#endif
        public float SliderValue;

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

        public override List<KnobKeywords> SendKeywords()
        {
            return new List<KnobKeywords>(){
                new KnobKeywords("SliderGameObject", typeof(GameObject)),
                new KnobKeywords("SliderTitle", typeof(string)),
                new KnobKeywords("SliderValue", typeof(float))
            };
        }

        protected void Start()
        {
            PinchSlider slider = SliderToPress.GetComponent<PinchSlider>();
            if (slider != null)
            {
                slider.OnValueUpdated.AddListener(MySliderPressed);
            }
            else
                SimplifyXRDebug.SimplifyXRLog(SimplifyXRDebug.Type.AuthorError, "There is no slider on this Directive {0}.", SimplifyXRDebug.Args(this));
        }

        protected new void OnDestroy()
        {
            base.OnDestroy();
            if (SliderToPress != null)
            {
                SliderToPress.OnValueUpdated.RemoveListener(MySliderPressed);
            }

        }


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

        private void MySliderPressed(SliderEventData sliderData)
        {
            float value = sliderData.NewValue;
            SliderValue = value;
            Initiate();
        }

        private void SendData()
        {
            string sliderText = null;
            if (SliderToPress.gameObject.GetComponentInChildren<Text>() != null)
                sliderText = SliderToPress.gameObject.GetComponentInChildren<Text>().text;
            var thisData = new List<object> { SliderToPress.gameObject, sliderText, SliderValue };
            var thisKeywords = new List<string> { "SliderGameObject", "SliderTitle", "SliderValue" };
            AddPassableData(thisKeywords, thisData);
        }
    }
}

#endif