from typing import Dict, Union, Optional

from ._DataType import DataType
from ._DefaultToken import DefaultToken
from ._Message import Message


class HttpResponseMessage(Message):
    def __init__(self, status_code, body=None, headers=None):
        # type: (int, Optional[Union[str, bytes]], Dict[str, str]) -> None
        self.status_code = status_code
        self.body = body
        self.http_headers = headers if headers is not None else {}

        data = str(self.status_code).encode("utf-8") + b"\0" + str(len(self.http_headers)).encode("utf-8") + b"\0"
        for header in self.http_headers.keys():
            data += header.encode("utf-8") + b"\0" + self.http_headers[header].encode("utf-8") + b"\0"

        if body is None:
            data += b""
        else:
            body = self.body
            if isinstance(body, str):
                body = body.encode("utf-8")
            data += body

        super(HttpResponseMessage, self).__init__(DefaultToken.TK_HTTP_MSG, DataType.http_response, data)
