from typing import (
    Dict,
    Union,
)


class HTMLTextFormatter(object):

    def __init__(
        self,             # type: HTMLTextFormatter
        customization,    # type: Dict[str, Union[str, bool, int, float]]
        columns,          # type: int
    ):
        # type: (...) -> None

        self.columns = columns
        self.customization = customization
        self.style = {
            "font-family": "monospace",
            "font-size": "1.3em",
        }

    def format(
        self,    # type: HTMLTextFormatter
        text,    # type: str
    ):
        # type: (...) -> str

        if self.customization is None:
            return self._make_tag(text=text)

        formatted_text = self._handle_align(text=text)
        self._handle_bold()

        return self._make_tag(text=formatted_text)

    def _handle_bold(
        self,
    ):
        # type: (...) -> None

        if self.customization.get("bold"):
            self.style["font-weight"] = "bold"

    def _make_tag(
        self,    # type: HTMLTextFormatter
        text,    # type: str
    ):
        # type: (...) -> str

        class_attributes = ";".join(["{}: {}".format(key, value) for key, value in self.style.items()])

        return u"<span style='{}'>{}</span>".format(
            class_attributes,
            text.replace(" ", "&nbsp;"),
        )

    def _handle_align(
        self,    # type: HTMLTextFormatter
        text,    # type: str
    ):
        # type: (...) -> str

        formatted_text = text
        align = self.customization.get("align", "center")
        diff = self.columns - len(text)
        if align == "left":
            formatted_text = (" " * diff) + text
        elif align == "right":
            formatted_text = text + (" " * diff)
        elif align == "center":
            half = int(diff / 2)
            formatted_text = (" " * half) + text + (" " * half)

        return formatted_text
