using Godot;
using System;
using DungeonRPG.Scripts.General;

public partial class PlayerDashState : PlayerState
{

    private Timer timer;

    [Export(PropertyHint.Range, "0, 20,0.1")] private float speed = 10;
    public override void _Ready()
    {
        base._Ready();
        timer = GetNode<Timer>("Timer");
        timer.Timeout += HandleDashTimeout;
    }
    
    public override void _PhysicsProcess(double delta)
    {
        characterNode.MoveAndSlide();
    }

    protected override void EnterState()
    {
        characterNode.AnimatedSprite.Play(GameConstants.ANIM_DASH);
        characterNode.Velocity = new Vector3(characterNode.Direction.X, 0, characterNode.Direction.Y);

        if (characterNode.Velocity == Vector3.Zero)
        {
            characterNode.Velocity = characterNode.AnimatedSprite.FlipH ? Vector3.Left : Vector3.Right;
        }
            
        characterNode.Velocity *= speed;
        timer.Start();
    }

    private void HandleDashTimeout()
    {
        characterNode.StateMachine.SwitchState<PlayerIdleState>();
        characterNode.Velocity = Vector3.Zero;
    }
}