import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.border.LineBorder;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.jdbc.JDBCCategoryDataset;
import java.awt.Color;
import java.awt.BorderLayout;
import java.sql.*;
import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.table.DefaultTableModel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import com.toedter.calendar.JDateChooser;

public class FitnessKalkulačka {

	private JFrame mainFrame;
	private JTextField textVyska;
	private JTextField textFieldVaha;
	private JTable tableInfo1;
	public static JTable mainTable;
	private JTextField textNazev;
	private JTextField textBilkoviny;
	private JTextField textSacharidy;
	private JTextField textTuky;
	private JTextField textKcal;
	private JTextField textGramy;
	public static JTable UserTable;
	public static JTextField textBden;
	public static JTextField textSden;
	public static JTextField textTden;
	public static JTextField textKden;
	public static JPanel panelDenikGraf;

	Connection connection = null;

	public static String Jmeno = null;
	public static int userid = 0;

	private JTextField textBMIvysl;
	public static JTable tableDenik;
	private JTextField textDenikVaha;
	private JDateChooser DenikDatum;
	private JTable tableInfo2;
	private JRadioButton rdbtnMuz;
	private JRadioButton rdbtnZena;

	/**
	 * Launch the application.
	 */
	public static void Fit() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FitnessKalkulačka window = new FitnessKalkulačka();
					window.mainFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */

	public FitnessKalkulačka() {
		initialize();
		connection = sqliteConnection.dbConnector();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		mainFrame = new JFrame();
		mainFrame.setResizable(false);
		mainFrame.setBounds(100, 100, 720, 475);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.getContentPane().setLayout(new CardLayout(0, 0));

		final JPanel panelMain = new JPanel();
		mainFrame.getContentPane().add(panelMain, "name_170921569733186");
		panelMain.setLayout(null);
		panelMain.setVisible(true);

		final JPanel panelBMI = new JPanel();
		mainFrame.getContentPane().add(panelBMI, "name_170947742214177");
		panelBMI.setLayout(null);
		panelMain.setVisible(false);
		panelMain.setVisible(false);

		final JPanel panelMacro = new JPanel();
		mainFrame.getContentPane().add(panelMacro, "name_170952600167461");
		panelMacro.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setEnabled(false);
		scrollPane.setBounds(299, 30, 384, 166);
		panelMacro.add(scrollPane);

		mainTable = new JTable();
		mainTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

				try {

					int row = mainTable.getSelectedRow();
					String Název_ = (mainTable.getModel().getValueAt(row, 0).toString());

					String querry = "select * from Macro where Název = '" + Název_ + "'";
					PreparedStatement pst = connection.prepareStatement(querry);
					ResultSet rs = pst.executeQuery();

					while (rs.next()) {
						textNazev.setText(rs.getString("Název"));
						textBilkoviny.setText(rs.getString("Bílkoviny_100"));
						textSacharidy.setText(rs.getString("Sacharidy_100"));
						textTuky.setText(rs.getString("Tuky_100"));
						textKcal.setText(rs.getString("Kcal_100"));
					}

					rs.close();
					pst.close();

				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
		scrollPane.setViewportView(mainTable);

		JButton btnMacroZpet = new JButton("Zp\u011Bt");
		btnMacroZpet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				panelMacro.setVisible(false);
				panelMain.setVisible(true);
			}
		});
		btnMacroZpet.setBounds(615, 412, 89, 23);
		panelMacro.add(btnMacroZpet);

		textNazev = new JTextField();
		textNazev.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				if (textNazev.getText().length() >= 15) {
					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
			}
		});
		textNazev.setBounds(92, 28, 86, 20);
		panelMacro.add(textNazev);
		textNazev.setColumns(10);

		textBilkoviny = new JTextField();
		textBilkoviny.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent arg0) {
				char c = arg0.getKeyChar();
				if (!(Character.isDigit(c) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_PERIOD)
						|| (c == KeyEvent.VK_BACK_SPACE))) {

					Toolkit.getDefaultToolkit().beep();
					arg0.consume();
				}
				if (textBilkoviny.getText().length() >= 15) {
					Toolkit.getDefaultToolkit().beep();
					arg0.consume();
				}
			}
		});
		textBilkoviny.setBounds(92, 59, 86, 20);
		panelMacro.add(textBilkoviny);
		textBilkoviny.setColumns(10);

		textSacharidy = new JTextField();
		textSacharidy.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent arg0) {
				char c = arg0.getKeyChar();
				if (!(Character.isDigit(c) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_PERIOD)
						|| (c == KeyEvent.VK_BACK_SPACE))) {

					Toolkit.getDefaultToolkit().beep();
					arg0.consume();
				}
				if (textSacharidy.getText().length() >= 15) {
					Toolkit.getDefaultToolkit().beep();
					arg0.consume();
				}
			}
		});
		textSacharidy.setBounds(92, 90, 86, 20);
		panelMacro.add(textSacharidy);
		textSacharidy.setColumns(10);

		textTuky = new JTextField();
		textTuky.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (!(Character.isDigit(c) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_PERIOD)
						|| (c == KeyEvent.VK_BACK_SPACE))) {

					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
				if (textTuky.getText().length() >= 15) {
					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
			}
		});
		textTuky.setBounds(92, 121, 86, 20);
		panelMacro.add(textTuky);
		textTuky.setColumns(10);

		textKcal = new JTextField();
		textKcal.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (!(Character.isDigit(c) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_PERIOD)
						|| (c == KeyEvent.VK_BACK_SPACE))) {

					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
				if (textKcal.getText().length() >= 15) {
					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
			}
		});
		textKcal.setBounds(92, 152, 86, 20);
		panelMacro.add(textKcal);
		textKcal.setColumns(10);

		JLabel lblNazev = new JLabel("N\u00E1zev");
		lblNazev.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNazev.setBounds(10, 31, 76, 14);
		panelMacro.add(lblNazev);

		JLabel lblBilkoviny = new JLabel("B\u00EDlkoviny/100");
		lblBilkoviny.setHorizontalAlignment(SwingConstants.RIGHT);
		lblBilkoviny.setBounds(0, 62, 86, 14);
		panelMacro.add(lblBilkoviny);

		JLabel lblSacharidy = new JLabel("Sacharidy/100");
		lblSacharidy.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSacharidy.setBounds(0, 93, 86, 14);
		panelMacro.add(lblSacharidy);

		JLabel lblTuky = new JLabel("Tuky/100");
		lblTuky.setHorizontalAlignment(SwingConstants.RIGHT);
		lblTuky.setBounds(0, 124, 86, 14);
		panelMacro.add(lblTuky);

		JLabel lblKcal = new JLabel("Kcal/100");
		lblKcal.setHorizontalAlignment(SwingConstants.RIGHT);
		lblKcal.setBounds(10, 155, 76, 14);
		panelMacro.add(lblKcal);

		JLabel lblGramy = new JLabel("Gramy");
		lblGramy.setHorizontalAlignment(SwingConstants.RIGHT);
		lblGramy.setBounds(24, 219, 62, 14);
		panelMacro.add(lblGramy);

		textGramy = new JTextField();
		textGramy.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (!(Character.isDigit(c) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_PERIOD)
						|| (c == KeyEvent.VK_BACK_SPACE))) {

					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
				if (textGramy.getText().length() >= 15) {
					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
			}
		});
		textGramy.setBounds(92, 216, 86, 20);
		panelMacro.add(textGramy);
		textGramy.setColumns(10);

		JLabel lblDatabazeJidla = new JLabel("Datab\u00E1ze j\u00EDdla");
		lblDatabazeJidla.setBounds(300, 11, 125, 14);
		panelMacro.add(lblDatabazeJidla);

		JButton btnUlozitDoDb = new JButton("Ulo\u017Eit do DB");
		btnUlozitDoDb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				try {

					String querry = "insert into Macro (Název,Bílkoviny_100,Sacharidy_100,Tuky_100,Kcal_100) values (?,?,?,?,?)";
					PreparedStatement pst = connection.prepareStatement(querry);

					pst.setString(1, textNazev.getText());
					pst.setString(2, textBilkoviny.getText());
					pst.setString(3, textSacharidy.getText());
					pst.setString(4, textTuky.getText());
					pst.setString(5, textKcal.getText());

					pst.execute();

					JOptionPane.showMessageDialog(null, "Uloženo");

					pst.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
				Helpers.RefreshTable();
			}
		});
		btnUlozitDoDb.setBounds(184, 27, 112, 28);
		panelMacro.add(btnUlozitDoDb);

		JButton btnUlozitDoDbden = new JButton("Ulo\u017Eit do DB/den");
		btnUlozitDoDbden.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				if (textGramy.getText().equals("") || Double.parseDouble(textGramy.getText()) < 0) {
					JOptionPane.showMessageDialog(null, "Vyplňte prosím pole Gramy kladnou hodnotou");
				} else {
					try {
						double Bilkoviny = 0;
						double Sacharidy = 0;
						double Tuky = 0;
						double Kcal = 0;
						double Gramy = 0;
						String Uzivatel = null;

						String querry = "insert into UzivatelDen (Název,Bílkoviny,Sacharidy,Tuky,Kcal,UserName) values (?,?,?,?,?,?)";
						PreparedStatement pst = connection.prepareStatement(querry);

						Gramy = Double.parseDouble(textGramy.getText());
						Bilkoviny = (Double.parseDouble(textBilkoviny.getText()) / 100) * Gramy;
						Sacharidy = (Double.parseDouble(textSacharidy.getText()) / 100) * Gramy;
						Tuky = (Double.parseDouble(textTuky.getText()) / 100) * Gramy;
						Kcal = (Double.parseDouble(textKcal.getText()) / 100) * Gramy;
						Uzivatel = Jmeno;

						double roundBilkoviny = Math.round(Bilkoviny * 100) / 100.0;
						double roundSacharidy = Math.round(Sacharidy * 100) / 100.0;
						double roundTuky = Math.round(Tuky * 100) / 100.0;
						double roundKcal = Math.round(Kcal * 100) / 100.0;

						pst.setString(1, textNazev.getText());
						pst.setString(2, String.valueOf(roundBilkoviny));
						pst.setString(3, String.valueOf(roundSacharidy));
						pst.setString(4, String.valueOf(roundTuky));
						pst.setString(5, String.valueOf(roundKcal));
						pst.setString(6, Uzivatel);

						pst.execute();

						JOptionPane.showMessageDialog(null, "Uloženo");

						pst.close();

					} catch (Exception e) {
						e.printStackTrace();
					}
					Helpers.RefreshUserTable();
					Helpers.RefreshMacroZaDen();
				}
			}
		});
		btnUlozitDoDbden.setBounds(90, 251, 138, 37);
		panelMacro.add(btnUlozitDoDbden);

		JButton btnAktualizovat = new JButton("Aktualizovat");
		btnAktualizovat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {

					String querry = "Update Macro set Název='" + textNazev.getText() + "',Bílkoviny_100='"
							+ textBilkoviny.getText() + "',Sacharidy_100='" + textSacharidy.getText() + "',Tuky_100='"
							+ textTuky.getText() + "',Kcal_100='" + textKcal.getText() + "' where Název = '"
							+ textNazev.getText() + "'";
					PreparedStatement pst = connection.prepareStatement(querry);

					pst.execute();

					JOptionPane.showMessageDialog(null, "Aktualizováno");

					pst.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
				Helpers.RefreshTable();
			}
		});
		btnAktualizovat.setBounds(184, 62, 112, 28);
		panelMacro.add(btnAktualizovat);

		JButton btnSmazat = new JButton("Smazat");
		btnSmazat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {

					String querry = "delete from Macro where Název='" + textNazev.getText() + "' and Bílkoviny_100='"
							+ textBilkoviny.getText() + "' and Sacharidy_100='" + textSacharidy.getText()
							+ "' and Tuky_100='" + textTuky.getText() + "' and Kcal_100='" + textKcal.getText() + "'";
					PreparedStatement pst = connection.prepareStatement(querry);

					pst.execute();

					JOptionPane.showMessageDialog(null, "Smazáno");

					pst.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
				Helpers.RefreshTable();
			}
		});
		btnSmazat.setBounds(184, 96, 112, 28);
		panelMacro.add(btnSmazat);

		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(299, 232, 384, 150);
		panelMacro.add(scrollPane_1);

		UserTable = new JTable();
		UserTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

				try {

					int row = UserTable.getSelectedRow();
					String Název_ = (UserTable.getModel().getValueAt(row, 1).toString());
					String Název_B = (UserTable.getModel().getValueAt(row, 2).toString());
					String Název_S = (UserTable.getModel().getValueAt(row, 3).toString());
					String Název_T = (UserTable.getModel().getValueAt(row, 4).toString());
					String Název_K = (UserTable.getModel().getValueAt(row, 5).toString());

					String querry = "select * from UzivatelDen where Název = '" + Název_ + "' and Bílkoviny='" + Název_B
							+ "' and Sacharidy='" + Název_S + "' and Tuky ='" + Název_T + "' and Kcal='" + Název_K
							+ "'";
					PreparedStatement pst = connection.prepareStatement(querry);
					ResultSet rs = pst.executeQuery();

					while (rs.next()) {
						textNazev.setText(rs.getString("Název"));
						textBilkoviny.setText(rs.getString("Bílkoviny"));
						textSacharidy.setText(rs.getString("Sacharidy"));
						textTuky.setText(rs.getString("Tuky"));
						textKcal.setText(rs.getString("Kcal"));
					}

					rs.close();
					pst.close();

				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
		scrollPane_1.setViewportView(UserTable);

		JLabel lblJidloZaDen = new JLabel("J\u00EDdlo za den");
		lblJidloZaDen.setBounds(299, 207, 101, 14);
		panelMacro.add(lblJidloZaDen);

		JButton btnSmazat1 = new JButton("Smazat z DB/den");
		btnSmazat1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				try {

					int row = UserTable.getSelectedRow();
					String pole = (UserTable.getModel().getValueAt(row, 0).toString());
					String Název_ = (UserTable.getModel().getValueAt(row, 1).toString());
					String Název_B = (UserTable.getModel().getValueAt(row, 2).toString());
					String Název_S = (UserTable.getModel().getValueAt(row, 3).toString());
					String Název_T = (UserTable.getModel().getValueAt(row, 4).toString());
					String Název_K = (UserTable.getModel().getValueAt(row, 5).toString());

					String querry = "delete from UzivatelDen where Název = '" + Název_ + "' and Bílkoviny='" + Název_B
							+ "' and Sacharidy='" + Název_S + "' and Tuky ='" + Název_T + "' and Kcal='" + Název_K
							+ "' and ID ='" + pole + "'";
					PreparedStatement pst = connection.prepareStatement(querry);

					pst.execute();

					JOptionPane.showMessageDialog(null, "Smazáno");

					pst.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
				Helpers.RefreshUserTable();
				Helpers.RefreshMacroZaDen();
			}
		});
		btnSmazat1.setBounds(90, 291, 138, 37);
		panelMacro.add(btnSmazat1);

		JButton btnSmazatVse = new JButton("Smazat v\u0161e");
		btnSmazatVse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				try {

					String querry = "delete from UzivatelDen ";
					PreparedStatement pst = connection.prepareStatement(querry);

					pst.execute();

					JOptionPane.showMessageDialog(null, "Smazán celý den");

					pst.close();

				} catch (Exception e) {
					e.printStackTrace();
				}
				Helpers.RefreshUserTable();
				Helpers.RefreshMacroZaDen();
			}
		});
		btnSmazatVse.setBounds(90, 333, 138, 37);
		panelMacro.add(btnSmazatVse);

		textBden = new JTextField();
		textBden.setEditable(false);
		textBden.setBounds(62, 413, 86, 20);
		panelMacro.add(textBden);
		textBden.setColumns(10);

		textSden = new JTextField();
		textSden.setEditable(false);
		textSden.setBounds(201, 413, 86, 20);
		panelMacro.add(textSden);
		textSden.setColumns(10);

		textTden = new JTextField();
		textTden.setEditable(false);
		textTden.setBounds(339, 413, 86, 20);
		panelMacro.add(textTden);
		textTden.setColumns(10);

		textKden = new JTextField();
		textKden.setEditable(false);
		textKden.setBounds(456, 413, 89, 20);
		panelMacro.add(textKden);
		textKden.setColumns(10);

		JLabel lblB = new JLabel("B:");
		lblB.setBounds(10, 416, 46, 14);
		panelMacro.add(lblB);

		JLabel lblS = new JLabel("S:");
		lblS.setBounds(158, 416, 46, 14);
		panelMacro.add(lblS);

		JLabel lblT = new JLabel("T:");
		lblT.setBounds(297, 416, 46, 14);
		panelMacro.add(lblT);

		JLabel lblK = new JLabel("K:");
		lblK.setBounds(432, 416, 46, 14);
		panelMacro.add(lblK);
		panelMain.setVisible(false);
		panelMain.setVisible(false);

		final JPanel panelDenik = new JPanel();
		mainFrame.getContentPane().add(panelDenik, "name_170964145688445");
		panelDenik.setLayout(null);

		panelDenikGraf = new JPanel();
		panelDenikGraf.setBounds(224, 11, 480, 390);
		panelDenikGraf.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelDenik.add(panelDenikGraf);
		panelDenikGraf.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane_2 = new JScrollPane();

		scrollPane_2.setBounds(10, 164, 204, 271);
		panelDenik.add(scrollPane_2);

		tableDenik = new JTable();
		tableDenik.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				try {
					int row = tableDenik.getSelectedRow();
					String Vaha_ = (tableDenik.getModel().getValueAt(row, 0).toString());
					String Datum_ = (tableDenik.getModel().getValueAt(row, 1).toString());

					String querry = "select * from Denik where Váha = '" + Vaha_ + "' and Datum = '" + Datum_ + "'";
					PreparedStatement pst = connection.prepareStatement(querry);
					ResultSet rs = pst.executeQuery();

					while (rs.next()) {
						textDenikVaha.setText(rs.getString("Váha"));
						((JTextField) DenikDatum.getDateEditor().getUiComponent()).setText(rs.getString("Datum"));
					}

					rs.close();
					pst.close();

				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		scrollPane_2.setViewportView(tableDenik);

		textDenikVaha = new JTextField();
		textDenikVaha.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (!(Character.isDigit(c) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_PERIOD)
						|| (c == KeyEvent.VK_BACK_SPACE))) {

					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
				if (textDenikVaha.getText().length() >= 15) {
					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
			}
		});
		textDenikVaha.setBounds(103, 11, 86, 20);
		panelDenik.add(textDenikVaha);
		textDenikVaha.setColumns(10);

		JButton btnDenikUloz = new JButton("Ulo\u017Eit");
		btnDenikUloz.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				try {
					String Uzivatel = Jmeno;
					String Datum = null;
					Datum = ((JTextField) DenikDatum.getDateEditor().getUiComponent()).getText();

					String querry = "select * from Denik where Datum ='" + Datum + "' and UserName = '" + Uzivatel
							+ "'";
					PreparedStatement pst = connection.prepareStatement(querry);
					ResultSet rs = pst.executeQuery();

					if (rs.next()) {
						JOptionPane.showMessageDialog(null,
								"Pro dané datum již existuje záznam, zvolte prosím jiné nebo smažte stávající záznam");
						pst.close();
					} else {
						try {

							if (Helpers.validateJavaDate(Datum)) {
								String querry1 = "insert into Denik (Váha,Datum,UserName) values (?,?,?)";
								PreparedStatement pst1 = connection.prepareStatement(querry1);

								pst1.setString(1, textDenikVaha.getText());
								pst1.setString(2, ((JTextField) DenikDatum.getDateEditor().getUiComponent()).getText());
								pst1.setString(3, Uzivatel);

								pst1.execute();

								JOptionPane.showMessageDialog(null, "Uloženo");

								pst1.close();
								pst.close();
							} else {

								JOptionPane.showMessageDialog(null, "Datum nemá validní formát yyyy-MM-dd");

							}

						} catch (Exception e2) {
							e2.printStackTrace();
						}
					}

				} catch (Exception e1) {
					e1.printStackTrace();
				}

				Helpers.RefreshDenikTable();
				Helpers.VytvorGrafDenik();
			}
		});
		btnDenikUloz.setBounds(68, 89, 89, 23);
		panelDenik.add(btnDenikUloz);

		JButton btnDenikSmazat = new JButton("Smazat");
		btnDenikSmazat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {

					String datum = ((JTextField) DenikDatum.getDateEditor().getUiComponent()).getText();
					String querry = "delete from Denik where Váha='" + textDenikVaha.getText() + "' and Datum='" + datum
							+ "' and UserName = '" + Jmeno + "'";
					PreparedStatement pst = connection.prepareStatement(querry);

					pst.execute();

					JOptionPane.showMessageDialog(null, "Smazáno");

					pst.close();

				} catch (Exception e1) {
					e1.printStackTrace();
				}
				Helpers.RefreshDenikTable();
				Helpers.VytvorGrafDenik();
			}
		});
		btnDenikSmazat.setBounds(68, 123, 89, 23);
		panelDenik.add(btnDenikSmazat);

		JLabel lblNewLabel = new JLabel("V\u00E1ha");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel.setBounds(34, 11, 59, 14);
		panelDenik.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("Datum");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewLabel_1.setBounds(34, 42, 59, 14);
		panelDenik.add(lblNewLabel_1);

		JButton btnDenikZpet = new JButton("Zp\u011Bt");
		btnDenikZpet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				panelDenik.setVisible(false);
				panelMain.setVisible(true);
			}
		});
		btnDenikZpet.setBounds(615, 412, 89, 23);
		panelDenik.add(btnDenikZpet);

		DenikDatum = new JDateChooser();
		DenikDatum.setDateFormatString("yyyy-MM-dd");
		DenikDatum.setBounds(103, 42, 86, 20);
		panelDenik.add(DenikDatum);

		JButton btnGraf = new JButton("Graf");
		btnGraf.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				try {

					String querry = "select Datum,Váha from Denik where UserName ='" + Jmeno + "' order by Datum";
					JDBCCategoryDataset dataset = new JDBCCategoryDataset(sqliteConnection.dbConnector(), querry);
					JFreeChart GrafDenik = ChartFactory.createLineChart("Deník", "Datum", "Váha", dataset,
							PlotOrientation.VERTICAL, false, true, true);
					@SuppressWarnings("unused")
					BarRenderer renderer = null;
					CategoryPlot plot = GrafDenik.getCategoryPlot();
					CategoryAxis xAxis = (CategoryAxis) plot.getDomainAxis();
					xAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);

					renderer = new BarRenderer();

					ChartFrame grafPop = new ChartFrame("Deník", GrafDenik);
					grafPop.setVisible(true);
					grafPop.setSize(450, 350);

				} catch (Exception e1) {
					e1.printStackTrace();
				}

			}
		});
		btnGraf.setBounds(516, 412, 89, 23);
		panelDenik.add(btnGraf);
		panelMain.setVisible(false);

		JButton btnBMI = new JButton("BMI");
		btnBMI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				panelBMI.setVisible(true);
				panelMain.setVisible(false);
			}
		});
		btnBMI.setBounds(212, 235, 100, 100);
		panelMain.add(btnBMI);

		JButton btnMacro = new JButton("Macro");
		btnMacro.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panelMacro.setVisible(true);
				panelMain.setVisible(false);
				Jmeno = Login.JmenoUzivatele();
				Helpers.RefreshTable();
				Helpers.RefreshUserTable();
				Helpers.RefreshMacroZaDen();
			}
		});
		btnMacro.setBounds(212, 88, 100, 100);
		panelMain.add(btnMacro);

		JButton btnDenik = new JButton("Denik");
		btnDenik.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panelDenik.setVisible(true);
				panelMain.setVisible(false);
				Jmeno = Login.JmenoUzivatele();
				Helpers.RefreshDenikTable();
				Helpers.VytvorGrafDenik();
			}
		});
		btnDenik.setBounds(385, 88, 100, 100);
		panelMain.add(btnDenik);

		JButton btnLogout = new JButton("LogOut");
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.dispose();
				Login.main(null);
			}
		});
		btnLogout.setBounds(385, 235, 100, 100);
		panelMain.add(btnLogout);

		JPanel panelGraf = new JPanel();
		panelGraf.setBorder(new LineBorder(new Color(0, 0, 0)));
		panelGraf.setBounds(329, 23, 341, 262);
		panelBMI.add(panelGraf);
		panelGraf.setLayout(new BorderLayout(0, 0));

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		panelGraf.add(tabbedPane, BorderLayout.CENTER);

		JPanel panelInfo1 = new JPanel();
		tabbedPane.addTab("BMI rozsah", null, panelInfo1, null);
		panelInfo1.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane_3 = new JScrollPane();
		panelInfo1.add(scrollPane_3, BorderLayout.CENTER);

		tableInfo1 = new JTable();
		scrollPane_3.setViewportView(tableInfo1);
		tableInfo1.setEnabled(false);
		tableInfo1.setBorder(new LineBorder(new Color(0, 0, 0)));
		tableInfo1.setModel(new DefaultTableModel(new Object[][] { { "Velmi vážně podvyživený", "< 16.0", "< 21.0" },
				{ "Vážně podvyživený", "16.0 - 16.9", "21.0 - 21.9" }, { "Podvyživený", "17.0 - 18.4", "22.0 - 23.4" },
				{ "Normální", "18.5 - 24.9", "23.5 - 29.9" }, { "Otylý", "25.0 - 29.9", "30.0 - 34.9" },
				{ "Obézní - 1. třída", "30.0 - 34.9", "35.0 - 39.9" },
				{ "Obézní - 2. třída", "35.0 - 39.9", "40.0 - 44.9" }, { "Obézní - 3. třída", ">= 40.0", ">= 45.0" } },
				new String[] { "Popis", "Rozsah - Muž", "Rozsah - Žena" }));

		JPanel panelInfo2 = new JPanel();
		tabbedPane.addTab("Doporučené BMI", null, panelInfo2, null);
		panelInfo2.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane_4 = new JScrollPane();
		panelInfo2.add(scrollPane_4, BorderLayout.CENTER);

		tableInfo2 = new JTable();
		scrollPane_4.setViewportView(tableInfo2);
		tableInfo2.setBorder(new LineBorder(new Color(0, 0, 0)));
		tableInfo2.setModel(new DefaultTableModel(
				new Object[][] { { "19-24", "19-24", "24-29" }, { "25-34", "20-25", "25-30" },
						{ "35-44", "21-26", "26-31" }, { "45-54", "22-27", "27-32" }, { "55-64", "23-28", "28-33" },
						{ ">65", "24-29", "29-34" } },
				new String[] { "Věk", "Doporučené BMI - Muž", "Doporučené BMI - Žena" }));

		JButton btnVypocet = new JButton("V\u00FDpo\u010Det");
		btnVypocet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				if (rdbtnMuz.isSelected() || rdbtnZena.isSelected()) {

					double kg = 0;
					double vyska = 0;
					double bmi = 0;

					kg = Double.parseDouble(textFieldVaha.getText());
					vyska = Double.parseDouble(textVyska.getText()) / 100;

					bmi = kg / (vyska * vyska);
					double roundbmi = Math.round(bmi * 100) / 100.0;

					if (rdbtnMuz.isSelected()) {
						String tbmi = Double.toString(roundbmi);
						textBMIvysl.setText(tbmi);
					} else {
						String tbmi = Double.toString(roundbmi + 5);
						textBMIvysl.setText(tbmi);
					}
					if (roundbmi < 16) {
						textBMIvysl.setOpaque(true);
						textBMIvysl.setBackground(Color.red);
					} else if (roundbmi <= 16.9) {
						textBMIvysl.setOpaque(true);
						textBMIvysl.setBackground(Color.pink);
					} else if (roundbmi <= 18.4) {
						textBMIvysl.setOpaque(true);
						textBMIvysl.setBackground(Color.yellow);
					} else if (roundbmi <= 24.9) {
						textBMIvysl.setOpaque(true);
						textBMIvysl.setBackground(Color.green);
					} else if (roundbmi <= 29.9) {
						textBMIvysl.setOpaque(true);
						textBMIvysl.setBackground(Color.yellow);
					} else if (roundbmi <= 34.9) {
						textBMIvysl.setOpaque(true);
						textBMIvysl.setBackground(Color.orange);
					} else if (roundbmi <= 39.9) {
						textBMIvysl.setOpaque(true);
						textBMIvysl.setBackground(Color.pink);
					} else if (roundbmi >= 40.0) {
						textBMIvysl.setOpaque(true);
						textBMIvysl.setBackground(Color.red);
					}
				} else {
					JOptionPane.showMessageDialog(null, "Prosím zvolte pohlaví");
				}
			}
		});
		btnVypocet.setBounds(100, 268, 122, 39);
		panelBMI.add(btnVypocet);

		JLabel lblVysledek = new JLabel("V\u00FDsledek:");
		lblVysledek.setHorizontalAlignment(SwingConstants.RIGHT);
		lblVysledek.setBounds(222, 366, 97, 14);
		panelBMI.add(lblVysledek);

		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(55, 23, 209, 207);
		panelBMI.add(panel);
		panel.setLayout(null);

		JLabel lblPohlavi = new JLabel("Pohlav\u00ED");
		lblPohlavi.setBounds(31, 125, 46, 14);
		panel.add(lblPohlavi);

		JLabel lblVyska = new JLabel("V\u00FD\u0161ka");
		lblVyska.setBounds(31, 32, 46, 14);
		panel.add(lblVyska);

		JLabel lblVaha = new JLabel("V\u00E1ha");
		lblVaha.setBounds(31, 78, 46, 14);
		panel.add(lblVaha);

		textVyska = new JTextField();
		textVyska.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (!(Character.isDigit(c) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_PERIOD)
						|| (c == KeyEvent.VK_BACK_SPACE))) {

					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
			}
		});
		textVyska.setBounds(91, 29, 86, 20);
		panel.add(textVyska);
		textVyska.setColumns(10);

		rdbtnMuz = new JRadioButton("Mu\u017E");
		rdbtnMuz.setBounds(83, 157, 70, 20);
		panel.add(rdbtnMuz);

		rdbtnZena = new JRadioButton("\u017Dena");
		rdbtnZena.setBounds(83, 122, 70, 20);
		panel.add(rdbtnZena);

		ButtonGroup group = new ButtonGroup();
		group.add(rdbtnZena);
		group.add(rdbtnMuz);

		textFieldVaha = new JTextField();
		textFieldVaha.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				char c = e.getKeyChar();
				if (!(Character.isDigit(c) || (c == KeyEvent.VK_DELETE) || (c == KeyEvent.VK_PERIOD)
						|| (c == KeyEvent.VK_BACK_SPACE))) {

					Toolkit.getDefaultToolkit().beep();
					e.consume();
				}
			}
		});
		textFieldVaha.setBounds(91, 75, 86, 20);
		panel.add(textFieldVaha);
		textFieldVaha.setColumns(10);

		JButton btnZpet1 = new JButton("Zpet");
		btnZpet1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panelBMI.setVisible(false);
				panelMain.setVisible(true);
			}
		});
		btnZpet1.setBounds(615, 412, 89, 23);
		panelBMI.add(btnZpet1);

		textBMIvysl = new JTextField();
		textBMIvysl.setEditable(false);
		textBMIvysl.setBounds(329, 363, 97, 20);
		panelBMI.add(textBMIvysl);
		textBMIvysl.setColumns(10);

	}
}
