package Controller;

import LoginDb.OtpUtility;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class OtpVerificationServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        String verifyId = (String) session.getAttribute("verifyId");
        //String otp = request.getParameter("otp");
        String user = (String) session.getAttribute("user");
        String pacsId = (String) session.getAttribute("pacsId");
        OtpUtility ou = new OtpUtility();

        String message = ou.verifyValidateOtp(verifyId, otp, user, pacsId);

        if (message.equals("OTP Verified Successfully!")) {

            ArrayList al = (ArrayList) session.getAttribute("list");
            session.setAttribute("UserName", al.get(0));
            session.setAttribute("pacsName", al.get(1));
            session.setAttribute("userRole", al.get(2));
            session.setAttribute("RoleName", al.get(3));
            session.setAttribute("subPacsFlag", al.get(4));
            session.setAttribute("headPacsId", al.get(5));

            //request.getRequestDispatcher("/welcome.jsp").forward(request, response);
            response.sendRedirect("./welcome.jsp");

        } else if (message.equals("OTP has been Already Verified")) {
            request.setAttribute("error", "OTP has already been verified");
            request.getRequestDispatcher("/Login.jsp").forward(request, response);
            session.invalidate();
        } else if (message.equals("Incorrect Otp!")) {
            request.setAttribute("error", "Incorrect Otp");
            request.setAttribute("otp_flag", "valid");//required to show the otp screen again
            request.getRequestDispatcher("/Login.jsp").forward(request, response);
            //session.invalidate();
        } else if (message.equals("OTP has been Expired!")) {
            request.setAttribute("error", "OTP has been Expired");
            request.getRequestDispatcher("/Login.jsp").forward(request, response);
            session.invalidate();
        } else {
            request.setAttribute("error", "Otp Error Occured");
            request.getRequestDispatcher("/Login.jsp").forward(request, response);
            session.invalidate();
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
