using UnityEngine;
using UnityEngine.UI;
using XvXR.Foundation;

namespace XvXR.Foundation.SampleScenes
{


    public class XvSpeechVoiceDemo : MonoBehaviour
    {
        public XvSpeechVoiceManager xvSpeechVoiceManager;
        private bool isRotate;
        public Transform obj;
        public AudioSource audioSource;
        public AudioClip voiceWake;
        public AudioClip voiceEnd;
        public GameObject StopCurrent;
        public Text modeText;

        [SerializeField]
        [Tooltip("Ƿʹ")]

        private bool isAvw = true;

        [SerializeField]
        [Tooltip("ǷԶ")]

        private bool runAwake = true;

        private void Awake()
        {
            if (xvSpeechVoiceManager == null)
            {
                xvSpeechVoiceManager = GameObject.FindObjectOfType<XvSpeechVoiceManager>();
            }
          

            StopCurrent.SetActive(isAvw);

            if (runAwake) { 
            Invoke("StartSpeechRecognition",3);

            }
        }

        private void Update()
        {
            if (isRotate)
            {
                obj.rotation *= Quaternion.Euler(Vector3.up * Time.deltaTime * 50);
            }


        }
        public void StartRotate()
        {
            isRotate = true;
        }
        public void StopRotate()
        {
            isRotate = false;
        }
        public void ScaleUp()
        {

            obj.localScale *= 1.1f;
        }
        public void ScaleDown()
        {
            obj.localScale *= 0.9f;
        }

      
        public void StartSpeechRecognition()
        {
            if (!xvSpeechVoiceManager.IsOn) {
               
                modeText.text = isAvw ? "ʶģʽ" : "ʶģʽ";

                if (isAvw)
                {

                    xvSpeechVoiceManager.StartAVW(5000, 500);
                }
                else
                {
                    xvSpeechVoiceManager.StartASR(5000, 500);
                }
                
            }
           
        }

        /// <summary>
        /// ֹͣʶ
        /// </summary>
        public void StopSpeechRecognition()
        {
            if (xvSpeechVoiceManager.IsOn) {
                modeText.text = isAvw ? "رʶģʽ" : "رʶģʽ";
                if (isAvw)
                {
                    //ֹͣѴʺʶ
                    xvSpeechVoiceManager.StopAVW();
                }
                else
                {
                    //ֹͣʶ
                    xvSpeechVoiceManager.StopASR();
                }
               
            }
        }

        /// <summary>
        /// ֹͣʵʶǻѴʶģʽֹͣʵʶ𣬲ֹͣѴʶ
        /// </summary>
        public void StopCurrentSpeechRecognition()
        {
            if (xvSpeechVoiceManager.IsOn)
            {
                modeText.text = isAvw ? "رʶģʽ" : "رʶģʽ";

                xvSpeechVoiceManager.StopASR();
               
            }
        }


        public void PlayVoiceWake()
        {
            if (audioSource != null && voiceWake != null)
            {
                audioSource.PlayOneShot(voiceWake);
            }
        }


      
        public void PlayVoiceEnd()
        {
            if (audioSource != null && voiceEnd != null)
            {
                audioSource.PlayOneShot(voiceEnd);
            }
        }


        public void OnRecognizedStatus(result result)
        {

            MyDebugTool.Log("OnRecognizedStatus:" + result.word);

        }


    }
}