package fr.monkhanny.dorfromantik.gui;

import fr.monkhanny.dorfromantik.Options;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonHoverAnimationListener implements ActionListener {

    private int step = 0;
    private final float scaleIncrement;
    private final boolean entering;
    private final JButton button;
    private final Color originalColor;
    private final Font originalFont;
    private float currentScale = 1.0f;

    public ButtonHoverAnimationListener(boolean entering, JButton button, Color originalColor, Font originalFont) {
        this.entering = entering;
        this.button = button;
        this.originalColor = originalColor;
        this.originalFont = originalFont;
        this.scaleIncrement = (Options.HOVER_FONT_SCALE - 1.0f) / Options.ANIMATION_STEPS;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        currentScale = entering ? (1.0f + step * scaleIncrement) : (Options.HOVER_FONT_SCALE - step * scaleIncrement);

        button.setForeground(entering ? Options.BUTTON_HOVER_COLOR : originalColor);
        button.setFont(originalFont.deriveFont(originalFont.getSize2D() * currentScale));

        step++;
        if (step >= Options.ANIMATION_STEPS) {
            ((Timer) e.getSource()).stop();
            if (!entering) {
                button.setFont(originalFont); // Restore the original font
            }
        }
    }
}
