package org.sistema.hibernate.criteria;

import java.util.List;

import org.sistema.hibernate.criteria.dao.GenericDAO;
import org.sistema.hibernate.criteria.dao.ProductTypeDAO;
import org.sistema.hibernate.criteria.models.Product;
import org.sistema.hibernate.criteria.models.ProductType;

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

	/**
	 * Simple function for reusing. Show the list of current products.
	 * 
	 * @param productDAOInterface
	 */
	public static void showAllProducts(List<Product> products) {
		String productDesc = "";

		System.out.println("\n--- Products ----- table contents -----------");

		for (Product product : products) {
			productDesc = "Id: " + product.getId() + " - Name: "
					+ product.getName() + " - Type: "
					+ product.getProductType().getName();
			System.out.println(productDesc);
		}

		System.out.println("Total products: " + products.size());
	}

	/**
	 * Simple function for reusing. Show the list of current type of products.
	 * 
	 * @param productDAOInterface
	 */
	public static void showAllProductTypes(List<ProductType> productTypes) {
		String productDesc = "";

		System.out
				.println("\n--- ProductsTypes ----- table contents -----------");

		for (ProductType productType : productTypes) {
			productDesc = "Id: " + productType.getId() + " - Name: "
					+ productType.getName() + " - Products: \n";
			productDesc += productType.getProducts().toString() + "\n";

			System.out.println(productDesc);
		}

		System.out.println("Total products types: " + productTypes.size());
	}

	/**
	 * Main function to load the products and manage them.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		GenericDAO<Product> productDAO = new GenericDAO<Product>();
		ProductTypeDAO productTypeDAO = new ProductTypeDAO();

		// Insert new data
		ProductType productType = new ProductType("Luxurious");
		productTypeDAO.insert(productType);
		Product product1 = new Product("Angulas", "Angulas de Aginaga",
				productType);
		productDAO.insert(product1);
		System.out.println("Inserted id: " + product1.getId());
		Product product2 = new Product("Caviar", "Caviar Beluga", productType);
		productDAO.insert(product2);
		System.out.println("Inserted id: " + product2.getId());

		System.out.println("\nShow data after new insert");
		showAllProducts(productDAO.selectAll(Product.class));

		// Select just one
		Product oneProduct = productDAO.selectById(1l, Product.class);
		System.out.println("Selected Name: " + oneProduct.getName());

		// Select ProductType with criteria
		System.out.println("All product types between an id range:");
		System.out.println("Total products types: " + productTypeDAO.selectIdRange(1l, 5l).size());
		System.out.println("Show all product types with a given prefix");
		showAllProductTypes(productTypeDAO.selectNamePrefix("L"));
		System.out
				.println("Show all product types whose product names contains a given String");
		showAllProductTypes(productTypeDAO.selectProductName("angulas"));

		// Update data
		product1.setName("Queso Cabrales");
		productDAO.update(product1);

		// Select all products and product types
		System.out.println("\nShow data after update");
		showAllProducts(productDAO.selectAll(Product.class));
		showAllProductTypes(productTypeDAO.selectAll(ProductType.class));

		// Delete data
		productDAO.delete(product1);

		System.out.println("\nShow data after deletion");
		showAllProducts(productDAO.selectAll(Product.class));

	}
}
