import logging
from typing import Dict

from httpclient import HttpRequest
from httpmessagehandler import HttpMessageHandler

from ._KongRequest import KongRequest
from ._LambdaResponse import LambdaResponse

logger = logging.getLogger()


class KongLambdaMessageHandler:

    def __init__(self, http_message_handler: HttpMessageHandler) -> None:
        self.http_message_handler = http_message_handler

    def handle_messages(self, event: Dict) -> Dict:
        http_request = self._parse_http_request(event)
        http_response = self.http_message_handler.handle_request(request=http_request)
        response = LambdaResponse(
            status=http_response.status_code,
            response=http_response.body,
            headers=http_response.headers,
        )
        return response.response

    @staticmethod
    def _parse_http_request(event):
        request = KongRequest(event)
        http_request = HttpRequest(
            url=request.path,
            method=request.method,
            query=request.args,
            headers=request.headers,
            body=request.data,
        )
        return http_request
