package ar.edu.itba.it.paw;

import java.io.IOException;

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

import ar.edu.itba.it.paw.model.CredentialsManager;
import ar.edu.itba.it.paw.model.UserInfo;

@SuppressWarnings("serial")
public class Login extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		if (CredentialsManager.getInstance().isValid(username, password)) {
			HttpSession sess = req.getSession(true);
			UserInfo userInfo = new UserInfo(username, "a@a.com");
			sess.setAttribute("USER", userInfo);
			resp.sendRedirect(req.getContextPath() + "/listHotels");
		} else {
			resp.sendRedirect(req.getContextPath() + "/login?error=true");
		}

	}
}
