package br.dsc.upe.poli.mvc;

import flexjson.JSONSerializer;
import ialgoritmos.IFatoracao;

import java.io.IOException;

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

import algoritmos.ECM;
import algoritmos.Fermat;
import algoritmos.Pollard;
import algoritmos.Pollardp;
import algoritmos.TrialDivision;

/**
 * Servlet implementation class ServletControladorFatorador
 */
public class ServletControladorFatorador extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletControladorFatorador() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String algoritmo = request.getParameter("algoritmo");
		
		
		try{
		int numero = Integer.parseInt(request.getParameter("numero"));
		if(algoritmo.equals("TrialDivision")){
			TrialDivision metodo = new TrialDivision(numero);
			String resposta =  metodo.fatora();
			JSONSerializer serializer = new JSONSerializer();
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(serializer.serialize(resposta));
		}else if(algoritmo.equals("Fermat")){
			Fermat metodo = new Fermat(numero);
			String resposta =  metodo.fatora();
			JSONSerializer serializer = new JSONSerializer();
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(serializer.serialize(resposta));
		
		}else if (algoritmo.equals("Pollard")){
			Pollard metodo = new Pollard(numero);
			String resposta =  metodo.fatora();
			JSONSerializer serializer = new JSONSerializer();
			//response.setCharacterEncoding("UTF-8");
			response.getWriter().write(serializer.serialize(resposta));
		}else if(algoritmo.equals("Pollardp")){
			Pollardp metodo = new Pollardp(numero);
			String resposta =  metodo.fatora();
			JSONSerializer serializer = new JSONSerializer();
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(serializer.serialize(resposta));
		}else if(algoritmo.equals("ECM")){
			ECM metodo = new ECM(numero);
			String resposta =  metodo.fatora();
			JSONSerializer serializer = new JSONSerializer();
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(serializer.serialize(resposta));
		}}catch (NumberFormatException e) {
			JSONSerializer serializer = new JSONSerializer();
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(serializer.serialize("Nossos algoritmos possuem uma limitação quanto ao tamanho dos números a serem fatorados. Tente um número menor"));
		}
		
	}	
	


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
