package edu.school;

import java.awt.BorderLayout;
import java.awt.Dimension;
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.JOptionPane;
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;
import javax.swing.JSpinner;
import javax.swing.JSlider;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.FlowLayout;

/**
 * Информатика, 8 клас, издателство Просвета, 2017
 * Автори: Светла Бойчева, Николина Николова, Елиза Стефанова, Антон Денев
 * Урок 2.2.2. Видове грешки при програмиране
 * Задача: Заредете проекта калкулатор за изчисляване на индекса
 * на телесна маса с графичен интерфейс с плъзгачи
 * (от папка BMIWithJSliderError).
 * Компилирайте го. Има ли синтактични грешки?
 * Стартирайте приложението.
 * Задайте стойности за височина, тегло и възраст съответно, 0 , 75 и 82.
 * Какъв тип е грешката?
 * Защо се получава?
 * 
 * Вариант 1. Открийте грешките!
 */
public class BMIFrame extends JFrame {

	private JPanel contentPane;
	private JLabel lblHeightValue;
	private JLabel lblWeightValue;
	private JLabel lblAgeValue;
	private ColoredSlider slider;
	private JLabel lblInterpretation;
	//private JPanel slider;
	private JLabel lblShowResult;
	/**
	 * Launch the application.
	 */
	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, 626, 418);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JPanel pnlInputData = new JPanel();
		contentPane.add(pnlInputData);
		SpringLayout sl_pnlInputData = new SpringLayout();
		pnlInputData.setLayout(sl_pnlInputData);
		
		JSlider sldHeight = new JSlider();
		sldHeight.setPaintLabels(true);
		sldHeight.setPaintTicks(true);		
		sldHeight.setMinorTickSpacing(1);
		sldHeight.setMajorTickSpacing(20);
		sldHeight.setMaximum(220);
		sldHeight.setOrientation(SwingConstants.VERTICAL);
		sldHeight.setValue(175);
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, sldHeight, 10, SpringLayout.NORTH, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, sldHeight, 10, SpringLayout.WEST, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.SOUTH, sldHeight, 287, SpringLayout.NORTH, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, sldHeight, 73, SpringLayout.WEST, pnlInputData);
		sldHeight.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent е) {
				lblHeightValue.setText(String.format("%d", sldHeight.getValue()));
			}
		});
		pnlInputData.add(sldHeight);
		pnlInputData.setPreferredSize(new Dimension(250, 370));
		
		JSlider sldWeight = new JSlider();
		sldWeight.setValue(75);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, sldWeight, 9, SpringLayout.EAST, sldHeight);
		sldWeight.setPaintTicks(true);
		sldWeight.setPaintLabels(true);
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, sldWeight, 0, SpringLayout.NORTH, sldHeight);
		sl_pnlInputData.putConstraint(SpringLayout.SOUTH, sldWeight, 0, SpringLayout.SOUTH, sldHeight);
		sldWeight.setMajorTickSpacing(20);
		sldWeight.setMinorTickSpacing(1);
		sldWeight.setMaximum(200);
		sldWeight.setOrientation(SwingConstants.VERTICAL);
		sldWeight.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				lblWeightValue.setText(String.format("%d", sldWeight.getValue()));
			}
		});
		
		pnlInputData.add(sldWeight);
		
		JSlider sldAge = new JSlider();
		sl_pnlInputData.putConstraint(SpringLayout.WEST, sldAge, 177, SpringLayout.WEST, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, sldAge, -10, SpringLayout.EAST, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, sldWeight, -6, SpringLayout.WEST, sldAge);
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, sldAge, 0, SpringLayout.NORTH, sldHeight);
		sl_pnlInputData.putConstraint(SpringLayout.SOUTH, sldAge, 0, SpringLayout.SOUTH, sldHeight);
		sldAge.setValue(18);
		sldAge.setMaximum(120);
		sldAge.setPaintLabels(true);
		sldAge.setPaintTicks(true);
		sldAge.setSnapToTicks(true);
		sldAge.setMinorTickSpacing(1);
		sldAge.setMajorTickSpacing(6);
		sldAge.setMinimum(18);
		sldAge.setOrientation(SwingConstants.VERTICAL);
		sldAge.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				lblAgeValue.setText(String.format("%d", sldAge.getValue()));
			}
		});
		
		pnlInputData.add(sldAge);
		
		JLabel lblHeight = new JLabel("\u0412\u0438\u0441\u043E\u0447\u0438\u043D\u0430(\u0441\u043C.)");
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblHeight, 73, SpringLayout.WEST, sldHeight);
		lblHeight.setHorizontalAlignment(SwingConstants.CENTER);
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblHeight, 6, SpringLayout.SOUTH, sldHeight);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblHeight, 10, SpringLayout.WEST, pnlInputData);
		pnlInputData.add(lblHeight);
		
		JLabel lblWeight = new JLabel("\u0422\u0435\u0433\u043B\u043E(\u043A\u0433.)");
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblWeight, 6, SpringLayout.SOUTH, sldWeight);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblWeight, 9, SpringLayout.EAST, lblHeight);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblWeight, -10, SpringLayout.EAST, sldWeight);
		lblWeight.setHorizontalAlignment(SwingConstants.CENTER);
		pnlInputData.add(lblWeight);
		
		JLabel lblAge = new JLabel("\u0412\u044A\u0437\u0440\u0430\u0441\u0442(\u0433.)");
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblAge, 6, SpringLayout.SOUTH, sldAge);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblAge, 177, SpringLayout.WEST, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblAge, -10, SpringLayout.EAST, pnlInputData);
		lblAge.setHorizontalAlignment(SwingConstants.CENTER);
		pnlInputData.add(lblAge);
		
		lblHeightValue = new JLabel("175");
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblHeightValue, 6, SpringLayout.SOUTH, lblHeight);
		lblHeightValue.setHorizontalAlignment(SwingConstants.CENTER);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblHeightValue, 0, SpringLayout.WEST, sldHeight);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblHeightValue, 0, SpringLayout.EAST, sldHeight);
		pnlInputData.add(lblHeightValue);
		
		lblWeightValue = new JLabel("75");
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblWeightValue, 6, SpringLayout.SOUTH, lblWeight);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblWeightValue, 31, SpringLayout.EAST, lblHeightValue);
		lblWeightValue.setHorizontalAlignment(SwingConstants.CENTER);
		pnlInputData.add(lblWeightValue);
		
		lblAgeValue = new JLabel("18");
		sl_pnlInputData.putConstraint(SpringLayout.WEST, lblAgeValue, 177, SpringLayout.WEST, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblWeightValue, -42, SpringLayout.WEST, lblAgeValue);
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, lblAgeValue, 0, SpringLayout.NORTH, lblHeightValue);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, lblAgeValue, -10, SpringLayout.EAST, pnlInputData);
		lblAgeValue.setHorizontalAlignment(SwingConstants.CENTER);
		pnlInputData.add(lblAgeValue);
		
		JPanel panel = new JPanel();
		sl_pnlInputData.putConstraint(SpringLayout.NORTH, panel, 6, SpringLayout.SOUTH, lblHeightValue);
		sl_pnlInputData.putConstraint(SpringLayout.WEST, panel, 49, SpringLayout.WEST, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.SOUTH, panel, -10, SpringLayout.SOUTH, pnlInputData);
		sl_pnlInputData.putConstraint(SpringLayout.EAST, panel, 25, SpringLayout.EAST, sldWeight);
		pnlInputData.add(panel);
		SpringLayout sl_panel = new SpringLayout();
		panel.setLayout(sl_panel);
		
		JComboBox<String> cbxSex = new JComboBox<String>();
		sl_panel.putConstraint(SpringLayout.NORTH, cbxSex, 0, SpringLayout.NORTH, panel);
		sl_panel.putConstraint(SpringLayout.WEST, cbxSex, 39, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.EAST, cbxSex, -10, SpringLayout.EAST, panel);
		cbxSex.setModel(new DefaultComboBoxModel<String>(new String[] {"\u041C\u044A\u0436", "\u0416\u0435\u043D\u0430"}));
		panel.add(cbxSex);
		
		JLabel lblSex = new JLabel("\u041F\u043E\u043B:");
		sl_panel.putConstraint(SpringLayout.NORTH, lblSex, 3, SpringLayout.NORTH, cbxSex);
		sl_panel.putConstraint(SpringLayout.EAST, lblSex, -6, SpringLayout.WEST, cbxSex);
		panel.add(lblSex);
		
		/*lblImage = new ImagePanel("male.jpg");
		sl_panel.putConstraint(SpringLayout.NORTH, lblImage, 10, SpringLayout.NORTH, panel);
		sl_panel.putConstraint(SpringLayout.WEST, lblImage, 6, SpringLayout.EAST, cbxSex);
		sl_panel.putConstraint(SpringLayout.SOUTH, lblImage, 67, SpringLayout.SOUTH, cbxSex);
		sl_panel.putConstraint(SpringLayout.EAST, lblImage, 98, SpringLayout.EAST, cbxSex);
		panel.add(lblImage);*/
		
		JPanel pnlButtons = new JPanel();
		contentPane.add(pnlButtons);
		SpringLayout sl_pnlButtons = new SpringLayout();
		pnlButtons.setLayout(sl_pnlButtons);
		
		pnlButtons.setPreferredSize(new Dimension(350, 370));
		
		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.SOUTH, btnReset, -327, SpringLayout.SOUTH, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.EAST, btnReset, -42, SpringLayout.EAST, pnlButtons);
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sldAge.setValue(18);
				sldHeight.setValue(175);
				sldWeight.setValue(75);
				slider.setValue(30);
				lblInterpretation.setText("");
				cbxSex.setSelectedIndex(0);
				lblShowResult.setText("");
			}
		});
		pnlButtons.add(btnReset);
		
		JButton btnCalculate = new JButton("\u041F\u0440\u0435\u0441\u043C\u0435\u0442\u043D\u0438");
		sl_pnlButtons.putConstraint(SpringLayout.NORTH, btnCalculate, 10, SpringLayout.NORTH, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.EAST, btnCalculate, -193, SpringLayout.EAST, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, btnReset, 38, SpringLayout.EAST, btnCalculate);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, btnCalculate, 44, SpringLayout.WEST, pnlButtons);
		btnCalculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				double height = ((int)sldHeight.getValue())/100.0;
				double result = (double)sldWeight.getValue() / (height*height);
				lblShowResult.setText(String.format("%.3f", result));
				slider.setValue((int)result);
				if(result == Double.POSITIVE_INFINITY || result == Double.NEGATIVE_INFINITY)
				{
					//JOptionPane.showMessageDialog(BMIFrame.this, "Некоректно въведени данни!", "Грешка", JOptionPane.ERROR_MESSAGE);
					return;
				}
				if(result <= 18.5)
				{
					lblInterpretation.setText("Под нормата");
				}
				else if(result > 18.5 && result <= 25)
				{
					lblInterpretation.setText("Нормално");
				}
				else if(result > 25 && result <= 30)
				{
					lblInterpretation.setText("Наднормено");
				}
				else if(result > 30 && result <= 30)
				{
					lblInterpretation.setText("Умерено затлъстяване");
				}
				else
				{
					lblInterpretation.setText("Опасно затлъстяване");
				}
			}
		});
		pnlButtons.add(btnCalculate);
		
		JButton btnClose = new JButton("\u0417\u0430\u0442\u0432\u043E\u0440\u0438");
		sl_pnlButtons.putConstraint(SpringLayout.NORTH, btnClose, 325, SpringLayout.NORTH, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, btnClose, 195, SpringLayout.WEST, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.SOUTH, btnClose, -10, SpringLayout.SOUTH, pnlButtons);
		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);
		
		JLabel lblResult = new JLabel("\u0420\u0435\u0437\u0443\u043B\u0442\u0430\u0442:");
		sl_pnlButtons.putConstraint(SpringLayout.WEST, lblResult, 0, SpringLayout.WEST, btnCalculate);
		lblResult.setFont(new Font("Tahoma", Font.PLAIN, 13));
		//sl_pnlButtons.putConstraint(SpringLayout.SOUTH, lblResult, 24, SpringLayout.SOUTH, pnlButtons);
		//sl_pnlButtons.putConstraint(SpringLayout.WEST, lblResult, 10, SpringLayout.WEST, pnlButtons);
		pnlButtons.add(lblResult);
		
		JPanel panel_1 = new JPanel();
		sl_pnlButtons.putConstraint(SpringLayout.SOUTH, btnCalculate, -34, SpringLayout.NORTH, panel_1);
		sl_pnlButtons.putConstraint(SpringLayout.NORTH, panel_1, 34, SpringLayout.SOUTH, btnReset);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, panel_1, 20, SpringLayout.WEST, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.EAST, panel_1, -21, SpringLayout.EAST, pnlButtons);
		//sl_pnlButtons.putConstraint(SpringLayout.NORTH, panel_1, 6, SpringLayout.SOUTH, btnCalculate);
		//sl_pnlButtons.putConstraint(SpringLayout.WEST, panel_1, 10, SpringLayout.WEST, pnlButtons);
		//sl_pnlButtons.putConstraint(SpringLayout.SOUTH, panel_1, -62, SpringLayout.SOUTH, lblShowResult);
		//sl_pnlButtons.putConstraint(SpringLayout.EAST, panel_1, 0, SpringLayout.EAST, lblShowResult);
		pnlButtons.add(panel_1);
		panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		Dimension preferredSize = new Dimension(55, 70);
		
		
		ImagePanel panel_2 = new ImagePanel("purple.png");
		panel_2.setPreferredSize(preferredSize);
		panel_1.add(panel_2);
		
		ImagePanel panel_3 = new ImagePanel("blue.png");
		panel_3.setPreferredSize(preferredSize);
		panel_1.add(panel_3);
		
		ImagePanel panel_4 = new ImagePanel("yellow.png");
		panel_4.setPreferredSize(preferredSize);
		panel_1.add(panel_4);
		
		ImagePanel panel_5 = new ImagePanel("orange.png");
		panel_5.setPreferredSize(preferredSize);
		panel_1.add(panel_5);
		
		ImagePanel panel_6 = new ImagePanel("red.png");
		panel_6.setPreferredSize(preferredSize);
		panel_1.add(panel_6);
	
		slider = new ColoredSlider("scrollbackground.png", "Untitled2.png");
		sl_pnlButtons.putConstraint(SpringLayout.NORTH, slider, 162, SpringLayout.NORTH, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.SOUTH, slider, -30, SpringLayout.NORTH, lblResult);
		sl_pnlButtons.putConstraint(SpringLayout.SOUTH, panel_1, -6, SpringLayout.NORTH, slider);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, slider, -1, SpringLayout.WEST, btnCalculate);
		sl_pnlButtons.putConstraint(SpringLayout.EAST, slider, 0, SpringLayout.EAST, btnReset);
		//sl_pnlButtons.putConstraint(SpringLayout.NORTH, slider, 20, SpringLayout.SOUTH, lblShowResult);
		//sl_pnlButtons.putConstraint(SpringLayout.WEST, slider, 21, SpringLayout.WEST, btnReset);
		//sl_pnlButtons.putConstraint(SpringLayout.EAST, slider, 0, SpringLayout.EAST, lblShowResult);
		double height = ((int)sldHeight.getValue())/100.0;
		double result = (double)sldWeight.getValue() / (height*height);
		pnlButtons.add(slider);
		
		lblInterpretation = new JLabel("\u041D\u0430\u0434\u043D\u043E\u0440\u043C\u0435\u043D\u043E \u0442\u0435\u0433\u043B\u043E");
		sl_pnlButtons.putConstraint(SpringLayout.SOUTH, lblResult, -22, SpringLayout.NORTH, lblInterpretation);
		sl_pnlButtons.putConstraint(SpringLayout.NORTH, lblInterpretation, 270, SpringLayout.NORTH, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.SOUTH, lblInterpretation, -35, SpringLayout.NORTH, btnClose);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, lblInterpretation, 47, SpringLayout.WEST, pnlButtons);
		sl_pnlButtons.putConstraint(SpringLayout.EAST, lblInterpretation, -85, SpringLayout.EAST, pnlButtons);
		lblInterpretation.setHorizontalAlignment(SwingConstants.LEFT);
		lblInterpretation.setFont(new Font("Tahoma", Font.BOLD, 16));
		pnlButtons.add(lblInterpretation);
		
		lblShowResult = new JLabel("New label");
		sl_pnlButtons.putConstraint(SpringLayout.NORTH, lblShowResult, 0, SpringLayout.NORTH, lblResult);
		sl_pnlButtons.putConstraint(SpringLayout.WEST, lblShowResult, 8, SpringLayout.EAST, lblResult);
		sl_pnlButtons.putConstraint(SpringLayout.SOUTH, lblShowResult, -22, SpringLayout.NORTH, lblInterpretation);
		sl_pnlButtons.putConstraint(SpringLayout.EAST, lblShowResult, -183, SpringLayout.EAST, pnlButtons);
		lblShowResult.setHorizontalAlignment(SwingConstants.LEFT);
		lblShowResult.setFont(new Font("Tahoma", Font.BOLD, 14));
		pnlButtons.add(lblShowResult);
		
		sldAge.setValue(18);
		sldHeight.setValue(175);
		sldWeight.setValue(75);
		slider.setValue(30);
		lblInterpretation.setText("");
		cbxSex.setSelectedIndex(0);
		lblShowResult.setText("");
	}
}
