package com.microstar.tap3.api;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;

import com.microstar.tap3.common.JWTPayload;
import com.microstar.tap3.common.JWTUtils;
import com.microstar.tap3.util.ConfigSingleton;

import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.internal.model.CognitoAuthorizerClaims;
import com.amazonaws.serverless.proxy.jersey.JerseyLambdaContainerHandler;
import com.amazonaws.serverless.proxy.jersey.factory.AwsProxyServletContextFactory;
import com.amazonaws.serverless.proxy.jersey.factory.AwsProxyServletRequestFactory;
import com.amazonaws.services.lambda.runtime.Context;
import com.fasterxml.jackson.databind.ObjectMapper;

public abstract class BaseLambdaFunctionHandler 
{
	public static Logger LOG = null;
    protected ResourceConfig jerseyApplication = null;
    protected JerseyLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
    protected ObjectMapper mapper = new ObjectMapper();

	static
	{
		System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
	}

	public BaseLambdaFunctionHandler()
	{
	}

	public BaseLambdaFunctionHandler(String thisPackage)
	{
		LOG.info("Base: Initializing with package " + thisPackage);
		try
		{
			jerseyApplication = new ResourceConfig()
					.packages(thisPackage + ",com.fasterxml.jackson.jaxrs.json")
					.register(new AbstractBinder() {
						
						@Override
						protected void configure() {
							bindFactory(AwsProxyServletRequestFactory.class)
								.to(HttpServletRequest.class)
								.in(RequestScoped.class);
							bindFactory(AwsProxyServletContextFactory.class)
								.to(ServletContext.class)
								.in(RequestScoped.class);
						}
					});
			handler = JerseyLambdaContainerHandler.getAwsProxyHandler(jerseyApplication);
		}
		catch ( Exception ex )
		{
			LOG.info("Caught Exception in BaseLambdaFunctionHandler: " + ex.getMessage());
			ex.printStackTrace();
		}
	}

    public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context) 
    {
    	try
    	{
	        AwsProxyResponse resp = handler.proxy(awsProxyRequest, context);
	        LOG.info("handleRequest::Response::statusCode = " + resp.getStatusCode());
	        LOG.info("handleRequest::Response::headers = " + resp.getHeaders());
	        LOG.info("handleRequest::Response::body = " + resp.getBody());
	        LOG.info("handleRequest::Clearing Identity");
	        return resp;
    	}
    	catch ( Exception ex )
    	{
    		LOG.info("handleRequest::caught exception: " + ex.getMessage());
    		ex.printStackTrace();
    		return new AwsProxyResponse(501);
    	}
    }
    

}
