using System.Collections.Generic;
using System.Linq;
using Cirno.Scripts.Components.Actors;
using Godot;

public partial class ActorFreeMovement : MovementHandler
{
    public override Vector2 FacingDirection
    {
        get => _parent.FacingDirection;
        set => _parent.FacingDirection = value;
    }

    public override Vector2 MovementDirection
    {
        get => _parent.MovementDirection;
        set => _parent.MovementDirection = value;
    }
    
    [Export] public StringName StrafeAction { get; private set; } = "strafe";
    
    public bool IsDestroyed => _parent.IsDestroyed;

    public override void Init(Actor parent) 
    {
        base.Init(parent);
       
        MovementDirection = Vector2.Zero;
        FacingDirection = Vector2.Down;
    }

    public override void Update(double delta)
    {
        
    }

    public override void PhysicsUpdate(double delta)
    {
        if (IsDestroyed) return;
        
        MovementDirection = AggregateInputProviders().Normalized();

        var aimingDirection = GetAimingDirection().Normalized();
        var isStrafing = GetActionPressed(StrafeAction);

        if (!isStrafing && aimingDirection.Length() > 0.1f)
        {
            FacingDirection = aimingDirection;
        }
        else if (MovementDirection != Vector2.Zero)
        {
            FacingDirection = MovementDirection;
        }
        
        _parent.Velocity = MovementDirection * _parent.MovementSpeed;

        _parent.MoveAndSlide();
    }

}