package com.servlets;

import java.io.IOException;
import java.io.PrintWriter;

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 com.artifacts.FactoryProvider;
import com.dao.SellerDao;
import com.entities.Seller;



public class SloginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
   
    public SloginServlet() {
        super();
       
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	response.setContentType("text/html;charset=UTF-8");
    	try(PrintWriter out = response.getWriter()){
    		try {
    			
    			String email = request.getParameter("email");
    			String password = request.getParameter("password");
    			
    			SellerDao sellerDao = new SellerDao(FactoryProvider.getFactory());
    			Seller seller = sellerDao.getUserByEmailAndPassword(email,password);
    			HttpSession httpSession = request.getSession();
    			
    			if (seller==null) {
    				
    				httpSession.setAttribute("message","Invalid Details! Try with verified one!");
    				response.sendRedirect("baccount.jsp");
    				
    			}else {
    				out.println("<h1>Hello , "+seller.getSellerName() +"</h1>");
    				httpSession.setAttribute("current-seller",seller);
    				response.sendRedirect("bussiness.jsp");
    				
    			}
    			
    			
    			
    			
    		}catch(Exception e) {
    			
    		}
    		
    	}
    		
    		
    		
    	}
    	
    	


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

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

}
