using System.Threading.Tasks;
using UnityEngine;

namespace WS
{
    [ObjectSystem]
    public class JumpToComponentLateUpdateSystem : LateUpdateSystem<JumpToComponent>
    {
        public override void LateUpdate(JumpToComponent self)
        {
            self.LateUpdate();
        }
    }

    public class JumpToComponent : OperateComponent<JumpToData>
    {
        private GameObject start;

        ///<summary>音频实例</summary>
        private VoiceEntity Voice;

        public override void Init() { }

        public override void StartOperate()
        {
            if (start == null)
            {
                start = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/TransferPoint"));
            }
            Voice = VoiceManager.Instance.Play(VoiceType.Voice, OperateData.audioName);

            switch (GameManager.Instance.Data.Pattern)
            {
                case PatternType.教学:
                    break;
                case PatternType.练习:
                    break;
                case PatternType.考核:
                    break;
            }
            start.transform.position = OperateData.startPos;
        }

        public void LateUpdate()
        {
            //if (VRInputComponent.Instance.GetKeyUp(VRKey.圆盘, VRHand.Any))
            //{
            //    if (Vector3.Distance(Player.Inst.transform.position, OperateData.startPos) <= 1f)
            //    {
            //        Player.Inst.transform.position = OperateData.endPos;
            //        Player.Inst.transform.rotation = Quaternion.Euler(OperateData.endRot);
            //        IsComplete = true;
            //    }
            //}
        }

        public override Task StartState()
        {
            if (start == null)
            {
                start = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/TeleportPoint"));
            }
            start.gameObject.SetActive(true);
            Player.Inst.transform.position = OperateData.startPos;
            return Task.CompletedTask;
        }

        public override Task EndState()
        {
            if (start == null)
            {
                start = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/TeleportPoint"));
            }
            start.gameObject.SetActive(false);
            Player.Inst.transform.position = OperateData.endPos;
            Player.Inst.transform.rotation = Quaternion.Euler(OperateData.endRot);
            return Task.CompletedTask;
        }

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

        public override void Update() { }
    }

    public class JumpToData
    {
        /// <summary> 起点 </summary>
        public Vector3 startPos;

        /// <summary> 终点 </summary>
        public Vector3 endPos;

        /// <summary> 起始旋转 </summary>
        public Vector3 startRot;

        /// <summary> 终点旋转 </summary>
        public Vector3 endRot;

        /// <summary> 起点名称 </summary>
        public string startName;

        /// <summary> 终点名称 </summary>
        public string endName;

        /// <summary> 音频 </summary>
        public string audioName;
    }
}
