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

import org.aglais.zms.AglaisZMS;
import org.aglais.zms.ui.LicenseDialog;
import org.aglais.zms.ui.component.ImagePanel;
import org.aglais.zms.i18n.InterpolatingResourceBundle;
import org.aglais.zms.util.ResourceUtil;
import org.aglais.zms.util.VersionUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.ResourceBundle;

/**
 * The AboutTab class represents the tab in the application that displays information
 * about the software, including version and Git branch information. It extends the
 * Tab class and provides specific implementations for displaying content related to
 * the application's details and versioning.
 */
public class AboutTab extends Tab {

    private static final int COPYRIGHT_START = 2025;

    private JPanel contentPane;
    private ImagePanel logoPanel;
    private JLabel versionInfoLabel;
    private JLabel branchInfoLabel;
    private JLabel runtimeVersionLabel;
    private JLabel licenseLabel;
    private JLabel licenseLinkLabel;

    /**
     * Creates a new about tab
     *
     * @param zms The application instance
     */
    public AboutTab(AglaisZMS zms) {
        super(zms);

        ResourceBundle uiBundle = ResourceBundle.getBundle("ui");

        licenseLinkLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
        licenseLinkLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                LicenseDialog licenseDialog = new LicenseDialog();

                licenseDialog.setIconImages(zms.getIconImages());
                licenseDialog.setTitle(uiBundle.getString("license"));
                licenseDialog.show(contentPane);
            }
        });
    }

    @Override
    public void onShow() {
        if (logoPanel.getImage() == null)
            logoPanel.setImage(ResourceUtil.getImage(ResourceUtil.APP_ICON_NAME + "." + ResourceUtil.APP_ICON_EXTENSION));

        InterpolatingResourceBundle uiBundle = (InterpolatingResourceBundle) ResourceBundle.getBundle("ui");
        String version = VersionUtil.getVersion();
        String gitBranch = VersionUtil.getGitBranch();

        versionInfoLabel.setText(uiBundle.getString("version_info", version));
        branchInfoLabel.setText(uiBundle.getString("git_branch_info", gitBranch));
        runtimeVersionLabel.setText(uiBundle.getString("java_version_info", Runtime.version()));

        int year = Calendar.getInstance().get(Calendar.YEAR);
        licenseLabel.setText(uiBundle.getString("copyright_info",
                year > COPYRIGHT_START ? COPYRIGHT_START + "-" + year : year));
        licenseLinkLabel.setText("<html><a href=\"license\">" + uiBundle.getString("copyright_license_info") + "</a>");
    }

    @Override
    public @NotNull JComponent getContentPane() {
        return contentPane;
    }
}