/*
 * Copyright (c) 2024. Elex. All Rights Reesrved.
 * https://www.elex-project.com/
 */

package com.elex_project.examples.dialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class DialogExample extends JPanel {
	private JFrame jFrame;

	public DialogExample(JFrame jFrame) {
		super(new FlowLayout());
		this.jFrame = jFrame;

		final JButton button1 = new JButton("Message Dialog");
		final JButton button2 = new JButton("Option Dialog");
		final JButton button3 = new JButton("Option Dialog 2");

		// 레이아웃에 추가
		add(button1);
		add(button2);
		add(button3);

		// 액션 리스너를 통해서 클릭 이벤트를 받는다.
		final ActionListener actionListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				JButton button = (JButton) actionEvent.getSource();
				if (button == button1) {
					JOptionPane
							.showMessageDialog(DialogExample.this, "Hello");
				} else if (button == button2) {
					final String[] options = {"A", "B", "C", "D"};
					int response = JOptionPane.showOptionDialog(DialogExample.this, "Make a decision!", "Pick one",
							JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE, null,
							options, options[0]);
					System.out.println(options[response]);
				} else if (button == button3) {
					final JOptionPane optionPane = new JOptionPane("Hello~",
							JOptionPane.INFORMATION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION);
					final JDialog jDialog = new JDialog(jFrame, "Hello", true);
					jDialog.setContentPane(optionPane);
					jDialog.pack();
					jDialog.setVisible(true);
					//jDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
					optionPane.addPropertyChangeListener(new PropertyChangeListener() {
						@Override
						public void propertyChange(PropertyChangeEvent propertyChangeEvent) {

						}
					});
					System.out.println(optionPane.getValue());
				}
			}
		};
		button1.addActionListener(actionListener);
		button2.addActionListener(actionListener);
		button3.addActionListener(actionListener);
	}

	public static void main(String... args) {

		final JFrame jFrame = new JFrame();
		jFrame.setTitle("Example");
		jFrame.setSize(800, 600);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setContentPane(new DialogExample(jFrame));

		SwingUtilities.invokeLater(() -> {
			jFrame.setVisible(true);
		});


	}

}
