package servlet.account;

import core.user.User;
import core.user.utils.AccountManagement;
import error.GExcptAccount;
import error.GExcptSQL;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/register")
public class S_Register extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String,String> user_info=new HashMap<>();
        String id=request.getParameter("id");
        user_info.put("id",id);
        String name=request.getParameter("name");
        user_info.put("name",name);
        String e_mail=request.getParameter("e_mail_location");
        user_info.put("e_mail_location",e_mail);
        String phone_number=request.getParameter("phone_number");
        user_info.put("phone_number",phone_number);
        String password=request.getParameter("password");
        user_info.put("password",password);
        String userType = request.getParameter("userType");
        User user=null;
        try {
            user = AccountManagement.register(userType,user_info);
        } catch (GExcptSQL gExcptSQL) {
            gExcptSQL.printStackTrace();
            response.sendRedirect("/op_fail.jsp");
        } catch (GExcptAccount gExcptAccount) {
            gExcptAccount.printStackTrace();
            response.sendRedirect("/op_fail.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("/op_fail.jsp");
        }
        request.getSession().setAttribute("user",user);
        response.sendRedirect("/home/home.jsp");
        return;
    }
}