/*
 * 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;

import org.aglais.zms.util.Log;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * A dialog for displaying the program's license text.
 */
public class LicenseDialog extends JDialog {

    private static final String LOG_TAG = LicenseDialog.class.getSimpleName();
    private static final String LICENSE_PATH = "/LICENSE";

    private JPanel contentPane;
    private JButton closeButton;
    private JTextArea licenseTextArea;
    private JScrollPane licenseScrollPane;

    /**
     * Create a new {@link LicenseDialog}.
     * The license text will be read from /LICENSE in the current classpath.
     */
    public LicenseDialog() {
        setContentPane(contentPane);
        setModal(true);

        closeButton.addActionListener(e -> onCancel());

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        licenseTextArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, licenseTextArea.getFont().getSize()));
        try (InputStream in = LicenseDialog.class.getResourceAsStream(LICENSE_PATH)) {
            if (in != null) {
                try (InputStreamReader reader = new InputStreamReader(in)) {
                    licenseTextArea.read(reader, null);
                } catch (IOException e) {
                    Log.e(LOG_TAG, "Failed to read license", e);
                }
            } else {
                Log.e(LOG_TAG, "License not found");
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Failed to open license", e);
        }
    }

    /**
     * Handles the Cancel button click by disposing the dialog
     */
    private void onCancel() {
        dispose();
    }

    /**
     * Shows the dialog relative to the given parent component, using half the height of the parent.
     *
     * @param parent Parent component
     */
    public void show(JComponent parent) {
        pack();
        setSize(getWidth(), parent.getHeight() / 2);
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentShown(ComponentEvent e) {
                setSize(getWidth() + licenseScrollPane.getVerticalScrollBar().getWidth(), getHeight());
            }
        });
        setLocationRelativeTo(parent);
        setVisible(true);
    }
}
