package zall.action;

import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import zall.bean.User;
import zall.util.msg.UserMessage;
import zall.util.msg.UserMessage.MessageType;
import zutil.db.DBConnection;
import zutil.log.LogUtil;

public class LoginAction extends ZalleryAction{

	private static final Logger logger = LogUtil.getLogger();

	public LoginAction() {
		super("login");
	}
	

	@Override
	public void handleRequest(DBConnection db, HttpServletRequest request, HttpServletResponse response, HttpSession session,
			PrintWriter out, User user, UserMessage msgs) throws SQLException {

		user = User.load(request, response, db, request.getParameter("email"), request.getParameter("password") );
		if( user != null ){
			logger.info("Used user input to auth \""+user.getName()+"\"");
			session.setAttribute("user", user);
			if( out != null )
				out.println("{ }");
		}
		else if(out != null)	out.println("{ \"error\":\"Wrong username or password!\"}");
		else					msgs.add(MessageType.ERROR, "Wrong username or password!");

	}

}
