package com.microstar.tap3;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.Response.Status;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.serverless.proxy.internal.model.ApiGatewayRequestContext;
import com.amazonaws.serverless.proxy.internal.model.ApiGatewayRequestIdentity;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse;
import com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient;
import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProvider;
import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProviderClientBuilder;
import com.amazonaws.services.cognitoidp.model.AdminGetUserRequest;
import com.amazonaws.services.cognitoidp.model.AdminGetUserResult;
import com.amazonaws.services.cognitoidp.model.GetUserRequest;
import com.amazonaws.services.lambda.runtime.Context;
import com.microstar.tap3.login.LoginLambdaFunctionHandler;
import com.microstar.tap3.login.LoginResourceImpl;
import com.microstar.tap3.util.TestUtil;


/**
 * A simple test harness for locally invoking your Lambda function handler.
 */
public class LoginLambdaFunctionHandlerTest {

    private static AwsProxyRequest input;
    private static LoginLambdaFunctionHandler handler = new LoginLambdaFunctionHandler();


    @BeforeClass
    public static void createInput() throws IOException {
        // TODO: set up your sample input object here.
        input = null;
    }

    private Context createContext() {
        CommonTestContext ctx = new CommonTestContext();

        // TODO: customize your context here if needed.
        ctx.setFunctionName("LoginAPI");

        return ctx;
    }

    @Test @Ignore
    public void testTest()
    {
    	try
    	{
		AWSCognitoIdentityProvider idpClient = AWSCognitoIdentityProviderClientBuilder.defaultClient();
//		idpClient.setRegion(Region.getRegion(Regions.US_WEST_2));
		String userPoolId=  "us-west-";
		AdminGetUserRequest arg0 = new AdminGetUserRequest()
				.withUsername("david@-3.com").withUserPoolId(userPoolId);
		AdminGetUserResult uRes = idpClient.adminGetUser(arg0);
		uRes.getSdkHttpMetadata();
    	}
    	catch ( Exception ex )
    	{
    		ex.printStackTrace();
    	}
    }
    
    
    @Test @Ignore
    public void testLoginLambdaFunctionHandler() {
        Context ctx = createContext();
        
        input = CommonTestContext.getRequest();

//        LoginResourceImpl.setup();
        input.setHttpMethod("POST");
        HashMap<String,String> headers = new HashMap<String, String>();
        headers.put("Accept", "application/json");
        headers.put("Content-Type", "application/json");
        input.setHeaders(headers);
        input.setPath("/login");
        
        Map<String, String> stageVariables = new HashMap<String, String>();
        stageVariables.put("apiURL", "https://api.foobar.com/v3-dev");
		input.setStageVariables(stageVariables);
        
        String body = "{\"username\":\"david@.com\",\"password\":\"fffff\"}";
        body = "{\"username\":\"dmcdaniel@aa.com.test\",\"password\":\"aa!!\"}";
		input.setBody(body);

        AwsProxyResponse output = handler.handleRequest(input, ctx);

        Assert.assertEquals(Status.OK.getStatusCode(), output.getStatusCode());
        
        // TODO: validate output here if needed.
        if (output != null) {
            System.out.println(output.getBody().toString());
        }
    }

    @Test @Ignore
    public void testChangePw() {
        Context ctx = createContext();
        
        input = CommonTestContext.getRequest();

//        LoginResourceImpl.setup();
        input.setHttpMethod("PUT");
        HashMap<String,String> headers = new HashMap<String, String>();
        headers.put("Accept", "application/json");
        headers.put("Content-Type", "application/json");
        input.setHeaders(headers);
        input.setPath("/login/nnnn");
        
        Map<String, String> stageVariables = new HashMap<String, String>();
        stageVariables.put("apiURL", "https://api.foobar.com/v3-dev");
		input.setStageVariables(stageVariables);
        
        String body = "{\"username\":\"david@.com\",\"password\":\"aa\"}";
        body = "{\"username\":\"dmcdaniel@aa.com.test\",\"password\":\"aa\"}";
		input.setBody(body);

        AwsProxyResponse output = handler.handleRequest(input, ctx);

        Assert.assertEquals(Status.OK.getStatusCode(), output.getStatusCode());
        
        // TODO: validate output here if needed.
        if (output != null) {
            System.out.println(output.getBody().toString());
        }
    }

}
