package br.dsc.upe.poli.mvc;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

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

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperRunManager;

import br.dsc.upe.poli.banco.ConexaoFactory;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processaRequisicao(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processaRequisicao(request, response);
	}

	public void processaRequisicao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		ServletOutputStream servletOutputStream = response.getOutputStream();
		String caminho = "WEB-INF/relatorios/";
		String relatorio = caminho+"alunosRespostas.jasper";
		InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream(relatorio);
		
		ServletContext context = getServletContext();
		String nome = request.getParameter("nomeAluno");
		System.out.println(nome);
		String identidade = request.getParameter("identidadeAluno");
		try {
			Connection conn = new ConexaoFactory().getConexao();
			HashMap<String, String> map = new HashMap<String, String>();
			PreparedStatement stmt = conn.prepareStatement("select nome_aluno, atividade, pergunta, resposta  from resposta, alunos_table as al where login = (select login from validacao_table where identidade = ?) and al.identidade_aluno =  ?");
			stmt.setString(1, identidade);
			stmt.setString(2, identidade);
			ResultSet rs = stmt.executeQuery();
			JRResultSetDataSource jr = new JRResultSetDataSource(rs);
			JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, null,jr);
			
			response.setContentType("application/pdf");
			servletOutputStream.flush();
			servletOutputStream.close();
			
			if(conn!=null)conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JRException e) {
			
			StringWriter stringWriter = new StringWriter();
			PrintWriter printWriter = new PrintWriter(stringWriter);
			e.printStackTrace(printWriter);
			response.setContentType("text/plain");
			response.getOutputStream().print(stringWriter.toString());
		}
		
	}
	
}
