using System.Threading.Tasks;
using UnityEngine;
using WS;

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

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

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

    ///<summary>响应物体</summary>
    private GameObject AnswerObj;

    ///<summary>响应物体动画组件</summary>
    private Animator answerAnim;

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

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

    public override void StartOperate()
    {
        SetInteractObjStatus(true);

        HighlightComponent.Instance.SetOperateObjLayer(InteractObj.transform, 7);
        if (
            !string.IsNullOrEmpty(OperateData.AnimationName)
            && !string.IsNullOrEmpty(OperateData.AnswerAnimName)
        )
        {
            AddComponent<AnimatorComponent, Animator, Animator>(anim, answerAnim);
        }
        else
        {
            if (!string.IsNullOrEmpty(OperateData.AnimationName))
            {
                AddComponent<AnimatorComponent, Animator>(anim);
            }
            if (!string.IsNullOrEmpty(OperateData.AnswerAnimName))
            {
                AddComponent<AnimatorComponent, Animator>(answerAnim);
            }
        }

        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.考核:
                break;
        }
    }

    public override void Update()
    {
        //if (VRInputComponent.Instance.OperateObject != null)
        //{
        //    if (VRInputComponent.Instance.OperateObject == InteractObj)
        //    {
        //        if (VRInputComponent.Instance.GetKeyDown(VRKey.扳机, VRInputComponent.Instance.GetSelectPointer.Hand))
        //        {
        //            VoiceManager.Instance.Play(VoiceType.Effects, OperateData.AudioEffect);

        //            HighlightComponent.Instance.SetOperateObjLayer(InteractObj.transform, 6);

        //            AnimatorComponent component = GetComponent<AnimatorComponent>();
        //            if (!string.IsNullOrEmpty(OperateData.AnimationName) && !string.IsNullOrEmpty(OperateData.AnswerAnimName))
        //            {
        //                component.PlayAnim(OperateData.AnimationName, OperateData.AnswerAnimName, OperateData.IsSimulAni, () =>
        //                {

        //                    SceneDataSaveComponent.Instance.AddSceneData(SceneComponent.Instance.ThisScene,OperateData.InteractName,OperateData.AnimationName);
        //                    SceneDataSaveComponent.Instance.AddSceneData(SceneComponent.Instance.ThisScene, OperateData.AnswerName, OperateData.AnswerAnimName);
        //                    IsComplete = true;
        //                });
        //            }
        //            else
        //            {
        //                if(!string.IsNullOrEmpty(OperateData.AnimationName))
        //                {
        //                    component.PlayAnim(OperateData.AnimationName, () =>
        //                    {

        //                        SceneDataSaveComponent.Instance.AddSceneData(SceneComponent.Instance.ThisScene, OperateData.InteractName, OperateData.AnimationName);
        //                        IsComplete = true;
        //                    });
        //                }
        //                else if (!string.IsNullOrEmpty(OperateData.AnswerAnimName))
        //                {
        //                    component.PlayAnim(OperateData.AnswerAnimName, () =>
        //                    {
        //                        SceneDataSaveComponent.Instance.AddSceneData(SceneComponent.Instance.ThisScene, OperateData.AnswerName, OperateData.AnswerAnimName);
        //                        IsComplete = true;
        //                    });
        //                }
        //                else
        //                {
        //                    IsComplete = true;
        //                }
        //            }
        //            SetInteractObjStatus(false);
        //        }
        //    }
        //    else
        //    {
        //        if (VRInputComponent.Instance.GetKeyDown(VRKey.扳机, VRInputComponent.Instance.GetSelectPointer.Hand))
        //        {
        //            processManager.GetVTaskRecord(processManager.GetCurrentModule().VTaskId).IsError = true;
        //        }
        //    }
        //}
    }

    /// <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 override void Prompt()
    {
        HighlightComponent.Instance.SetOperateObjLayer(InteractObj.transform, 7);
    }

    public override Task StartState()
    {
        //Log.LogDebug("切换到初始状态");
        InteractObj = GameObject.Find(OperateData.InteractName);
        if (!string.IsNullOrEmpty(OperateData.AnswerName))
        {
            AnswerObj = GameObject.Find(OperateData.AnswerName);
        }

        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
            );
        }
        if (!string.IsNullOrEmpty(OperateData.AnswerAnimName))
        {
            answerAnim = AnswerObj.transform.GetComponent<Animator>();
            answerAnim.Play(OperateData.AnswerAnimName, -1, 0f);
            answerAnim.speed = 0f;
            SceneDataSaveComponent.Instance.RemoveSceneData(
                SceneComponent.Instance.ThisScene,
                OperateData.AnswerName,
                OperateData.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
            );
        }

        if (!string.IsNullOrEmpty(OperateData.AnswerAnimName))
        {
            answerAnim.Play(OperateData.AnswerAnimName, -1, 1);
            SceneDataSaveComponent.Instance.AddSceneData(
                SceneComponent.Instance.ThisScene,
                OperateData.AnswerName,
                OperateData.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 (GameManager.Instance.Data.Pattern == PatternType.引导)
        {
            SceneDataSaveComponent.Instance.RemoveSceneData(
                SceneComponent.Instance.ThisScene,
                OperateData.InteractName,
                OperateData.AnimationName
            );
        }
        InteractObj = null;
    }
}

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

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

    ///<summary>响应物体</summary>
    public string AnswerName;

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

    ///<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;
}
