package org.sistema.hibernate.sql;

import java.util.List;

import org.hibernate.HibernateException;
import org.sistema.hibernate.sql.dao.GenericDAO;
import org.sistema.hibernate.sql.dao.ProductDAO;
import org.sistema.hibernate.sql.models.Product;
import org.sistema.hibernate.sql.models.ProductType;

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

	static ProductDAO productDAO = new ProductDAO();
	static GenericDAO<ProductType> productTypeDAO = new GenericDAO<ProductType>();

	/**
	 * 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());
	}

	/**
	 * Create the products and save them in the database.
	 */
	private static void loadProducts() {

		// 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());

		productType = new ProductType("Electronics");
		productTypeDAO.insert(productType);
		Product product3 = new Product("Smartphone", "Samsung Galaxy",
				productType);
		productDAO.insert(product3);
		System.out.println("Inserted id: " + product3.getId());
		Product product4 = new Product("Monitor", "LG Smart TV", productType);
		productDAO.insert(product4);
		System.out.println("Inserted id: " + product4.getId());

		productType = new ProductType("Cosmetics");
		productTypeDAO.insert(productType);
		Product product5 = new Product("Makeup", "Professional Makeup tools",
				productType);
		productDAO.insert(product5);
		System.out.println("Inserted id: " + product5.getId());

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

	/**
	 * Searches for products with empty description
	 */
	private static void searchEmptyDescription() {
		List<Object[]> products = null;
		try {
			products = productDAO.selectEmptyDescription();
		} catch (HibernateException e) {
			System.err
					.println("An error ocurred while trying to retrieve the user");
			e.printStackTrace();
		}
		if (products == null) {
			System.out.println("\nProducts with empty description not found");
		} else {
			System.out.println("\nProducts found without description:");
			for (Object[] product : products) {
				System.out.println("Product 1: " + product[0] + " - "
						+ product[1]);
			}

		}
	}

	/**
	 * Show the list of products for a given name.
	 * 
	 * @param productName
	 */
	private static void searchProductsByName(String productName) {
		List<Product> products = null;

		try {
			products = productDAO.selectName(productName);
		} catch (HibernateException e) {
			System.err
					.println("An error ocurred while trying to retrieve products");
			e.printStackTrace();
		}
		if (products == null) {
			System.out.println("\nProducts not found");
		} else {
			System.out.println("\nProducts found by name[" + productName
					+ "]: " + products.toString());
		}
	}

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

		// Insert the products
		Main.loadProducts();

		// Select just one
		Product product1 = productDAO.selectById(1l, Product.class);

		// Update data
		product1.setDescription("");
		productDAO.update(product1);

		// Native SQL
		// Select product with empty description
		Main.searchEmptyDescription();

		// Select product by name
		Main.searchProductsByName(product1.getName());
	}
}
