package com.microstar.tap3.contact;

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

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

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

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.lambda.runtime.Context;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.microstar.tap3.CommonTestContext;
//import com.jrestless.aws.gateway.io.DefaultGatewayRequest;
//import com.jrestless.aws.gateway.io.GatewayResponse;
import com.microstar.tap3.contact.api.ContactObject;
import com.microstar.tap3.contact.api.ContactObjectAddress;
import com.microstar.tap3.contact.api.ContactObjectContact;
import com.microstar.tap3.util.TestUtil;

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

	private         ContactLambdaFunctionHandler handler = new ContactLambdaFunctionHandler();

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

        ctx.setFunctionName("TAP3ContactAPI");

        return ctx;
    }

    @Before
    public void setup()
    {
//    	handler = new ContactLambdaFunctionHandler();
    }

    @Test
    public void testPing() throws Exception
    {
    	System.out.println("Entering testPing");
        Context ctx = createContext();
        AwsProxyRequest req = CommonTestContext.getRequest();
        req.setHttpMethod("HEAD");
        req.setPath("/v3/contacts");

        AwsProxyResponse output = handler.handleRequest(req, ctx);
        
        if ( output != null )
        	System.out.println(output.toString());
        
        Assert.assertTrue(output.getStatusCode()==200);
    }

    @Test
    public void testContactCreateAndDelete() throws IOException
    {
    	System.out.println("Entering testContactCreateAndDelete");
        Context ctx = createContext();
        AwsProxyRequest req = CommonTestContext.getRequest();
        req.setHttpMethod("POST");
        
        req.getHeaders().put("Content-Type", "application/json");
        req.setPath("/v3/contacts");
    	
        ContactObject co = new ContactObject();
        ContactObjectContact c = co.createContact(null, "Mr.", "David", "McDaniel", "Jr.", "Test", "3", "", 
        		"dmcdaniel@.com", true, true, true, true, null, null, null, null, 0, null, null);
        c.setCellPhone("7203418008");
        c.setContactTypeId("DEF");
        c.setDefaultForType(false);
        c.setDefaultForType(false);
        c.setDeskPhone("3033300125");
        c.setEmailAddress("DMcDaniel@microstarkegs.com");
        c.setLastName("McDaniel");
        c.setJobTitle("The Man");
        c.setNotes("Notes");
        c.setPrefix(null);
        c.setReceiveAckRequests(true);
        c.setReceiveAckRequestsDelayHrs(10);
        c.setReceiveBill(true);
        c.setReceiveEmptyKegCountRequests(true);
        c.setReceiveInvoice(true);
        c.setReceiveNewsletter(true);
        c.setReceiveOrderUpdates(true);

        ContactObjectAddress a = co.createAddress(null, "Other", "24  Rd.", null, null, "Southborough", "MA", "01772", "US", 1.0, 2.0, null, null, null);
        a.setDefaultForType(true);
        a.setAddressTypeId("DEF");
        co.setContact(c);
        co.setAddress(a);

        ObjectMapper mapper = new ObjectMapper();
        req.setBody(mapper.writeValueAsString(co));
        
        AwsProxyResponse output = handler.handleRequest(req, ctx);
        
        if (output != null) {
            System.out.println(output.toString());
        }
        Assert.assertTrue(output.getStatusCode()==Status.CREATED.getStatusCode());
        co = mapper.readValue(output.getBody(), ContactObject.class);
        
        // Now do the delete
        req.setHttpMethod("DELETE");
        req.setPath("/v3/contacts/" + co.getId());
        
        output = handler.handleRequest(req, ctx);
        if (output != null) {
            System.out.println(output.toString());
        }
        Assert.assertTrue(output.getStatusCode()==200);
        
        System.out.println("Exiting");
    }

    @Test @Ignore
    public void testContactUpdate() throws JsonProcessingException
    {
    	System.out.println("Entering testContactUpdate");
        Context ctx = createContext();
        AwsProxyRequest req = CommonTestContext.getRequest();
        req.setHttpMethod("PUT");
        req.getHeaders().put("Content-Type", "application/json");
        req.setPath("/v3/contacts/1234");
        HashMap<String, String> params = new HashMap<>();
        params.put("id", "1234");
        req.setPathParameters(params);
        ContactObject co = new ContactObject();
        ContactObjectContact c = co.createContact(
        		null, null, "David " + new Date().getSeconds(), null, null, null, null, null, null, false, false, false, false, null, "Notes", true, true, 0, false, false);
        c.setCellPhone("7203418008");
        c.setContactTypeId("DEF");
        c.setDefaultForType(false);
        c.setDefaultForType(false);
        c.setDeskPhone("3033300125");
        c.setEmailAddress("DMcDaniel@microstarkegs.com");
        c.setLastName("McDaniel");
        c.setJobTitle("The Man");
        c.setNotes("Notes");
        c.setPrefix(null);
        c.setReceiveAckRequests(true);
        c.setReceiveAckRequestsDelayHrs(10);
        c.setReceiveBill(true);
        c.setReceiveEmptyKegCountRequests(true);
        c.setReceiveInvoice(true);
        c.setReceiveNewsletter(true);
        c.setReceiveOrderUpdates(true);
        
        ContactObjectAddress a = co.createAddress(null, null, null, null, "Addr3a", null, null, null, null, 0.0, 0.0, null, null, false);
        a.setDefaultForType(false);
        
        co.setContact(c);
        co.setAddress(a);

        ObjectMapper mapper = new ObjectMapper();
        req.setBody(mapper.writeValueAsString(co));
        
        AwsProxyResponse output = handler.handleRequest(req, ctx);
        
        if (output != null) {
            System.out.println(output.toString());
        }
        Assert.assertTrue(output.getStatusCode()==200);
        System.out.println("Exiting");
    }
    
    @Test
    public void testContactGetSingle() {
    	System.out.println("Entering testContactGetSingle");
        Context ctx = createContext();
        AwsProxyRequest req = CommonTestContext.getRequest();
        req.setHttpMethod("GET");
        req.setPath("/v3/contacts/1234");
        HashMap<String, String> params = new HashMap<>();
        params.put("id", "1234");
        req.setPathParameters(params);
        AwsProxyResponse output = handler.handleRequest(req, ctx);

        if (output != null) {
            System.out.println(output.toString());
        }
        Assert.assertTrue(output.getStatusCode()==200);
        System.out.println("Exiting");
    }
    
    @Test
    public void testContactGetByCustId() {
    	System.out.println("Entering testContactGetByCustId");
        Context ctx = createContext();
        AwsProxyRequest req = CommonTestContext.getRequest();
        req.setHttpMethod("GET");
        req.setPath("/v3/contacts");
        HashMap<String, String> params = new HashMap<>();
        params.put("cid", "1234");
        req.setQueryStringParameters(params);
        AwsProxyResponse output = handler.handleRequest(req, ctx);

        if (output != null) {
            System.out.println(output.toString());
        }
        Assert.assertTrue(output.getStatusCode()==200);
        System.out.println("Exiting");
    }

    @Test
    public void testContactGetNonExistent() {
    	System.out.println("Entering testContactGetNonExistent");
        Context ctx = createContext();
        AwsProxyRequest req = CommonTestContext.getRequest();
        req.setHttpMethod("GET");
        req.setPath("/v3/contacts/1235");
        HashMap<String, String> params = new HashMap<>();
        params.put("id", "1235");
        req.setPathParameters(params);
        AwsProxyResponse output = handler.handleRequest(req, ctx);

        if (output != null) {
            System.out.println(output.toString());
        }
        
        Assert.assertTrue(output.getStatusCode()==404);
        System.out.println("Exiting");
    }
}
