package galerie.view;

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

public class ConfirmOnCloseDialog extends JDialog {

    public interface ResultHandler { void onYes(); void onNo(); }

    public ConfirmOnCloseDialog(JFrame parent, ResultHandler handler) {
        super(parent, "Quitter l'application ?", true);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        JPanel content = new JPanel(new BorderLayout(10,10));
        content.setBorder(BorderFactory.createEmptyBorder(12,12,12,12));
        JLabel msg = new JLabel("Voulez-vous vraiment quitter ?", SwingConstants.CENTER);

        JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton yes = new JButton("Oui");
        JButton no  = new JButton("Non");
        buttons.add(yes);
        buttons.add(no);

        yes.addActionListener(e -> { handler.onYes(); dispose(); });
        no.addActionListener(e -> { handler.onNo();  dispose(); });

        content.add(msg, BorderLayout.CENTER);
        content.add(buttons, BorderLayout.SOUTH);
        setContentPane(content);
        pack();
        setLocationRelativeTo(parent);
    }
}
