/*
 * Copyright (C) 2025  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.component;

import org.aglais.zms.ui.util.TextUtil;

import javax.swing.*;
import java.awt.*;

/**
 * A swing component that will draw a status circle in IUCN status diagram.
 */
public class IUCNStatusCircle extends JPanel {

    private final IUCNStatus.Status status;
    private boolean active;

    /**
     * Create a new {@link IUCNStatusCircle} object for drawing the given status.
     *
     * @param status The IUCN status to draw
     * @param active True if this status is active for the given diagram
     */
    public IUCNStatusCircle(IUCNStatus.Status status, boolean active) {
        this.status = status;
        this.active = active;
    }

    /**
     * Get the status represented by this circle
     *
     * @return The status this circle represents
     */
    public IUCNStatus.Status getStatus() {
        return status;
    }

    /**
     * Check if this circle is marked as active
     *
     * @return True if active
     */
    public boolean isActive() {
        return active;
    }

    /**
     * Set this circle's status
     *
     * @param active True to mark as active
     */
    public void setActive(boolean active) {
        this.active = active;

        repaint();
    }

    private Dimension getSquareSize(Dimension d) {
        int size = Math.max(d.height, d.width);

        return new Dimension(size, size);
    }

    @Override
    public Dimension getPreferredSize() {
        return getSquareSize(super.getPreferredSize());
    }

    @Override
    public Dimension getMinimumSize() {
        return getSquareSize(super.getMinimumSize());
    }

    @Override
    public Dimension getMaximumSize() {
        return getSquareSize(super.getMaximumSize());
    }

    /**
     * Get the circle size
     *
     * @return Circle size
     */
    public int getCircleSize() {
        return Math.min(getWidth(), getHeight());
    }

    @Override
    protected void paintBorder(Graphics g) {
        super.paintBorder(g);

        Graphics2D g2 = (Graphics2D) g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int size = getCircleSize();

        g.setColor(UIManager.getColor("Label.foreground"));
        g.drawOval(0, 0, size - 1, size - 1);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int size = getCircleSize();
        String text = status.name();

        // Set colors
        Color background = active ? status.getBackground() : UIManager.getColor("Panel.background");
        Color textColor = active ? status.getTextColor() : UIManager.getColor("Label.foreground");

        // Fill background
        g.setColor(background);
        g.fillOval(0, 0, size - 1, size - 1);

        // Draw text
        g.setFont(TextUtil.getBestFontSize(g, g.getFont(), size, size, true, text));
        g.setColor(textColor);

        TextUtil.drawStringCentered(g, size, size, text);
    }
}
