package edu.school;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
 * Информатика, 8 клас, издателство Просвета, 2017
 * Автори: Светла Бойчева, Николина Николова, Елиза Стефанова, Антон Денев
 * Урок 2.1. Интегрирана среда за визуално програмиране
 * Задача: Заредете в интегрираната среда за програмиране
 * готовия проект GCD за пресмятане
 * на най-голям общ делител (НОД) на две числа
 *  чрез алгоритъма на Евклид.
 *  • Редактирайте графичния потребителски интерфейс,
 *  като подре дите бутоните центрирано отгоре.
 *  • Запазете проекта.
 *  • Стартирайте го.
 *  
 *  Вариант 1. Направете необходимите корекции в дизайна на ГПИ!
 */
public class GCDFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtA;
	private JTextField txtB;

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

	/**
	 * Create the frame.
	 */
	public GCDFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 408, 220);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel pnlInputData = new JPanel();
		pnlInputData.setBounds(0, 0, 194, 181);
		contentPane.add(pnlInputData);
		SpringLayout sl_pnlInputData = new SpringLayout();
		pnlInputData.setLayout(sl_pnlInputData);

		JLabel lblA = new JLabel("a = ");
		lblA.setFont(new Font("Tahoma", Font.PLAIN, 14));
		pnlInputData.add(lblA);

		txtA = new JTextField();
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblA, 3, SpringLayout.NORTH, txtA);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblA, -6, SpringLayout.WEST, txtA);
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, txtA, 10, SpringLayout.NORTH, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, txtA, 41, SpringLayout.WEST, pnlInputData);
		txtA.setFont(new Font("Tahoma", Font.PLAIN, 14));
		pnlInputData.add(txtA);
		txtA.setColumns(10);

		JLabel lblB = new JLabel("b =");
		lblB.setFont(new Font("Tahoma", Font.PLAIN, 14));
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblB, 15, SpringLayout.SOUTH, txtA);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblB, 0, SpringLayout.WEST, lblA);
		pnlInputData.add(lblB);

		txtB = new JTextField();
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, txtB, -3, SpringLayout.NORTH, lblB);
		txtB.setFont(new Font("Tahoma", Font.PLAIN, 14));
		sl_pnlInputData.putConstraint(SpringLayout.WEST, txtB, 0, SpringLayout.WEST, txtA);
		pnlInputData.add(txtB);
		txtB.setColumns(10);

		JLabel lblResult = new JLabel("\u041D\u041E\u0414 (a, b) =");
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblResult, 41, SpringLayout.SOUTH, txtB);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblResult, 0, SpringLayout.WEST, lblA);
		lblResult.setFont(new Font("Tahoma", Font.PLAIN, 14));
		pnlInputData.add(lblResult);

		JPanel pnlButtons = new JPanel();
		pnlButtons.setBounds(195, 0, 194, 181);
		contentPane.add(pnlButtons);
		SpringLayout sl_pnlButtons = new SpringLayout();
		pnlButtons.setLayout(sl_pnlButtons);

		JButton btnCalculate = new JButton("\u041F\u0440\u0435\u0441\u043C\u0435\u0442\u043D\u0438");
		sl_pnlButtons.putConstraint(SpringLayout.WEST, btnCalculate, 10, SpringLayout.WEST, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.SOUTH, btnCalculate, -10, SpringLayout.SOUTH, pnlButtons);
		btnCalculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					String strA = txtA.getText();
					if (strA.isEmpty() || !strA.matches("\\d+")) {
						JOptionPane.showMessageDialog(null, "Моля, въведете цяло число в полето за a!", "Грешка!",
								JOptionPane.ERROR_MESSAGE);
					}
					int a = Integer.parseInt(strA);

					String strB = txtB.getText();
					if (strB.isEmpty() || !strB.matches("\\d+")) {
						JOptionPane.showMessageDialog(null, "Моля, въведете цяло число в полето за b!", "Грешка!",
								JOptionPane.ERROR_MESSAGE);
					}
					int b = Integer.parseInt(strB);

					while (a != b) {
						if (a > b)
							a -= b;
						else
							b -= a;
					}

					lblResult.setText("НОД(a,b) = " + a);
				} catch (NumberFormatException nfe) {
					JOptionPane.showMessageDialog(null, "Некоректни данни!", "Грешка!", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnCalculate.setFont(new Font("Tahoma", Font.PLAIN, 14));
		pnlButtons.add(btnCalculate);

		JButton btnReset = new JButton("\u0418\u0437\u0447\u0438\u0441\u0442\u0438");
		sl_pnlButtons.putConstraint(SpringLayout.NORTH, btnReset, 10, SpringLayout.NORTH, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, btnReset, 0, SpringLayout.WEST, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.SOUTH, btnReset, -146, SpringLayout.SOUTH, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.EAST, btnReset, -94, SpringLayout.EAST, pnlButtons);
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtA.setText("");
				txtB.setText("");
				lblResult.setText("НОД(a,b) = ");
			}
		});
		btnReset.setFont(new Font("Tahoma", Font.PLAIN, 14));
		pnlButtons.add(btnReset);

		JButton btnClose = new JButton("\u0417\u0430\u0442\u0432\u043E\u0440\u0438");
		sl_pnlButtons.putConstraint(SpringLayout.NORTH, btnClose, 6, SpringLayout.SOUTH, btnReset);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, btnClose, 38, SpringLayout.WEST, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.EAST, btnClose, -55, SpringLayout.EAST, pnlButtons);
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		btnClose.setFont(new Font("Tahoma", Font.PLAIN, 14));
		pnlButtons.add(btnClose);
	}
}
