/*
 * 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 javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * The ImagePanel class is a custom Swing component that extends {@link JPanel}
 * and is designed to display a {@link BufferedImage}. The image can be set
 * and rendered within the component, resizing to match the dimensions
 * of the component.
 */
public class ImagePanel extends JPanel {

    private BufferedImage image;

    /**
     * Constructs a new ImagePanel object with the specified {@link BufferedImage}.
     *
     * @param image The image to be displayed by this ImagePanel component
     */
    public ImagePanel(BufferedImage image) {
        this.image = image;
    }

    /**
     * Constructs a new ImagePanel object with no initial image.
     * This will create an ImagePanel component with a null {@link BufferedImage}.
     */
    public ImagePanel() {
        this(null);
    }

    /**
     * Sets the {@link BufferedImage} to be displayed by this ImagePanel component.
     * This method updates the current image and triggers a repaint of the component
     * to render the new image.
     *
     * @param image The new {@link BufferedImage} to set and display.
     *              Passing null will clear the current image.
     */
    public void setImage(BufferedImage image) {
        this.image = image;

        repaint();
    }

    /**
     * Get the currently displayed {@link BufferedImage}
     *
     * @return Currently displayed image
     */
    public BufferedImage getImage() {
        return image;
    }

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

        if (image != null) {
            float widthRatio = (float) getWidth() / image.getWidth();
            float heightRatio = (float) getHeight() / image.getHeight();
            float scale = Math.min(widthRatio, heightRatio);

            int scaledWidth = (int) (image.getWidth() * scale);
            int scaledHeight = (int) (image.getHeight() * scale);
            int x = (getWidth() - scaledWidth) / 2;
            int y = (getHeight() - scaledHeight) / 2;
            Graphics2D g2 = (Graphics2D) g;

            g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2.drawImage(image, x, y, x + scaledWidth, y + scaledHeight, 0, 0,
                    image.getWidth(), image.getHeight(), null);
        }
    }
}
