package edu.school;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.BoxLayout;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.Font;
	/**
	 * Информатика, 8 клас, изадтелство Просвета, 2017 
	 * Автори: Светла Бойчева, Николина Николова, Елиза Стефанова, Антон Денев 
	 * Урок 2.2.2. Видове грешки при програмиране
	 * Задача: Заредете от работното пространство проекта за изчисляване 
	 * на индекса на телесна маса (папка BMIError) в интегрираната среда Eclipse.
	 * Компилирайте го. Обърнете внимание открити ли са синтактични
	 * грешки. Стартирайте приложението. Задайте стойности
	 * за височина, тегло и възраст съответно 1.77, 81 и 19. 
	 * Откривате ли грешка?
	 * Ако да, каква.
	 * 
	 * Версия 1. Открийте и коригирайте грешките!
	 */
public class BMIFrame extends JFrame {

	private JPanel contentPane;
	private JTextField txtHeight;
	private JTextField txtWeight;
	private JTextField txtAge;



	public static void main(String[] args) {
		try {
			// Set System L&F
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			// handle exception
		} catch (ClassNotFoundException e) {
			// handle exception
		} catch (InstantiationException e) {
			// handle exception
		} catch (IllegalAccessException e) {
			// handle exception
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BMIFrame frame = new BMIFrame();
					frame.setTitle("Индекс на телесна маса");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BMIFrame() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 360, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));

		JPanel pnlInputData = new JPanel();
		contentPane.add(pnlInputData);
		SpringLayout sl_pnlInputData = new SpringLayout();
		pnlInputData.setLayout(sl_pnlInputData);

		JLabel lblHeight = new JLabel("\u0412\u0438\u0441\u043E\u0447\u0438\u043D\u0430");
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblHeight, 22, SpringLayout.NORTH, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblHeight, 10, SpringLayout.WEST, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblHeight, -115, SpringLayout.EAST, pnlInputData);
		pnlInputData.add(lblHeight);

		txtHeight = new JTextField();
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, txtHeight, -3, SpringLayout.NORTH, lblHeight);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, txtHeight, 63, SpringLayout.WEST, pnlInputData);
		pnlInputData.add(txtHeight);
		txtHeight.setColumns(10);

		JLabel lblWeight = new JLabel("\u0422\u0435\u0433\u043B\u043E");
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblWeight, 12, SpringLayout.SOUTH, lblHeight);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblWeight, 28, SpringLayout.WEST, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblWeight, 0, SpringLayout.EAST, lblHeight);
		pnlInputData.add(lblWeight);

		txtWeight = new JTextField();
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, txtWeight, 6, SpringLayout.SOUTH, txtHeight);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, txtWeight, 0, SpringLayout.WEST, txtHeight);
		pnlInputData.add(txtWeight);
		txtWeight.setColumns(10);

		JLabel lblAge = new JLabel("\u0412\u044A\u0437\u0440\u0430\u0441\u0442");
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblAge, 12, SpringLayout.SOUTH, lblWeight);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblAge, 16, SpringLayout.WEST, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblAge, 0, SpringLayout.EAST, lblHeight);
		pnlInputData.add(lblAge);

		txtAge = new JTextField();
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, txtAge, 6, SpringLayout.SOUTH, txtWeight);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, txtAge, 0, SpringLayout.EAST, txtHeight);
		pnlInputData.add(txtAge);
		txtAge.setColumns(10);

		JLabel lblSex = new JLabel("\u041F\u043E\u043B");
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblSex, 12, SpringLayout.SOUTH, lblAge);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblSex, 38, SpringLayout.WEST, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblSex, 0, SpringLayout.EAST, lblHeight);
		pnlInputData.add(lblSex);

		JComboBox cbxSex = new JComboBox();
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, cbxSex, 6, SpringLayout.SOUTH, txtAge);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, cbxSex, 0, SpringLayout.EAST, txtHeight);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, cbxSex, 0, SpringLayout.WEST, txtHeight);
		cbxSex.setModel(new DefaultComboBoxModel(new String[] { "\u041C\u044A\u0436", "\u0416\u0435\u043D\u0430" }));
		pnlInputData.add(cbxSex);

		JLabel lblResult = new JLabel("\u0420\u0435\u0437\u0443\u043B\u0442\u0430\u0442");
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblResult, 60, SpringLayout.SOUTH, lblSex);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblResult, -115, SpringLayout.EAST, pnlInputData);
		pnlInputData.add(lblResult);

		JLabel lblCalculatedResult = new JLabel("");
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblCalculatedResult, 45, SpringLayout.SOUTH, cbxSex);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblCalculatedResult, 63, SpringLayout.WEST, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.SOUTH, lblCalculatedResult, -60, SpringLayout.SOUTH, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblCalculatedResult, 23, SpringLayout.EAST, txtHeight);
		lblCalculatedResult.setFont(new Font("Tahoma", Font.BOLD, 16));
		pnlInputData.add(lblCalculatedResult);

		JPanel pnlButtons = new JPanel();
		contentPane.add(pnlButtons);
		SpringLayout sl_pnlButtons = new SpringLayout();
		pnlButtons.setLayout(sl_pnlButtons);

		JButton btnReset = new JButton("\u0418\u0437\u0447\u0438\u0441\u0442\u0438");
		sl_pnlButtons.putConstraint(SpringLayout.NORTH, btnReset, 20, SpringLayout.NORTH, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, btnReset, 45, SpringLayout.WEST, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.EAST, btnReset, -40, SpringLayout.EAST, pnlButtons);
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				txtAge.setText("");
				txtHeight.setText("");
				txtWeight.setText("");
				lblCalculatedResult.setText("");
			}
		});
		pnlButtons.add(btnReset);

		JButton btnCalculate = new JButton("\u041F\u0440\u0435\u0441\u043C\u0435\u0442\u043D\u0438");
		sl_pnlButtons.putConstraint(SpringLayout.NORTH, btnCalculate, 6, SpringLayout.SOUTH, btnReset);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, btnCalculate, 0, SpringLayout.WEST, btnReset);
		sl_pnlButtons.putConstraint(SpringLayout.EAST, btnCalculate, 0, SpringLayout.EAST, btnReset);
		btnCalculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				double weight = Double.parseDouble(txtWeight.getText());
				double height = Double.parseDouble(txtHeight.getText()) / 100.0;
				double result = weight / (height * height);
				lblCalculatedResult.setText(String.format("%.3f", result));
			}
		});
		pnlButtons.add(btnCalculate);

		JButton btnClose = new JButton("\u0417\u0430\u0442\u0432\u043E\u0440\u0438");
		sl_pnlButtons.putConstraint(SpringLayout.NORTH, btnClose, 6, SpringLayout.SOUTH, btnCalculate);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, btnClose, 0, SpringLayout.WEST, btnReset);
		sl_pnlButtons.putConstraint(SpringLayout.EAST, btnClose, 0, SpringLayout.EAST, btnReset);
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BMIFrame.this.dispose();
			}
		});
		pnlButtons.add(btnClose);
	}
}
