import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
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 net.proteanit.sql.DbUtils;

public class Helpers {

	private static Connection connection = sqliteConnection.dbConnector();

	public static void RefreshTable() {
		try {

			String querry = "select * from Macro";
			PreparedStatement pst = connection.prepareStatement(querry);
			ResultSet rs = pst.executeQuery();
			FitnessKalkulačka.mainTable.setModel(DbUtils.resultSetToTableModel(rs));

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

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

	public static void RefreshUserTable() {
		try {

			String querry = "select ID,Název,Bílkoviny,Sacharidy,Tuky,Kcal from UzivatelDen where UserName ='"
					+ FitnessKalkulačka.Jmeno + "'";
			PreparedStatement pst = connection.prepareStatement(querry);
			ResultSet rs = pst.executeQuery();
			FitnessKalkulačka.UserTable.setModel(DbUtils.resultSetToTableModel(rs));

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

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

	public static void RefreshDenikTable() {
		try {

			String querry = "select Váha,Datum from Denik where UserName ='" + FitnessKalkulačka.Jmeno + "'";
			PreparedStatement pst = connection.prepareStatement(querry);
			ResultSet rs = pst.executeQuery();
			FitnessKalkulačka.tableDenik.setModel(DbUtils.resultSetToTableModel(rs));

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

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

	public static Double VypocetDen(int pocet) {
		int rowsCount = FitnessKalkulačka.UserTable.getRowCount();
		double mezisoucet = 0;

		for (int i = 0; i < rowsCount; i++) {
			mezisoucet = mezisoucet + Double.parseDouble(FitnessKalkulačka.UserTable.getValueAt(i, pocet).toString());
		}

		Double hodnota = Math.round(mezisoucet * 100) / 100.0;

		return hodnota;
	}

	public static void RefreshMacroZaDen() {

		FitnessKalkulačka.textBden.setText(Double.toString(VypocetDen(2)));
		FitnessKalkulačka.textSden.setText(Double.toString(VypocetDen(3)));
		FitnessKalkulačka.textTden.setText(Double.toString(VypocetDen(4)));
		FitnessKalkulačka.textKden.setText(Double.toString(VypocetDen(5)));
	}

	public static void VytvorGrafDenik() {
		try {

			String querry = "select Datum,Váha from Denik where UserName ='" + FitnessKalkulačka.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();

			ChartPanel graf = new ChartPanel(GrafDenik);
			FitnessKalkulačka.panelDenikGraf.removeAll();
			FitnessKalkulačka.panelDenikGraf.add(graf);
			FitnessKalkulačka.panelDenikGraf.validate();

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

	}

	public static boolean validateJavaDate(String strDate) {
		if (strDate.trim().equals("")) {
			return false;
		} else {

			SimpleDateFormat sdfrmt = new SimpleDateFormat("yyyy-MM-dd");
			sdfrmt.setLenient(false);
			@SuppressWarnings("unused")
			Date javaDate = null;
			try {
				javaDate = sdfrmt.parse(strDate);

			} catch (Exception e) {
				return false;
			}
			return true;
		}
	}

}
