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

///<summary>交互</summary>
public class InteractAnimsComponent : OperateComponent<InteractAnimsData>
{
    ///<summary>音频实例</summary>
    private VoiceEntity Voice;

    ///<summary>正确音效</summary>
    private string successVoiceName = "T";

    ///<summary>错误音效</summary>
    private string failVoiceName = "F";

    ///<summary>正确音效实例</summary>
    private VoiceEntity successVoice;

    ///<summary>错误音效实例</summary>
    private VoiceEntity failVoice;

    ///<summary>交互物体</summary>
    private GameObject InteractObj;

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

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

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

    ///<summary>正在播放交互动画</summary>
    private bool onPlayeAnim = false;

    public override void Init()
    {
        processManager = GameManager.Instance.ProcessManager;
        InteractObj = GameObject.Find(OperateData.InteractName);
        if (!string.IsNullOrEmpty(OperateData.AnimationName))
        {
            anim = InteractObj.transform.GetComponent<Animator>();
        }
    }

    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);
        }
        switch (GameManager.Instance.Data.Pattern)
        {
            case PatternType.教学:
                Voice = VoiceManager.Instance.Play(VoiceType.Voice, OperateData.AudioName);
                HighlightComponent.Instance.SetOperateObjLayer(InteractObj.transform, 7);
                break;
            case PatternType.练习:
                Voice = VoiceManager.Instance.Play(VoiceType.Voice, OperateData.AudioName);
                break;
            case PatternType.考核:
                Voice = VoiceManager.Instance.Play(VoiceType.Voice, OperateData.AudioName);
                break;
        }
    }

    public override async void Update()
    {
        //if (VRInputComponent.Instance.OperateObject != null)
        //{
        //    if (VRInputComponent.Instance.OperateObject == InteractObj)
        //    {
        //        if (VRInputComponent.Instance.GetKeyDown(VRKey.扳机, VRInputComponent.Instance.GetSelectPointer.Hand) && !onPlayeAnim)
        //        {
        //            VoiceManager.Instance.Play(VoiceType.Effects, OperateData.AudioEffect);
        //            onPlayeAnim = true;
        //            HighlightComponent.Instance.SetOperateObjLayer(InteractObj.transform, 6);

        //            AnimatorComponent component = GetComponent<AnimatorComponent>();
        //            if(OperateData.IsSimulAni)
        //            {
        //                component.PlayAnimParallel(OnComplete);
        //            }
        //            else
        //            {
        //                await component.PlayAnimSerial(OnComplete);
        //            }
        //            SetInteractObjStatus(false);
        //        }
        //    }
        //    else
        //    {
        //        if (VRInputComponent.Instance.GetKeyDown(VRKey.扳机, VRInputComponent.Instance.GetSelectPointer.Hand))
        //        {
        //            if(!onPlayeAnim)
        //            {
        //                processManager.GetVTaskRecord(processManager.GetCurrentModule().VTaskId).IsError = true;
        //                failVoice = VoiceManager.Instance.Play(VoiceType.Voice, failVoiceName);
        //                await FacadeComponent.Instance.GetController<ProgressPopController>().ResultHint(false);
        //            }
        //            else
        //            {
        //                FacadeComponent.Instance.GetController<ProgressPopController>().WarningHint();
        //            }
        //        }
        //    }
        //}
    }

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

    public async void OnComplete()
    {
        if (
            GameManager.Instance.Data.Pattern != PatternType.教学
            && GameManager.Instance.Data.Pattern != PatternType.引导
        )
        {
            VoiceManager.Instance.Play(VoiceType.Tips, successVoiceName);
            await FacadeComponent.Instance.GetController<ProgressPopController>().ResultHint(true);
        }
        HighlightComponent.Instance.SetOperateObjLayer(InteractObj.transform, 6);
        IsComplete = true;
    }

    public override void Prompt()
    {
        HighlightComponent.Instance.SetOperateObjLayer(InteractObj.transform, 7);
    }

    public override Task StartState()
    {
        //Log.LogDebug("切换到初始状态"+GameManager.Instance.ProcessManager.GetCurrentModule().VTaskId);
        InteractObj = GameObject.Find(OperateData.InteractName);

        HighlightComponent.Instance.SetOperateObjLayer(InteractObj.transform, 6);
        if (!string.IsNullOrEmpty(OperateData.AnimationName))
        {
            anim = InteractObj.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()
    {
        //Log.LogDebug("切换到结束状态");
        HighlightComponent.Instance.SetOperateObjLayer(InteractObj.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.AddSceneData(
                    SceneComponent.Instance.ThisScene,
                    OperateData.AnswerObjDataList[i].AnswerName,
                    OperateData.AnswerObjDataList[i].AnswerAnimName
                );
            }
        }
        SetInteractObjStatus(false);

        IsComplete = true;
        return Task.CompletedTask;
    }

    public override void Dispose()
    {
        base.Dispose();
        if (Voice != null)
        {
            if (!Voice.IsDisposed)
                VoiceManager.Instance.Stop(Voice);
            Voice = null;
        }
        if (failVoice != null)
        {
            if (!failVoice.IsDisposed)
                VoiceManager.Instance.Stop(failVoice);
            failVoice = null;
        }
        if (successVoice != null)
        {
            if (!successVoice.IsDisposed)
                VoiceManager.Instance.Stop(successVoice);
            successVoice = null;
        }
        if (GameManager.Instance.Data.Pattern == PatternType.引导)
        {
            SceneDataSaveComponent.Instance.RemoveSceneData(
                SceneComponent.Instance.ThisScene,
                OperateData.InteractName,
                OperateData.AnimationName
            );
        }
        InteractObj = null;
        onPlayeAnim = false;
    }
}

///<summary>交互数据</summary>
public class InteractAnimsData
{
    ///<summary>交互物体</summary>
    public string InteractName;

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

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

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

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

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

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

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

public class AnswerObjData
{
    ///<summary>响应物体</summary>
    public string AnswerName = null;

    ///<summary>响应动画名称</summary>
    public string AnswerAnimName = null;
}
