from typing import Optional, override

from .framework import Widget
from ..painting.text_style import TextStyle
from ..painting.text_painter import TextOverflow


class Text(Widget):
    def __init__(
        self,
        data: Optional[str] = None,
        *,
        key=None,
        style: Optional[TextStyle] = None,
        textAlign=None,
        softWrap: Optional[bool] = None,
        maxLines: Optional[int] = None,
        overflow: Optional[TextOverflow] = None,
        textSpan=None,
    ):
        super().__init__("Text", key=key)
        self.data = data
        self.style = style
        self.textAlign = textAlign
        self.softWrap = softWrap
        self.maxLines = maxLines
        self.overflow = overflow
        self.textSpan = textSpan

    @classmethod
    def rich(
        cls,
        textSpan,
        *,
        key=None,
        style: Optional[TextStyle] = None,
        textAlign=None,
        softWrap: Optional[bool] = None,
        maxLines: Optional[int] = None,
        overflow: Optional[TextOverflow] = None,
    ):
        return cls(
            key=key,
            style=style,
            textAlign=textAlign,
            softWrap=softWrap,
            maxLines=maxLines,
            overflow=overflow,
            textSpan=textSpan,
        )

    @override
    def _flut_pack(self) -> dict:
        result = self._flut_base_props()
        if self.data is not None:
            result["data"] = self.data
        if self.textSpan is not None:
            result["textSpan"] = self.textSpan._flut_pack()
        if self.style is not None:
            result["style"] = self.style._flut_pack()
        if self.textAlign is not None:
            result["textAlign"] = self.textAlign._flut_pack()
        if self.softWrap is not None:
            result["softWrap"] = self.softWrap
        if self.maxLines is not None:
            result["maxLines"] = self.maxLines
        if self.overflow is not None:
            result["overflow"] = self.overflow._flut_pack()
        return result
