using System.Threading;
using System.Threading.Tasks;
using Godot;
using GTweens.Builders;
using GTweens.Easings;
using GTweens.Tweens;
using GTweensGodot.Extensions;

namespace Cirno.Scripts.Activables;

public partial class PlayerMover : ChainActivable
{
    
    private GameManager _gameManager;

    [Export]
    public Vector2 RelativeTargetPosition = Vector2.Zero;

    [Export]
    public float MovementTime = 1f;

    [Export]
    public Tween.EaseType EaseType = Tween.EaseType.InOut;
    
    [Export]
    public Tween.TransitionType TransitionType { get; private set; } = Tween.TransitionType.Linear;

    [Export] public GTweens.Easings.Easing GTweenEasing { get; private set; } = Easing.Linear;
    
    public override void _Ready()
    {
        base._Ready();
        
        _gameManager = this.GetGameManager();
    }

    public override bool Activate(ActivationType activationType = ActivationType.Toggle)
    {
        if (_gameManager.Player is null) return false;

        _ = MovePlayer();
        
        return true;
    }

    private async Task MovePlayer()
    {
        //_gameManager.Player.RequestMovementDisable(true);
        _gameManager.Player.SetState(PlayerState.Cutscene);

        var gtween = GTweenSequenceBuilder.New()
            .Append(_gameManager.Player.MainObject.TweenGlobalPosition(
                    _gameManager.Player.MainObject.GlobalPosition + RelativeTargetPosition, MovementTime)
                .SetEasing(GTweenEasing)
                //.
            )
            .Build();
        
        await gtween.PlayAsync(CancellationToken.None);
            
        
        // Tween tween = GetTree().CreateTween();
        // tween.SetEase(EaseType);
        // tween.SetTrans(TransitionType);
        // tween.TweenProperty(_gameManager.Player, "global_position", _gameManager.Player.GlobalPosition + RelativeTargetPosition, MovementTime);
        //
        // // Wait for the tween to finish
        // await ToSignal(tween, "finished");
        
        //_gameManager.Player.RequestMovementDisable(false);
        _gameManager.Player.SetState(PlayerState.Cutscene);
        
        ActivateTargets();
    }
    

}