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

public class LoadSceneComponent : OperateComponent<LoadSceneData>
{
    private MapController mapPop;

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

    public override void Init() { }

    public override void StartOperate()
    {
        //switch (GameManager.Instance.Data.Pattern)
        //{
        //    case PatternType.教学:
        //        break;
        //    case PatternType.练习:
        //        break;
        //    case PatternType.考核:
        //        break;
        //}
        GameManager.Instance.Data.pos = Player.Inst.transform.position;
        GameManager.Instance.Data.rot = Player.Inst.transform.rotation;
        Voice = VoiceManager.Instance.Play(VoiceType.Voice, OperateData.AudioName);
        mapPop = FacadeComponent.Instance.CreateController<MapController>();
        mapPop.OpenView();
        mapPop.SetView(
            OperateData.endSceneName,
            () =>
            {
                if (Player.Inst.gameObject != null)
                {
                    Player.Inst.transform.position = OperateData.pos;
                    Player.Inst.transform.rotation = Quaternion.Euler(OperateData.rot);
                }
                IsComplete = true;
            }
        );
    }

    public override async Task StartState()
    {
        if (OperateData.startSceneName == SceneName.None)
        {
            return;
        }
        if (mapPop != null)
        {
            mapPop.CloseView();
        }
        Player.Inst.transform.position = GameManager.Instance.Data.pos;
        Player.Inst.transform.rotation = GameManager.Instance.Data.rot;
        if (OperateData.startSceneName.ToString() != SceneComponent.Instance.ThisScene)
        {
            if (OperateData.startSceneName == SceneName.进场交通洞)
            {
                await SceneComponent.Instance.LoadSceneAsync(SceneName.发电机层.ToString());
            }
            else
            {
                await SceneComponent.Instance.LoadSceneAsync(OperateData.startSceneName.ToString());
            }
            SceneDataSaveComponent.Instance.LoadScene(OperateData.startSceneName.ToString());
        }
        else
        {
            return;
        }
    }

    public override async Task EndState()
    {
        if (OperateData.endSceneName == SceneName.None)
        {
            return;
        }
        GameManager.Instance.Data.pos = Player.Inst.transform.position;
        GameManager.Instance.Data.rot = Player.Inst.transform.rotation;
        if (mapPop != null)
        {
            mapPop.CloseView();
        }
        if (Player.Inst.gameObject != null)
        {
            Player.Inst.transform.position = OperateData.pos;
            Player.Inst.transform.rotation = Quaternion.Euler(OperateData.rot);
        }
        await SceneComponent.Instance.LoadSceneAsync(OperateData.endSceneName.ToString());

        SceneDataSaveComponent.Instance.LoadScene(OperateData.endSceneName.ToString());
    }

    public override void Update() { }
}

public class LoadSceneData
{
    /// <summary> 起始场景 </summary>
    public SceneName startSceneName;

    /// <summary> 目标场景 </summary>
    public SceneName endSceneName;

    /// <summary> 位置点 </summary>
    public string transferPoint;

    /// <summary> 位置 </summary>
    public Vector3 pos;

    /// <summary> 旋转 </summary>
    public Vector3 rot;

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