using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

namespace WS
{
    public class InteractObjsComponent : OperateComponent<InteractObjsData>
    {
        ///<summary>音频实例</summary>
        private VoiceEntity Voice;

        ///<summary>音效实例</summary>
        private VoiceEntity EffectVoice;

        ///<summary>交互的父物体</summary>
        private GameObject InteractObjParent;

        ///<summary>可交互物体</summary>
        private List<GameObject> InteractObjs = new List<GameObject>();

        ///<summary>动画组件</summary>
        private Animator anim;

        ///<summary>流程管理器</summary>
        private ProcessManager processManager;

        ///<summary>动画组件</summary>
        private AnimatorComponent animatorComponent;

        public override void Init()
        {
            processManager = GameManager.Instance.ProcessManager;
            InteractObjParent = GameObject.Find(OperateData.InteractName);
            if (!string.IsNullOrEmpty(OperateData.AnimationName))
            {
                anim = InteractObjParent.transform.GetComponent<Animator>();
            }
            for (int i = 0; i < InteractObjParent.transform.childCount; i++)
            {
                InteractObjs.Add(InteractObjParent.transform.GetChild(i).gameObject);
            }
        }

        public override void StartOperate()
        {
            SetInteractObjStatus(true);
            animatorComponent = AddComponent<AnimatorComponent>();
            if (!string.IsNullOrEmpty(OperateData.AnimationName))
            {
                animatorComponent.InitInteractAnim(
                    OperateData.InteractName,
                    OperateData.AnimationName
                );
            }
            if (OperateData.AnswerObjDataList.Count > 0)
            {
                animatorComponent.InitAnswerAnim(OperateData.AnswerObjDataList);
            }

            HighlightComponent.Instance.SetOperateObjLayer(InteractObjParent.transform, 7);
            switch (GameManager.Instance.Data.Pattern)
            {
                case PatternType.教学:
                    Voice = VoiceManager.Instance.Play(VoiceType.Voice, OperateData.AudioName);
                    HighlightComponent.Instance.SetOperateObjLayer(InteractObjParent.transform, 7);
                    break;
                case PatternType.练习:
                    Voice = VoiceManager.Instance.Play(VoiceType.Voice, OperateData.AudioName);
                    break;
                case PatternType.考核:
                    break;
            }
        }

        public override async void Update()
        {
            //if (VRInputComponent.Instance.OperateObject != null)
            //{
            //    GameObject Obj = InteractObjs.Find((x) => { return x == VRInputComponent.Instance.OperateObject; });
            //    if (VRInputComponent.Instance.OperateObject == Obj)
            //    {
            //        if (VRInputComponent.Instance.GetKeyDown(VRKey.扳机, VRInputComponent.Instance.GetSelectPointer.Hand))
            //        {
            //            VoiceManager.Instance.Play(VoiceType.Effects, OperateData.AudioEffect);
            //            HighlightComponent.Instance.SetOperateObjLayer(InteractObjParent.transform, 6);
            //            AnimatorComponent component = GetComponent<AnimatorComponent>();
            //            await component.PlayAnimSerial(() =>
            //            {
            //                IsComplete = true;
            //            });
            //            SetInteractObjStatus(false);
            //        }
            //    }
            //    else
            //    {
            //        if (VRInputComponent.Instance.GetKeyDown(VRKey.扳机, VRInputComponent.Instance.GetSelectPointer.Hand))
            //        {
            //            processManager.GetVTaskRecord(processManager.GetCurrentModule().VTaskId).IsError = true;
            //        }
            //    }
            //}
        }

        public override void Prompt() { }

        public override Task StartState()
        {
            InteractObjParent = GameObject.Find(OperateData.InteractName);
            HighlightComponent.Instance.SetOperateObjLayer(InteractObjParent.transform, 6);
            if (!string.IsNullOrEmpty(OperateData.AnimationName))
            {
                anim = InteractObjParent.transform.GetComponent<Animator>();
                anim.Play(OperateData.AnimationName, -1, 0f);
                anim.speed = 0f;
                SceneDataSaveComponent.Instance.RemoveSceneData(
                    SceneComponent.Instance.ThisScene,
                    OperateData.InteractName,
                    OperateData.AnimationName
                );
            }

            for (int i = 0; i < OperateData.AnswerObjDataList.Count; i++)
            {
                GameObject answerObj = GameObject.Find(OperateData.AnswerObjDataList[i].AnswerName);
                string animName = OperateData.AnswerObjDataList[i].AnswerAnimName;
                if (!string.IsNullOrEmpty(animName))
                {
                    Animator answerAnim = answerObj.transform.GetComponent<Animator>();
                    answerAnim.Play(animName, -1, 0f);
                    answerAnim.speed = 0f;
                    SceneDataSaveComponent.Instance.RemoveSceneData(
                        SceneComponent.Instance.ThisScene,
                        OperateData.AnswerObjDataList[i].AnswerName,
                        OperateData.AnswerObjDataList[i].AnswerAnimName
                    );
                }
            }
            SetInteractObjStatus(false);
            IsComplete = false;
            return Task.CompletedTask;
        }

        public override Task EndState()
        {
            HighlightComponent.Instance.SetOperateObjLayer(InteractObjParent.transform, 6);
            if (!string.IsNullOrEmpty(OperateData.AnimationName))
            {
                anim.Play(OperateData.AnimationName, -1, 1);
                SceneDataSaveComponent.Instance.AddSceneData(
                    SceneComponent.Instance.ThisScene,
                    OperateData.InteractName,
                    OperateData.AnimationName
                );
            }

            for (int i = 0; i < OperateData.AnswerObjDataList.Count; i++)
            {
                GameObject answerObj = GameObject.Find(OperateData.AnswerObjDataList[i].AnswerName);
                string animName = OperateData.AnswerObjDataList[i].AnswerAnimName;
                if (!string.IsNullOrEmpty(animName))
                {
                    Animator answerAnim = answerObj.transform.GetComponent<Animator>();
                    answerAnim.Play(animName, -1, 1f);
                    SceneDataSaveComponent.Instance.RemoveSceneData(
                        SceneComponent.Instance.ThisScene,
                        OperateData.AnswerObjDataList[i].AnswerName,
                        OperateData.AnswerObjDataList[i].AnswerAnimName
                    );
                }
            }
            SetInteractObjStatus(false);

            IsComplete = true;
            return Task.CompletedTask;
        }

        /// <summary>
        /// 设置交互物体的交互和显示状态
        /// </summary>
        /// <param name="enabled"></param>
        private void SetInteractObjStatus(bool enabled)
        {
            if (OperateData.BeforeEnable)
            {
                Renderer[] renderers = InteractObjParent.GetComponentsInChildren<Renderer>();
                for (int i = 0; i < renderers.Length; i++)
                {
                    renderers[i].enabled = enabled;
                }
            }
            if (OperateData.InteractInside)
            {
                Collider[] colliders = InteractObjParent.GetComponentsInChildren<Collider>();
                for (int i = 0; i < colliders.Length; i++)
                {
                    colliders[i].enabled = enabled;
                }
            }
        }

        public override void Dispose()
        {
            base.Dispose();
            if (Voice != null)
            {
                if (!Voice.IsDisposed)
                    VoiceManager.Instance.Stop(Voice);
                Voice = null;
            }
            if (EffectVoice != null)
            {
                if (!EffectVoice.IsDisposed)
                    VoiceManager.Instance.Stop(EffectVoice);
                EffectVoice = null;
            }
            InteractObjParent = null;
        }
    }

    public class InteractObjsData
    {
        ///<summary>交互物体</summary>
        public string InteractName;

        ///<summary>动画名称</summary>
        public string AnimationName;

        ///<summary> 是否为临时物体 </summary>
        public bool BeforeEnable;

        ///<summary>响应物体列表</summary>
        public List<AnswerObjData> AnswerObjDataList = new List<AnswerObjData>();

        ///<summary>音频名称</summary>
        public string AudioName;

        ///<summary>交互音效</summary>
        public string AudioEffect;

        ///<summary> 是否有内部交互物体 </summary>
        public bool InteractInside;

        ///<summary> 同时播放动画 </summary>
        public bool IsSimulAni;
    }
}
