package control;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;

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

import model.MD5;
import model.User;
import model.UserMod;

/**
 * Servlet implementation class Process
 */
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Register() {
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html; charset=UTF-8");		
		String userName = request.getParameter("user");
		String password = request.getParameter("pass");
		String email = request.getParameter("email");
		String address = request.getParameter("address");
		String phone = request.getParameter("phone");
		String realname = request.getParameter("realname");
		String cmnd = request.getParameter("cmnd");
		
		try {
			password = MD5.encode(password);
		} catch (NoSuchAlgorithmException e) {
			System.out.println("Mã hóa MD5 thất bại");
		}
		User user = new User(userName, password, email, address, phone,realname,cmnd);
		UserMod usermod = new UserMod();
		boolean isExist = usermod.checkRegister(userName);
		boolean isEmailex = usermod.checkEmail(email);
		if(isExist == true || isEmailex == true ) {
			if (isEmailex == true){
				request.setAttribute("failmail", email);
			}
			// response.sendRedirect("register.jsp?euser=exist");
			if (isExist == true){
			request.setAttribute("failuser", userName);
			}
			request.setAttribute("user", userName);
			request.setAttribute("pass", email);
			request.setAttribute("email", email);
			request.setAttribute("address", address);
			request.setAttribute("phone", phone);
			request.setAttribute("realname", realname);
			request.setAttribute("cmnd", cmnd);
			RequestDispatcher transferexist = getServletContext().getRequestDispatcher("/index.jsp?customer=reg");
			transferexist.forward(request, response);
			
		}else {
			usermod.Register(user);
			// response.sendRedirect("success.jsp");
			request.setAttribute("user", userName);
			request.setAttribute("email", email);
			request.setAttribute("address", address);
			request.setAttribute("phone", phone);
			request.setAttribute("realname", realname);
			request.setAttribute("cmnd", cmnd);
			RequestDispatcher transfer = getServletContext().getRequestDispatcher("/index.jsp?customer=regok");
			transfer.forward(request, response);
		}
		
	}

}
