package io.kemper;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.kemper.api.*;
import io.kemper.domain.Riddle;
import io.kemper.service.RiddleService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GetRiddleHandler implements RequestHandler<Map<String, Object>, Response> {

    @Override
    public Response handleRequest(Map<String, Object> input, Context context) {
        context.getLogger().log("Input: " + input);

        //TODO: get riddle
        Riddle riddle = new RiddleService().getRandomRiddle();

        Action getAnswerAction = new Action("answer", "Get Answer", "button", riddle.getId().toString());
        List<Action> actions = new ArrayList<>();
        actions.add(getAnswerAction);

        Attachment getRiddleAttachment = new Attachment("...", "Unable to get answer", "mycallbackid", "#00FF00", "default", actions);
        List<Attachment> attachments = new ArrayList<>();
        attachments.add(getRiddleAttachment);
        SlackResponse slackResponse = new SlackResponse(ResponseType.in_channel, riddle.getQuestion(), attachments);

        String body = marshall(slackResponse);

        Response response = new Response(body, 200);
        System.out.println(response);
        return response;
    }


    public static String marshall(Object obj) {
        ObjectMapper mapper = new ObjectMapper();
        String body = null;
        try {
            body = mapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return body;
    }

}

@@ -352,45 +352,67 @@
 ava.
-util.ArrayList;%0Aimport java.util.List
+io.UnsupportedEncodingException;%0Aimport java.net.URLDecoder
 ;%0Aim
@@ -426,19 +426,17 @@
 va.util.
-Map
+*
 ;%0A%0Apubli
@@ -486,35 +486,34 @@
 Handler%3C
-Map%3CString, Object%3E
+LambdaProxyRequest
 , Respon
@@ -571,32 +571,33 @@
 est(
-Map%3CString, Object%3E inpu
+LambdaProxyRequest reques
 t, C
@@ -662,43 +662,336 @@
 %22 + 
-input);%0A%0A        //TODO: get riddle
+request);%0A        Map%3CString, String%3E requestBody = parseRequestBody(request.body);%0A        System.out.println(requestBody);%0A%0A        if(%22help%22.equals(requestBody.get(%22text%22))) %7B%0A            return getHelp();%0A        %7D%0A%0A        return getRiddle(requestBody.get(%22text%22));%0A%0A    %7D%0A%0A    private Response getRiddle(String category) %7B
 %0A   
@@ -1774,16 +1774,874 @@
 %0A    %7D%0A%0A
+    private Response getHelp() %7B%0A        SlackResponse slackResponse = new SlackResponse(ResponseType.ephemeral, %22Type %60/riddle%60 to get a random riddle.%22, null);%0A        String body = marshall(slackResponse);%0A        Response response = new Response(body, 200);%0A        return response;%0A    %7D%0A%0A    public Map%3CString, String%3E parseRequestBody(String body) %7B%0A        try %7B%0A            body = URLDecoder.decode(body, %22UTF-8%22);%0A        %7D catch (UnsupportedEncodingException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        Map%3CString, String%3E requestBody = new HashMap%3C%3E();%0A%0A        String%5B%5D keypairs = body.split(%22&%22);%0A        for(String keypair : keypairs) %7B%0A            String%5B%5D temp = keypair.split(%22=%22);%0A            if(temp.length == 2) %7B%0A                requestBody.put(temp%5B0%5D, temp%5B1%5D);%0A            %7D%0A        %7D%0A%0A        return requestBody;%0A    %7D%0A
 %0A    pub

add help subcommand