/*
 * ComparativeReportSelection.java
 *
 */

package com.fenalco.servlets.sysmanager.reports;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.fenalco.entities.User;

import com.fenalco.utilities.MessageBuilder;

/**
 *
 * @author  Néstor Romero León
 * @version
 */
public class ComparativeReportSelection extends HttpServlet {
    
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        
    }
    
    /** Destroys the servlet.
     */
    public void destroy() {
        
    }
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        String url = "";
        String message = "";
        HttpSession session = request.getSession(false);
        //try{
            if( session == null || ( User )session.getAttribute( "user" ) == null || ( ( User )session.getAttribute( "user" ) ).getRoleId() != 5 ){
                url = "/index.jsp";
                message = MessageBuilder.buildMessage(15);
            }else{

                int consolidatedType = 0;
                int behavioralType = 0;
                boolean isBehavioral = false;
                boolean firstTime = false;
                
                if( request.getParameter( "consolidated" ) != null ){
                    consolidatedType = Integer.parseInt(request.getParameter( "consolidated" ));
                }else{
                    firstTime = true;
                }
                
                /*
                if( request.getParameter( "behavioralType" ) != null ){
                    behavioralType = Integer.parseInt(request.getParameter( "behavioralType" ));
                }else{
                    firstTime = true;
                }
                 */
                
                
                if( request.getParameter( "isBehavioral" ) != null ){
                    int auxB = Integer.parseInt(request.getParameter( "isBehavioral" ));
                    isBehavioral = (auxB==0)?false:true;
                }else{
                    firstTime = true;
                }
                
                System.out.println( consolidatedType + " :: " + isBehavioral  + " :: " + firstTime );
                
                if( !firstTime ){
                
                    if( isBehavioral ){
                    
                        //Reporte de comportamiento/crecimiento
                        url = "/administrador/reportes/comportamiento";
                        
                    }else{
                    
                        //Reporte Comparativo
                        switch( consolidatedType ){
                        
                            //Proveedor
                            case 0:
                                url = "/administrador/reportes/comparativo_proveedores";                            
                            break;

                            //Ciudad
                            case 1:
                                url = "/administrador/reportes/comparativo_ciudades";
                            break;
                        }
                        
                    }
                    
                }else{
                    url = "/administrador/reportes/seleccion_comparativo.jsp";
                }
                
                System.out.println( "URL: " + url );
                
                ////////////////////////////////////////////////////////////////
                
            }    
        
        
        request.setAttribute("message", message);
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);
        
    }
    
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    
}
