package grc.controlador.action;

import grc.model.patronDAO.factorias.DAOFactory;
import grc.model.patronDAO.interfaces.SugerenciasDAO;
import grc.recursos.beans.BeanSugerencias;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.MappingDispatchAction;
import javax.servlet.http.HttpSession;


public class ActionSugerencias extends MappingDispatchAction {


     public ActionForward detalleSugerencias(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

        DAOFactory bd = DAOFactory.getDAOFactory(DAOFactory.POSTGRE);

        SugerenciasDAO dao = bd.getSugerenciasDAO();

        try {
            String codigo = request.getParameter("idSugerencia");

            BeanSugerencias detalle = dao.detalleSugerencias(Integer.parseInt(codigo));

            request.setAttribute("sugerencias", detalle);

            request.setAttribute("codigo", detalle.getCodigo());
            request.setAttribute("nombre", detalle.getNombre());
            request.setAttribute("apellido", detalle.getApellido());
            request.setAttribute("email", detalle.getEmail());
            request.setAttribute("direccion", detalle.getDireccion());
            request.setAttribute("asunto", detalle.getAsunto());
            request.setAttribute("mensaje", detalle.getMensaje());

        } catch (Exception e) {
            System.out.println(e);
        }
        return mapping.findForward("sugerencias");

    }
     public ActionForward lista(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        DAOFactory bd = DAOFactory.getDAOFactory(DAOFactory.POSTGRE);
        SugerenciasDAO dao = bd.getSugerenciasDAO();
        try {
            ArrayList<BeanSugerencias> lista = dao.listarTodos();
            request.setAttribute("sugerencias", lista);
        } catch (Exception e) {
            System.out.println(e);
        }
        return mapping.findForward("salidaLista");
    }

    public ActionForward inserta(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

        DynaActionForm formSugerencias = (DynaActionForm) form;

        String nombre = formSugerencias.getString("nombre");
        String apellido = formSugerencias.getString("apellido");
        String email = formSugerencias.getString("email");
        String direccion = formSugerencias.getString("direccion");
        String asunto = formSugerencias.getString("asunto");
        String mensaje = formSugerencias.getString("mensaje");

        //validacines;
         Functions fnc = new Functions();
        Boolean bln = true;
        
        if(nombre.equals("")){
            request.setAttribute("msgNombre", "Ingresar el Nombre");
            bln = false;
        }
        if(apellido.equals("")){
            request.setAttribute("msgApellido", "Ingresar el Apellido");
            bln = false;
        }
        if(!fnc.isEmail(email)){
            request.setAttribute("msgEmail", "Ingrese un Email correcto");
            bln = false;
        }
        if(mensaje.equals("")){
            request.setAttribute("msgMensaje", "Ingresar el Mensaje");
            bln = false;
        }
        if (!bln)
            return mapping.findForward("error");
      
           //Para el captcha----------------------todos los campos tienen que estar llenos

            HttpSession session1 = request.getSession(false);

            String captcha = (String) session1.getAttribute("captchatxt"); // Captch text stored in session.

            session1.removeAttribute("captchatxt");

            String captchaText = formSugerencias.getString("captchaText");

            
             if (captcha == null || captchaText == null)
                {
                 return mapping.findForward("error");
                }
           
           
                         

              //Fin Para el captcha---------------------------------

        DAOFactory bd = DAOFactory.getDAOFactory(DAOFactory.POSTGRE);

        SugerenciasDAO dao = bd.getSugerenciasDAO();


          if (captcha.equals(captchaText))

          {

                    
                   try {

                        BeanSugerencias bean = new BeanSugerencias();
                        bean.setNombre(nombre);
                        bean.setApellido(apellido);
                        bean.setEmail(email);
                        bean.setDireccion(direccion);
                        bean.setAsunto(asunto);
                        bean.setMensaje(mensaje);

                        dao.insertar(bean);

                    } catch (Exception e) {
                        System.out.println(e);
                    }
                   return mapping.findForward("salida");
          }
          else
          {
           return mapping.findForward("error");
          }
       // return lista(mapping, form, request, response);  ---antes estaba esto y tambien funcionaba
          
    }

     public ActionForward elimina(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        DAOFactory bd = DAOFactory.getDAOFactory(DAOFactory.POSTGRE);
        SugerenciasDAO dao = bd.getSugerenciasDAO();

        String codigo = request.getParameter("codigo");
        try {
            dao.eliminar(Integer.parseInt(codigo));
        } catch (Exception e) {
            System.out.println(e);
        }

        return lista(mapping, form, request, response);
    }

}
