package ru.job4j;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

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 java.io.IOException;

/**
 * Ñåðâëåò, îòâå÷àþùèé çà àóòåíòèôèêàöèþ ïîëüçîâàòåëÿ.
 *
 * @author Ayuzyak
 * @version 1.0
 * @since 06.12.2017
 */
public class SignInController extends HttpServlet {
    /**
     * Ëîããåð.
     */
    private static final Logger LOGGER = LogManager.getLogger(Logger.class.getName());

    /**
     * Ìåòîä äëÿ ïåðåíàïðàâëåíèÿ íà ñòðàíèöó àóòåíòèôèêàöèè.
     *
     * @param req  çàïðîñ.
     * @param resp îòâåò.
     * @throws ServletException .
     * @throws IOException      .
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("WEB-INF/views/LoginView.jsp").forward(req, resp);
    }

    /**
     * Àóòåíòèôèêàöèÿ ïîëüçîâàòåëÿ.
     *
     * @param req  çàïðîñ.
     * @param resp îòâåò.
     * @throws ServletException .
     * @throws IOException      .
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String login = req.getParameter("login");
        String password = req.getParameter("password");
        if (UserStore.getInstance().isCredential(login, password)) {
            String role = UserStore.getInstance().getRole(login, password);
            HttpSession session = req.getSession();
            session.setAttribute("login", login);
            session.setAttribute("role", role);
            resp.sendRedirect(String.format("%s/", req.getContextPath()));
        } else {
            req.setAttribute("error", "Credential error");
            doGet(req, resp);
        }
    }
}