/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Dao.LoginDao;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 1004242
 */
public class resetPasswordServlet extends HttpServlet {


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

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession(false);
        String modifierLoginId = (String) session.getAttribute("user");
        String pacsId = (String) session.getAttribute("pacsId");
        //String resetableUserId = request.getParameter("userId");
        LoginDao lDao = new LoginDao();
        boolean result = false;
        boolean result1 = false;
        List<String> userSessionList = new ArrayList<String>();
        int counter= 0;

        try {
            if (!(modifierLoginId.equalsIgnoreCase(resetableUserId))) {
                result = lDao.verifySamPacsForUser(resetableUserId, pacsId);
                if (result) {
                    result1 = lDao.resetUserPassword(resetableUserId, pacsId);
                    if (result1) {
                       // request.setAttribute("error", "Reset password is succesful!");
                       // request.getRequestDispatcher("/Deposit/ResetPassword.jsp").forward(request, response);
                        //response.sendRedirect("./login.jsp");
                       // session.removeAttribute("resetableUserId");
                      //  session.invalidate();
                      userSessionList = lDao.deleteFromLoggedInUser(resetableUserId, pacsId);
                    for (String sessionId : userSessionList) {
                        if (LoginServlet.sesssionMap.containsKey(sessionId)) {
                            HttpSession sessionFromMap = LoginServlet.sesssionMap.get(sessionId);
                            sessionFromMap.invalidate();
                            LoginServlet.sesssionMap.remove(sessionId);
                            counter++;

                            request.setAttribute("error", "User login successfully reset. Please login again to check.");
                            request.getRequestDispatcher("/Deposit/ResetPassword.jsp").forward(request, response);
                        }
                    }                    
                       // lDao.insertToResetLoginActivity(modifierLoginId, resetableUserId, pacsId);
                        request.setAttribute("error", "User login successfully reset. Please login again to check.");
                        request.getRequestDispatcher("/Deposit/ResetPassword.jsp").forward(request, response);                    
                    }
                    else {
                        request.setAttribute("error", "Soemthing is wrong. PLease try again.");
                        request.getRequestDispatcher("/Deposit/ResetPassword.jsp").forward(request, response);
                    }
                }
                else {
                request.setAttribute("error", "Target User for reset does not belong to your PACS. Activity is unauthorized.");
                request.getRequestDispatcher("/Deposit/ResetPassword.jsp").forward(request, response);
            }
            } else {
                request.setAttribute("error", "Unauthorized activity. You cannot reset password of your current user.");
                request.getRequestDispatcher("/Deposit/ResetPassword.jsp").forward(request, response);
            }
        } catch (Exception e) {
           // e.printStackTrace();
            request.setAttribute("error", "Soemthing is wrong. PLease try again.");
            request.getRequestDispatcher("/Deposit/ResetPassword.jsp").forward(request, response);
        
        } finally {
            System.out.println("Processing done.");
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
