package locadora.servlets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import locadora.hibernate.HibernateUtil;
import locadora.modelo.user.User;
import locadora.persistencia.dao.UserDAO;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.Session;

public class Login implements ServletRequestAware {
	private static final long serialVersionUID = 1L;
    private HttpServletRequest request;
    private HttpSession session = request.getSession(true);
    
	public String execute() throws Exception {

	
		String email = (String)request.getParameter("email"); 
		String senha = (String)request.getParameter("senha"); 
		String url = null;
	     
	   try {  
		 Session hibernateSession = new HibernateUtil().getSession();
		 UserDAO userDao = new UserDAO(User.class, hibernateSession);
		 User novoCliente= userDao.verificaLogin(email,senha);
	     if(novoCliente!=null){
	    	//Seta a sessao de login
				session.setAttribute("cliente", novoCliente);
				session.setAttribute("nome_autor", novoCliente.getNome()); 
				return "logado";
								
	     }
	     else{
	    	 session.setAttribute("cliente", novoCliente);
	    	 return "naologado";   
			 
		 }
	   
	    } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "naologado";
		}
	    
	}
	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.request = servletRequest;
	}
	
	public HttpServletRequest getServletRequest() {
		return this.request;
	}

}
