package org.sistema.hibernate.filters;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.sistema.hibernate.filters.dao.GenericDAO;
import org.sistema.hibernate.filters.models.Sale;
import org.sistema.hibernate.filters.models.Salesperson;

/**
 * Main class
 * 
 * @author Eugenia Pérez
 * @email eugenia_perez@cuatrovientos.org
 */
public class Main {

	static GenericDAO<Salesperson> salespersonDAO = new GenericDAO<Salesperson>();
	static GenericDAO<Sale> salesDAO = new GenericDAO<Sale>();

	/**
	 * Load the salespeople and save them in the database.
	 */
	private static void loadSalespeople() {
		Salesperson sp1 = new Salesperson("Eugenia Martínez", 30000f, 0.15f);
		Salesperson sp2 = new Salesperson("Alfredo Pérez", 33000f, 0.20f);

		salespersonDAO.insert(sp1);
		salespersonDAO.insert(sp2);

	}

	/**
	 * Save some sales and add to the current sales of the sales agent.
	 */
	private static void saveSales() {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date1 = "2014-09-15";
		String date2 = "2015-09-15";

		Sale sale1 = null, sale2 = null;
		try {
			sale1 = new Sale(1200.0, 0.1f, sdf.parse(date1));
			sale2 = new Sale(650.0, 0.25f, sdf.parse(date2));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Salesperson sp1 = salespersonDAO.selectById(1l, Salesperson.class);
		sp1.addSales(sale1);
		sp1.addSales(sale2);

		salesDAO.insert(sale1);
		salesDAO.insert(sale2);
	}

	/**
	 * Show the sales made by every single sales agent.
	 */
	private static void showSalesByAgent() {

		List<Salesperson> agents = (List<Salesperson>) salespersonDAO
				.selectAll(Salesperson.class);
		for (Salesperson agent : agents) {
			System.out.println(agent.toString());
		}
	}

	/**
	 * Load the application and manage the logical operations.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		Main.loadSalespeople();
		Main.saveSales();
		Main.showSalesByAgent();

	}

}
