package com.etherios.gae.connector.tank;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.etherios.gae.connector.tank.datastore.UserData;
import com.etherios.gae.connector.tank.datastore.UserDataUtils;

@SuppressWarnings("serial")
public class BaseServlet extends HttpServlet {
	// Constants.
	// Errors.
	public static final String ERROR_INVALID_USER = "Invalid user or password.";
	public static final String ERROR_EMPTY_USER = "User and Password cannot be empty.";
	public static final String ERROR_SESSION_EXPIRED = "Your session has expired, plese log in again.";
	public static final String ERROR_NOT_LOGGED = "You need to login first.";
	
	public static final String SESSION_USER = "UserName";
	public static final String SESSION_USER_ID = "UserID";
	
	// Pages.
	public static final String PAGE_LOGIN = "/login.jsp";
	public static final String PAGE_TANK = "/tank.jsp";
	public static final String PAGE_DEVICES = "/devices.jsp";
	public static final String PAGE_DEVICE_INFO = "/device.jsp";
	
	protected static final String PAGE_TANK_SERVLET = "/tank";
	protected static final String PAGE_LOGIN_SERVLET = "/login";
	protected static final String PAGE_DEVICES_SERVLET = "/devices";
	
	// Variables.
	protected String errorMessage = "";
	protected RequestDispatcher loginDispatcher;
	protected RequestDispatcher tanksDispatcher;
	protected RequestDispatcher devicesDispatcher;
	protected RequestDispatcher deviceInfoDispatcher;
	
	/*
	 * (non-Javadoc)
	 * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		ServletContext ctx = config.getServletContext();
		loginDispatcher = ctx.getRequestDispatcher(PAGE_LOGIN);
		devicesDispatcher = ctx.getRequestDispatcher(PAGE_DEVICES);
		deviceInfoDispatcher = ctx.getRequestDispatcher(PAGE_DEVICE_INFO);
		tanksDispatcher = ctx.getRequestDispatcher(PAGE_TANK);
	}
	
	/**
	 * Returns the UserData from the Data Storage based on the key saved in 
	 * the session cookie.
	 * 
	 * @param req The request to take the session cookie from.
	 * @return The UserData object from the Data Storage. Null if there was any error.
	 */
	protected UserData getUserFromSession(HttpServletRequest req) {
		clearErrorMessage();
		
		UserData user = null;
		//String sessionID = null;
		String userID = null;
		
		HttpSession session = req.getSession(false);
		if (session == null) {
			errorMessage = ERROR_SESSION_EXPIRED;
			return null;
		}
		
		userID = (String)session.getAttribute(SESSION_USER_ID);
		if (userID == null) {
			errorMessage = ERROR_NOT_LOGGED;
			return null;
		}
		
		// Get the user from the data store.
		user = UserDataUtils.getUserFromDataStore(userID);
		
		// TO_DO: It would be nice to add here a checker to avoid cross site 
		//       request forgeries (csrf). The UserData model should include a 
		//       csrfId value in the Data Storage to do that.
		
		return user;
		
	}
	
	/**
	 * Returns the error message generated.
	 * 
	 * @return The error message.
	 */
	public String getErrorMessage() {
		return errorMessage;
	}
	
	/**
	 * Clears the error message.
	 */
	public void clearErrorMessage() {
		errorMessage = "";
	}
}
