/*
 * 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.db.core.model.AncestryData;

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

/**
 * A swing component that will draw a point in an ancestry diagram.
 */
public class AncestryDiagramPoint extends JButton {

    private final AncestryData data;
    private boolean extinct = false;

    /**
     * Create a new {@link AncestryDiagramPoint} object for drawing the given data.
     *
     * @param data The data to draw
     */
    public AncestryDiagramPoint(AncestryData data) {
        this.data = data;

        setText(data.getGeneration());
    }

    /**
     * The data represented by this UI object.
     *
     * @return Represented data
     */
    public AncestryData getData() {
        return data;
    }

    /**
     * Calculates the square dimension based on the larger value of the width and height
     * of the given dimension. Returns a Dimension object with equal width and height.
     *
     * @param d The original dimension from which the square size will be calculated
     * @return A new Dimension object with equal width and height
     */
    private Dimension getSquareSize(Dimension d) {
        int size = Math.max(d.height, d.width);

        return new Dimension(size, size);
    }

    /**
     * Sets the extinct status of this point and adjusts its visual properties accordingly.
     * When the point is set to extinct, its background color becomes semi-transparent
     * and the component is no longer opaque. When not extinct, the background color is fully
     * opaque and the component remains opaque.
     *
     * @param extinct A boolean value indicating whether the point should be marked as extinct
     */
    public void setExtinct(boolean extinct) {
        this.extinct = extinct;

        Color backgroundColor = getBackground();
        backgroundColor = new Color(backgroundColor.getRed(), backgroundColor.getGreen(), backgroundColor.getBlue(),
                extinct ? 100 : 255);

        setOpaque(!extinct);
        setBackground(backgroundColor);
    }

    /**
     * Returns the current extinct status of this point.
     *
     * @return A boolean value indicating whether the point is currently marked as extinct
     */
    public boolean isExtinct() {
        return extinct;
    }

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

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

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