package projetMusee.ui;

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

public class MainWindow {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(MainWindow::createAndShowGUI);
    }

    private static void createAndShowGUI() {
        // Création de la fenêtre principale
        JFrame frame = new JFrame("Portail des Musées Français");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setLocationRelativeTo(null);
        frame.setResizable(true);

        // Panel principal avec image de fond
        JPanel mainPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Image background = new ImageIcon("data/musee.jpg").getImage(); // Vérifier le chemin de l’image
                g.drawImage(background, 0, 0, getWidth(), getHeight(), this);
            }
        };
        mainPanel.setLayout(new BorderLayout(20, 40));

        // Panel du haut
        JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 20));
        headerPanel.setOpaque(false);

        // Bouton guide utilisateur
        JButton manualButton = new JButton("Guide utilisateur");
        // manualButton.setIcon(createDownloadIcon());
        stylizeButton(manualButton, new Color(30, 144, 255), 16, true);
        headerPanel.add(manualButton);

        // Contenu central
        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        contentPanel.setOpaque(false);

        JPanel titlePanel = createTextPanel("Bienvenue sur France Musées", 40);
        titlePanel.setMaximumSize(new Dimension(800, 100));

        JPanel descPanel = createTextPanel(
                "Découvrez les collections françaises à travers une interface interactive et des outils de visualisation modernes.",
                18);
        descPanel.setMaximumSize(new Dimension(1200, 150));

        // Bouton commencer
        JButton startButton = new JButton("COMMENCER");
        // startButton.setIcon(createArrowIcon());
        stylizeButton(startButton, new Color(34, 139, 34), 24, false);
        startButton.setPreferredSize(new Dimension(400, 80));
        startButton.setAlignmentX(Component.CENTER_ALIGNMENT);

        contentPanel.add(Box.createVerticalGlue());
        contentPanel.add(titlePanel);
        contentPanel.add(descPanel);
        contentPanel.add(Box.createVerticalStrut(60));
        contentPanel.add(startButton);
        contentPanel.add(Box.createVerticalGlue());

        mainPanel.add(headerPanel, BorderLayout.NORTH);
        mainPanel.add(contentPanel, BorderLayout.CENTER);

        frame.setContentPane(mainPanel);
        frame.setVisible(true);

        // Action du bouton manuel
        manualButton.addActionListener(e -> {
            try {
                Desktop.getDesktop().open(new java.io.File("docs/Guide-Utilisateur.pdf"));
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(frame, "Erreur lors de l'ouverture du guide.", "Erreur",
                        JOptionPane.ERROR_MESSAGE);
            }
        });

        // Action du bouton commencer (sans fermer la fenêtre)
        startButton.addActionListener(e -> {
            try {
                SelectionPanel selectionPanel = new SelectionPanel();
                frame.setContentPane(selectionPanel); // Remplace le contenu actuel
                frame.revalidate();
                frame.repaint();
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(frame,
                        "Erreur : " + ex.getMessage(),
                        "Erreur",
                        JOptionPane.ERROR_MESSAGE);
            }
        });
    }

    private static JPanel createTextPanel(String text, int fontSize) {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBackground(new Color(255, 255, 255, 200));
        panel.setBorder(BorderFactory.createEmptyBorder(15, 30, 15, 30));

        JLabel label = new JLabel(text);
        label.setFont(new Font("Lora", Font.BOLD, fontSize));
        label.setForeground(new Color(25, 25, 112));
        label.setHorizontalAlignment(SwingConstants.CENTER);

        panel.add(label, BorderLayout.CENTER);
        return panel;
    }

    private static void stylizeButton(JButton button, Color color, int fontSize, boolean isDownload) {
        button.setFont(new Font("Lora", Font.BOLD, fontSize));
        button.setBackground(color);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Color.WHITE, 2),
                BorderFactory.createEmptyBorder(15, 30, 15, 30)));
        button.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        button.setIconTextGap(15);
        button.setHorizontalTextPosition(isDownload ? SwingConstants.LEFT : SwingConstants.RIGHT);
    }

}