package de.duehl.vocabulary.japanese.ui.dialog.kana.hiraganatest.components;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Point;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import de.duehl.swing.ui.GuiTools;
import de.duehl.swing.ui.dialogs.base.ModalDialogBase;
import de.duehl.swing.ui.layout.VerticalLayout;
import de.duehl.vocabulary.japanese.data.FumikoDataStructures;
import de.duehl.vocabulary.japanese.data.symbol.Hiragana;
import de.duehl.vocabulary.japanese.ui.data.FumikoUiObjects;

/**
 * Diese Klasse zeigt die Details eines Hiragana inklusive der internen Daten des Benutzers an.
 *
 * @version 1.01     2026-08-21
 * @author Christian Dühl
 */

public class HiraganaDetailDialog extends ModalDialogBase {

    private static final int MIN_WIDTH = 700;


    /** Das UI-Komponente, auf der die Daten des Hiragana dargestellt werden. */
    private final HiraganaPanel hiraganaPanel;

    /** Der Button zum Beenden. */
    private final JButton okButton;

    /**
     * Konstruktor.
     *
     * @param hiragana
     *            Das anzuzeigende Hiragana.
     * @param dataStructures
     *            Die Datenstrukturen des Vokabeltrainers.
     * @param uiObjects
     *            Die häufig verwendeten Funktionen der grafischen Oberfläche des Vokabeltrainers.
     * @param parentLocation
     *            Position des Rahmens der Oberfläche, vor der dieser Dialog erzeugt wird.
     * @param programImage
     *            Anzuzeigendes ProgrammIcon.
     */
    public HiraganaDetailDialog(Hiragana hiragana, FumikoDataStructures dataStructures,
            FumikoUiObjects uiObjects, Point parentLocation, Image programImage) {
        super(parentLocation, programImage, "Details des zu " + hiragana.getCharacter());
        addEscapeBehaviour();

        hiraganaPanel = new HiraganaPanel(dataStructures, uiObjects);
        hiraganaPanel.showHiraganaAsViewerToo();
        hiraganaPanel.showInternalHiraganaDataToo();
        hiraganaPanel.createGui();
        hiraganaPanel.showHiragana(hiragana, getLocation());

        okButton = new JButton("Beenden");

        init();
        fillDialog();
    }

    private void init() {
        okButton.addActionListener(e -> closeDialog());
    }

    @Override
    protected void populateDialog() {
        add(createDummyWidthLabel(), BorderLayout.NORTH);
        add(createCenterPart(), BorderLayout.CENTER);
        add(createButtonsPart(),  BorderLayout.SOUTH);

        SwingUtilities.invokeLater(() -> okButton.requestFocus());
    }

    private Component createDummyWidthLabel() {
        JLabel label = new JLabel("");
        label.setPreferredSize(new Dimension(MIN_WIDTH, 0));
        return label;
    }

    private Component createCenterPart() {
        JPanel panel = new JPanel();
        panel.setLayout(new VerticalLayout(0, VerticalLayout.BOTH));
        GuiTools.createTitle(panel);

        panel.add(hiraganaPanel.getPanel());

        return panel;
    }

    private Component createButtonsPart() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());

        panel.add(okButton, BorderLayout.EAST);

        return panel;
    }

}
