package agilpizza.servlet;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import agilpizza.dao.CategoriaDAO;
import agilpizza.modelo.Categoria;
import agilpizza.modelo.Ingrediente;
import agilpizza.modelo.Pizza;


public class GerarPizzaAleatoriaServlet extends HttpServlet {
	private static final long serialVersionUID = -2022384804075410287L;

	private Pizza pizza; 
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		
		pizza = new Pizza("Aleatória");
		adicionarIngredientesAleatorios();
		req.getSession().setAttribute("pizza", pizza);
		resp.sendRedirect("mostraPizza.jsp");
	}
	
	
	private void adicionarIngredientesAleatorios() {
		CategoriaDAO categoriaDAO =  CategoriaDAO.getCategoriaDAO();
		List<Categoria> categorias = categoriaDAO.buscaCategorias();
		
		Random random = new Random();
		int indiceNumeroIngredientes = random.nextInt(10) + 1;
		int indice;

		
		for(int i = 0; i < indiceNumeroIngredientes; i++) {
			indice = random.nextInt(categorias.size());
			Categoria categoriaAleatoria = categorias.get(indice);
			
			indice = random.nextInt(categoriaAleatoria.getIngredientes().size());
			Ingrediente ingrediente = categoriaAleatoria.getIngredientes().get(indice);
			indice = random.nextInt(10) + 1;
			pizza.adicionaIngrediente(ingrediente.getNome(),ingrediente.getPreco(),indice);
		}
		
		String[] tiposDeMassa = new String[]{"Média", "Fina", "Grossa"};
		indice = random.nextInt(tiposDeMassa.length);
		pizza.setTipoMassa(tiposDeMassa[indice]);
	}


	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		doGet(req,resp);
	}
	
	
	public Pizza getPizza() {
		return pizza;
	}
	
}

