package eu.dnetlib.enabling.ui;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.servlet.VelocityViewServlet;
import org.springframework.beans.factory.annotation.Required;

import eu.dnetlib.enabling.ui.server.auth.AuthenticationManager;
import eu.dnetlib.enabling.ui.server.session.SessionManager;

/**
 * Main Servlet.
 *
 * @author michele
 *
 */
public abstract class MainServlet extends VelocityViewServlet {

	/**
	 * serializable
	 */
	private static final long serialVersionUID = 2751728218621288364L;

	/**
	 * GWT module
	 */
	protected String gwtModule;

	/**
	 * Page title
	 */
	protected String title;

	/**
	 * template path
	 */
	protected String template;


	/**
	 * Session Manager
	 */
	protected SessionManager sessionManager;

	/**
	 * Auth manager.
	 */
	protected AuthenticationManager authManager;

	/**
	 * velocity properties.
	 */
	private final ExtendedProperties velocityProperties;


	public MainServlet() throws ServletException {
		super();
		velocityProperties = new ExtendedProperties();
		velocityProperties.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem");
		velocityProperties.setProperty(Velocity.RESOURCE_LOADER, "class");
		velocityProperties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
		init();
	}
	/**
	 * Obtains the singleton instance and lazily instantiates the oai servlet.
	 *
	 * @return oai servlet
	 * @throws ServletException
	 *             could happen
	 */
/*	public static MainServlet getInstance() throws ServletException {
		if (instance == null)
			instance = new MainServlet();
		return instance;
	}*/

	/**
	 * {@inheritDoc}
	 *
	 * @see org.apache.velocity.tools.view.servlet.VelocityViewServlet#loadConfiguration(javax.servlet.ServletConfig)
	 */
	@Override
	protected ExtendedProperties loadConfiguration(final ServletConfig config) {
		return velocityProperties;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.apache.velocity.tools.view.servlet.VelocityViewServlet#setContentType(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void setContentType(final HttpServletRequest request, final HttpServletResponse response) {
		response.setContentType("text/html; charset=utf-8");
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.apache.velocity.tools.view.servlet.VelocityViewServlet#handleRequest(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, org.apache.velocity.context.Context)
	 */
	abstract public Template handleRequest(final HttpServletRequest request, final HttpServletResponse response, final Context context) throws Exception;


	public boolean isSessionExpired(HttpServletRequest request) {
		String sessionId = "";
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie c : cookies) {
				if (c.getName().equals("session")) {
					sessionId =  c.getValue();
				}
			}
		}

		return ((sessionId == "")||(sessionManager.isValidSession(sessionId)));
	}


	public String getGwtModule() {
		return gwtModule;
	}

	@Required
	public void setGwtModule(String gwtModule) {
		this.gwtModule = gwtModule;
	}

	public String getTitle() {
		return title;
	}

	@Required
	public void setTitle(String title) {
		this.title = title;
	}

	public SessionManager getSessionManager() {
		return sessionManager;
	}

	@Required
	public void setSessionManager(SessionManager sessionManager) {
		this.sessionManager = sessionManager;
	}

	public String getTemplate() {
		return template;
	}

	@Required
	public void setTemplate(String template) {
		this.template = template;
	}

	public AuthenticationManager getAuthManager() {
		return authManager;
	}

	@Required
	public void setAuthManager(AuthenticationManager authManager) {
		this.authManager = authManager;
	}

}
