using CMF;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using WS;

public class FlowAnimator : FlowBase
{
    public Transform movePoint;
    public Animation animation;
    public AnimationClip clip;
    public float animatorTime;
    public CameraController controller;

    public override async void StartFlow()
    {
        base.StartFlow();
        FacadeComponent.Instance.GetController<PointController>().ShowPoint(0, null);
        await WaitMove();
        await PlayAnimation();
        await ResetCameraPos();
        FlowManager.Inst.NextFlow();
    }

    public override void UpdateFlow() { }

    // Start is called before the first frame update
    void Start() { }

    private async UniTask WaitMove()
    {
        if (controller != null)
        {
            controller.enabled = false;
            Camera.main.transform.DOMove(movePoint.position, 2);
            Camera.main.transform.DORotate(movePoint.eulerAngles + new Vector3(45, 0, 0), 2);
            await UniTask.Delay(2000);
        }
    }

    private async UniTask ResetCameraPos()
    {
        if (controller != null)
        {
            Camera.main.transform.DOLocalMove(Vector3.zero, 2);
            Camera.main.transform.DOLocalRotate(Vector3.zero, 2);
            await UniTask.Delay(2000);
            controller.enabled = true;
        }
    }

    private async UniTask PlayAnimation()
    {
        animation.AddClip(clip, clip.name);
        animation.clip = clip;
        animation.Play(clip.name);
        await UniTask.Delay((int)(animatorTime * 1000));
        animation.Stop();
    }
}
