using Godot;
using Newlon.Components.Level;


namespace Newlon;

public partial class Sun : Area2D
{
    public static Control Counter;

    [Export] public int amount = 25;
    [Export] private Timer _deathTimer;
    [Export] private AnimationPlayer _rotation;
    [Export] private AnimationPlayer _fade;

    private bool scoring;

    public override void _Ready()
    {
        _rotation.SpeedScale = 1.0f + GD.Randf() / 2.0f;
    }
    public override void _InputEvent(Viewport viewport, InputEvent @event, int shapeIdx)
    {
        if (scoring) return;

        if (@event.IsActionPressed("primary_action"))
        {

            _fade.Stop();
            scoring = true;

            GetNode<ChannelPlayer>("SunPlayer").Play();
            var tween = CreateTween();
            tween.TweenInterval(0.1);
            tween.TweenProperty(this, "global_position", GetCanvasTransform().AffineInverse() * (Counter.GlobalPosition + Counter.PivotOffset), 0.5).SetTrans(Tween.TransitionType.Cubic).SetEase(Tween.EaseType.Out);
            tween.TweenCallback(Callable.From(() =>
            {
                RuntimeLevelData.Instance.AddSun(amount);
                QueueFree();
            }));
        }
    }

    public override void _Process(double delta)
    {
        if (scoring) return;
        if (_deathTimer.TimeLeft / _deathTimer.WaitTime <= 0.25)
        {
            _fade.Play("main");
        }
    }
}
