package control;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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 model.User;
import model.UserMod;

/**
 * Servlet implementation class AuthenticAccount
 */
public class AuthenticAccount extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AuthenticAccount() {
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
		String name =  request.getParameter("user");
		String pass = request.getParameter("pass");
		
		User user = new User(name, pass, "", "", "","","");
		UserMod usermod = new UserMod();	
		boolean isExist = usermod.checkLogin(user);	
		
		if(isExist == true) {
			// Dat Session
			HttpSession session = request.getSession();
			session.setAttribute("user", name);
			// Ket Thuc Session
			response.sendRedirect("index.jsp?customer=changepw");
		}else {
			request.setAttribute("loginattempt", name);
			RequestDispatcher transfer = getServletContext().getRequestDispatcher("/index.jsp?customer=attempt");
			transfer.forward(request, response);
		}
		
		} catch (Exception e) {
			response.setCharacterEncoding("UTF-8");
		    response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().write("Hacking attempt !");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
