package pl.alx.kjava.kalkulator;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;

import java.awt.Color;
import java.awt.Component;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Kalkulator {
	// private JFormattedTextField
	// java.text.NumberFormat - wersja ewentualnie do wypróbowania w domu; doczytać

	private JFrame frmKalkulatorJava;	
	private JTextField txtLewy;
	private JTextField txtPrawy;
	private JTextField txtWynik;
	private JComboBox<String> comboBox;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Kalkulator window = new Kalkulator();
					window.frmKalkulatorJava.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Kalkulator() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmKalkulatorJava = new JFrame();
		frmKalkulatorJava.setTitle("Kalkulator Java");
		frmKalkulatorJava.setBounds(100, 100, 958, 345);
		frmKalkulatorJava.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JLabel lblKalkulatorZeSzkolenia = new JLabel("Kalkulator ze szkolenia");
		lblKalkulatorZeSzkolenia.setFont(new Font("Dialog", Font.PLAIN, 16));
		lblKalkulatorZeSzkolenia.setHorizontalAlignment(SwingConstants.CENTER);
		
		txtLewy = new JTextField();
		txtLewy.setHorizontalAlignment(SwingConstants.RIGHT);
		txtLewy.setFont(new Font("Dialog", Font.BOLD, 36));
		
		txtPrawy = new JTextField();
		txtPrawy.setHorizontalAlignment(SwingConstants.RIGHT);
		txtPrawy.setFont(new Font("Dialog", Font.BOLD, 36));
		
		comboBox = new JComboBox<>();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				dzialaj();
			}
		});
		comboBox.setModel(new DefaultComboBoxModel<>(new String[] {"+", "-", "*", "/"}));
		comboBox.setFont(new Font("Dialog", Font.BOLD, 36));
		// wyśrodkowanie zawartości comboBox (za StackOverflow)
		((JLabel)comboBox.getRenderer()).setHorizontalAlignment(JLabel.CENTER);
				
		JButton btnOblicz = new JButton("Oblicz");
		btnOblicz.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				dzialaj();
			}
		});
		btnOblicz.setFont(new Font("Dialog", Font.BOLD, 36));
		
		txtWynik = new JTextField();
		txtWynik.setHorizontalAlignment(SwingConstants.RIGHT);
		txtWynik.setFont(new Font("Dialog", Font.BOLD, 38));
		txtWynik.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(frmKalkulatorJava.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblKalkulatorZeSzkolenia, GroupLayout.DEFAULT_SIZE, 926, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(btnOblicz, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(txtLewy, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE))
							.addGap(18)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(comboBox, 0, 130, Short.MAX_VALUE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(txtPrawy, GroupLayout.PREFERRED_SIZE, 334, GroupLayout.PREFERRED_SIZE))
								.addComponent(txtWynik, GroupLayout.DEFAULT_SIZE, 525, Short.MAX_VALUE))))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblKalkulatorZeSzkolenia)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtLewy)
						.addComponent(txtPrawy)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 52, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addComponent(txtWynik)
						.addComponent(btnOblicz, GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE))
					.addGap(121))
		);
		groupLayout.linkSize(SwingConstants.VERTICAL, new Component[] {txtLewy, txtPrawy, comboBox});
		groupLayout.linkSize(SwingConstants.HORIZONTAL, new Component[] {txtLewy, txtPrawy});
		frmKalkulatorJava.getContentPane().setLayout(groupLayout);
		
		// nasze
		DocumentListener dl = new DocumentListener() {
			public void removeUpdate(DocumentEvent ev) {
				dzialaj();
			}
			
			public void insertUpdate(DocumentEvent ev) {
				dzialaj();
			}
			
			public void changedUpdate(DocumentEvent ev) {
				dzialaj();
			}
		};
		
		txtLewy.getDocument().addDocumentListener(dl);
		txtPrawy.getDocument().addDocumentListener(dl);
	}

	private void dzialaj() {
		try {
			double arg1 = Double.parseDouble(txtLewy.getText());
			double arg2 = Double.parseDouble(txtPrawy.getText());
			String operacja = (String)comboBox.getSelectedItem();
			
			double wynik = LogikaKalkulatora.oblicz(arg1, arg2, operacja);
			txtWynik.setText( String.valueOf(wynik) );
			txtWynik.setForeground(Color.BLUE);
		} catch (NumberFormatException e) {
			txtWynik.setText("błąd");
			txtWynik.setForeground(Color.RED);
		}
	}
}
