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

public partial class MusicVisualizerCanvas : CanvasLayer
{
    private Label _nameLabel;
    private GTween _tween;

    public override void _Ready()
    {
        _nameLabel = GetNode<Label>("Title");
        _nameLabel.Hide();
    }

    private void ResetLabel(string trackName, string authorName)
    {
        _nameLabel.Text = string.IsNullOrWhiteSpace(authorName) ? trackName : $"{trackName} ({authorName})";
        _nameLabel.SetAnchorsAndOffsetsPreset(Control.LayoutPreset.BottomRight, margin: 0);
        _nameLabel.Modulate = Colors.White;
        _nameLabel.Hide();
    }

    public void ShowName(string trackName, string authorName)
    {
        _tween?.Kill();
        //_tween?.Complete();
        ResetLabel(trackName, authorName);
        _nameLabel.Show();

        _tween = GTweenSequenceBuilder.New()
            .Append(_nameLabel.TweenPositionX(_nameLabel.Position.X + 32, 0f)) // Add offset
            .Join(_nameLabel.TweenModulateAlpha(0, 0f)) // Invisibilify
            .Append(_nameLabel.TweenPositionX(_nameLabel.Position.X - 32, 1f)) // Animate back
            .Join(_nameLabel.TweenModulateAlpha(1, 0.5f))
            .AppendTime(3f) // Wait before hiding
            .Append(_nameLabel.TweenPositionX(_nameLabel.Position.X + 32, 1f)) // Start moving and hiding
            .Join(_nameLabel.TweenModulateAlpha(0, 0.5f))
            .Append(_nameLabel.TweenPositionX(_nameLabel.Position.X - 32, 0f)) // Move back to default position
            .AppendCallback(() => _nameLabel.Hide())
            .Build();

        _tween.SetEasing(Easing.OutCubic);
        _tween.Play();
    }
}
