/*
 * Copyright (C) 2025-2026  Tim Zimmermann <tim@aglais.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.aglais.zms.ui.util;

import java.awt.*;

/**
 * Utility class for drawing text.
 */
public class TextUtil {

    /**
     * Get the font size that fills most of the given space.
     *
     * @param g      Graphics object for measuring
     * @param base   The base font to use for resizing
     * @param width  The width of the textbox
     * @param height The height of the textbox
     * @param circle True if the textbox is circle shaped
     * @param text   The text to draw
     * @return The optimal font size for the given textbox
     */
    public static Font getBestFontSize(Graphics g, Font base, int width, int height, boolean circle, String... text) {
        int maxTextBoxWidth = circle ? (int) (width * 0.7071) : width; // √2/2
        int maxTextBoxHeight = circle ? (int) (height * 0.7071) : height;
        int fontSize = Math.max(width, height);
        Font font;
        FontMetrics metrics;
        int textWidth, textHeight;

        do {
            font = base.deriveFont((float) fontSize);
            g.setFont(font);

            metrics = g.getFontMetrics();
            textWidth = 0;
            for (String line : text) {
                textWidth = metrics.stringWidth(line);
            }
            textHeight = text.length * (metrics.getAscent() + metrics.getDescent());
            fontSize--;
        } while ((textWidth > maxTextBoxWidth || textHeight > maxTextBoxHeight) && fontSize > 0);

        return font;
    }

    /**
     * Draw a horizontally and vertically centered string.
     *
     * @param g       Graphics object for drawing
     * @param width   The width of the textbox
     * @param height  The height of the textbox
     * @param baseX   Textbox base position X
     * @param baseY   Textbox base position Y
     * @param centerX True to center horizontally too
     * @param text    The text to draw
     * @return Position where the drawn text ends, absolute coordinates
     */
    public static Point drawStringCentered(Graphics g, int width, int height, int baseX, int baseY, boolean centerX,
                                           String... text) {
        FontMetrics metrics = g.getFontMetrics(g.getFont());
        int lineHeight = metrics.getAscent() + metrics.getDescent();
        int textHeight = text.length * lineHeight;

        int y = (height - textHeight) / 2 + metrics.getAscent();
        int lastX = 0;

        for (String line : text) {
            int textWidth = metrics.stringWidth(line);
            int x = centerX ? (width - textWidth) / 2 : 0;

            g.drawString(line, baseX + x, baseY + y);

            y += lineHeight;
            lastX = x + textWidth;
        }
        if (text.length > 0)
            y -= lineHeight; // Line height is added one more time than needed

        return new Point(baseX + lastX, baseY + y);
    }

    /**
     * Draw a horizontally and vertically centered string.
     *
     * @param g      Graphics object for drawing
     * @param width  The width of the textbox
     * @param height The height of the textbox
     * @param text   The text to draw
     * @return Position where the drawn text ends, absolute coordinates
     */
    public static Point drawStringCentered(Graphics g, int width, int height, String... text) {
        return drawStringCentered(g, width, height, 0, 0, true, text);
    }
}
