package cz.kamma.kfmanager.ui;

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

/**
 * Dialog pro výběr fontu
 */
public class FontChooserDialog extends JDialog {
    private Font selectedFont;
    private boolean approved = false;
    
    private JList<String> fontList;
    private JList<Integer> sizeList;
    private JList<String> styleList;
    private JTextArea previewArea;
    
    public FontChooserDialog(Window parent, Font initialFont) {
        super(parent, "Výběr fontu", ModalityType.APPLICATION_MODAL);
        this.selectedFont = initialFont;
        
        initComponents();
        selectFont(initialFont);
        
        setSize(600, 400);
        setLocationRelativeTo(parent);
    }
    
    private void initComponents() {
        setLayout(new BorderLayout(10, 10));
        
    // Panel pro výběr (font, velikost, styl)
    JPanel selectionPanel = new JPanel(new GridLayout(1, 3, 10, 0));
        
        // Seznam fontů
        JPanel fontPanel = new JPanel(new BorderLayout());
        fontPanel.add(new JLabel("Font:"), BorderLayout.NORTH);
        
        String[] availableFonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
                .getAvailableFontFamilyNames();
        fontList = new JList<>(availableFonts);
        fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        fontList.addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                updatePreview();
            }
        });
        
        JScrollPane fontScroll = new JScrollPane(fontList);
        fontPanel.add(fontScroll, BorderLayout.CENTER);
        
        // Seznam velikostí
        JPanel sizePanel = new JPanel(new BorderLayout());
        sizePanel.add(new JLabel("Velikost:"), BorderLayout.NORTH);
        
        Integer[] sizes = {8, 9, 10, 11, 12, 13, 14, 16, 18, 20, 22, 24, 26, 28, 32, 36, 40, 48, 56, 64, 72};
        sizeList = new JList<>(sizes);
        sizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        sizeList.addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                updatePreview();
            }
        });
        
        JScrollPane sizeScroll = new JScrollPane(sizeList);
        sizePanel.add(sizeScroll, BorderLayout.CENTER);

        // Seznam stylů (Plain, Bold, Italic, Bold Italic)
        JPanel stylePanel = new JPanel(new BorderLayout());
        stylePanel.add(new JLabel("Styl:"), BorderLayout.NORTH);

        String[] styles = {"Plain", "Bold", "Italic", "Bold Italic"};
        styleList = new JList<>(styles);
        styleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        styleList.addListSelectionListener(e -> {
            if (!e.getValueIsAdjusting()) {
                updatePreview();
            }
        });
        JScrollPane styleScroll = new JScrollPane(styleList);
        stylePanel.add(styleScroll, BorderLayout.CENTER);
        
    selectionPanel.add(fontPanel);
    selectionPanel.add(sizePanel);
    selectionPanel.add(stylePanel);
        
        // Preview oblast
        JPanel previewPanel = new JPanel(new BorderLayout());
        previewPanel.setBorder(BorderFactory.createTitledBorder("Náhled"));
        
        previewArea = new JTextArea();
        previewArea.setText("AaBbCcDdEeFfGgHhIiJjKkLl\n0123456789\nThe quick brown fox jumps over the lazy dog");
        previewArea.setEditable(false);
        previewArea.setLineWrap(true);
        previewArea.setWrapStyleWord(true);
        
        JScrollPane previewScroll = new JScrollPane(previewArea);
        previewScroll.setPreferredSize(new Dimension(0, 120));
        previewPanel.add(previewScroll, BorderLayout.CENTER);
        
        // Tlačítka
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        
        JButton okButton = new JButton("OK");
        okButton.addActionListener(e -> {
            approved = true;
            dispose();
        });
        
        JButton cancelButton = new JButton("Zrušit");
        cancelButton.addActionListener(e -> {
            approved = false;
            dispose();
        });
        
        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);
        
        // Layout
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        mainPanel.add(selectionPanel, BorderLayout.CENTER);
        mainPanel.add(previewPanel, BorderLayout.SOUTH);
        
        add(mainPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }
    
    private void selectFont(Font font) {
        // Najít a vybrat font
        fontList.setSelectedValue(font.getName(), true);
        
        // Najít a vybrat velikost
        sizeList.setSelectedValue(font.getSize(), true);

        // Najít a vybrat styl
        int style = font.getStyle();
        switch (style) {
            case Font.BOLD:
                styleList.setSelectedValue("Bold", true);
                break;
            case Font.ITALIC:
                styleList.setSelectedValue("Italic", true);
                break;
            case Font.BOLD | Font.ITALIC:
                styleList.setSelectedValue("Bold Italic", true);
                break;
            default:
                styleList.setSelectedValue("Plain", true);
                break;
        }

        updatePreview();
    }
    
    private void updatePreview() {
        String fontName = fontList.getSelectedValue();
        Integer fontSize = sizeList.getSelectedValue();
        String styleName = styleList.getSelectedValue();

        if (fontName != null && fontSize != null && styleName != null) {
            int style = Font.PLAIN;
            if ("Bold".equals(styleName)) style = Font.BOLD;
            else if ("Italic".equals(styleName)) style = Font.ITALIC;
            else if ("Bold Italic".equals(styleName)) style = Font.BOLD | Font.ITALIC;

            selectedFont = new Font(fontName, style, fontSize);
            previewArea.setFont(selectedFont);
        }
    }
    
    public Font getSelectedFont() {
        return selectedFont;
    }
    
    public boolean isApproved() {
        return approved;
    }
    
    /**
     * Zobrazí dialog a vrátí vybraný font, nebo null pokud byl zrušen
     */
    public static Font showDialog(Window parent, Font initialFont) {
        FontChooserDialog dialog = new FontChooserDialog(parent, initialFont);
        dialog.setVisible(true);
        
        if (dialog.isApproved()) {
            return dialog.getSelectedFont();
        }
        return null;
    }
}
