package com.bizofficer.configurations;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class AuthenticationInterceptor implements HandlerInterceptor {

	   /*******************************************************************************************************
	    * preHandle() method − This is used to perform operations before sending the request to the controller. This method should return true to return the response to the client.
	    * 
	    * ******************************************************************************************************/	
	
	   @Override
	   public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {
		   
			  String uri = request.getRequestURI();
			  
			  //****************************************************
			  /*
				SessionMake session = new SessionMake();
			    Registrations userPassObj = new Registrations();
				userPassObj.setId(Long.valueOf(1));
				userPassObj.setEmail("aa@aa.com");
				userPassObj.setFullName("Romesh Choudhary");
				session.setRegistrations(userPassObj);
				session.setFullName(userPassObj.getFullName());	
				*/
			  //*********************************************************	
			  
			  ///Registrations userObj = (Registrations)request.getSession().getAttribute("registrations");
			  
		   if(!uri.endsWith("registration") && !uri.endsWith("login") && !uri.endsWith("logout") && !uri.endsWith("password") && !(uri.indexOf("/api/")>-1) ) 
		   {
	      
//				if(userObj==null) {
//					response.sendRedirect("login");
//					return false;
//				}
	
		   }else if(uri.endsWith("login")){ // if user session is not expired
//			   if(userObj != null){
//				   response.sendRedirect("home");
//				   return false;
//			   }
		   }
	       return true;
	   }
	  
	   
	   /*******************************************************************************************************
	    * postHandle() method − This is used to perform operations before sending the response to the client.
	    * 
	    * ******************************************************************************************************/	
	   
	   /*
	   @Override
	   public void postHandle(HttpServletRequest request, HttpServletResponse response, 
	      Object handler, ModelAndView modelAndView) throws Exception {
	      
	      System.out.println("Post Handle method is Calling");
	   }
	   */
	   
	   
	   /*******************************************************************************************************
	    * afterCompletion() method − This is used to perform operations after completing the request and response.
	    * 
	    * ******************************************************************************************************/	
	   
	   /*
	   @Override
	   public void afterCompletion
	      (HttpServletRequest request, HttpServletResponse response, Object 
	      handler, Exception exception) throws Exception {
	      
	      System.out.println("Request and Response is completed");
	   }
	   */
	   
}
