package se.urmo.game.entities;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;

public final class ScorePopup {
    private final double startX, startY;    // world px (not screen)
    private final String text;
    private final long startNs;
    private final long lifeNs;               // e.g. 1_000_000_000L (1s)

    // simple motion params
    private final double risePixels;         // e.g. 16 px total rise

    public ScorePopup(double x, double y, String text, long lifeNs, double risePixels) {
        this.startX = x;
        this.startY = y;
        this.text = text;
        this.lifeNs = lifeNs;
        this.risePixels = risePixels;
        this.startNs = System.nanoTime();
    }

    public boolean isAlive() {
        return (System.nanoTime() - startNs) < lifeNs;
    }

    public void draw(Graphics2D g, int offsetX, int offsetY, Font font) {
        long dt = System.nanoTime() - startNs;
        double t = Math.min(1.0, dt / (double) lifeNs);  // 0..1

        // motion: ease-out upward (quadratic)
        double y = startY - (risePixels * (1 - (1 - t) * (1 - t)));

        // fade: alpha 1 → 0
        float alpha = (float) (1.0 - t);

        var oldComp = g.getComposite();
        var oldFont = g.getFont();
        g.setFont(font);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));

        // draw centered on tile center
        var fm = g.getFontMetrics();
        int sx = offsetX + (int) Math.round(startX);
        int sy = offsetY + (int) Math.round(y);
        int x = sx - fm.stringWidth(text) / 2;
        int baseline = sy; // tune if you want it a tad above the exact point

//        g.setColor(Color.WHITE);
        g.setColor(Color.BLACK);
        g.drawString(text, x + 1, baseline + 1);
        g.setColor(Color.WHITE);
        g.drawString(text, x, baseline);
        g.drawString(text, x, baseline);

        g.setComposite(oldComp);
        g.setFont(oldFont);
    }
}
