from typing import Optional, override

from flut.dart.ui import TextAlign
from flut.flutter.material.input_decorator import InputDecoration
from flut.flutter.widgets.framework import Widget


class TextField(Widget):
    def __init__(
        self,
        *,
        key=None,
        controller=None,
        focusNode=None,
        decoration: Optional[InputDecoration] = InputDecoration(),
        style=None,
        textAlign: TextAlign = TextAlign.start,
        readOnly: bool = False,
        autofocus: bool = False,
        obscureText: bool = False,
        maxLines: Optional[int] = 1,
        minLines: Optional[int] = None,
        maxLength: Optional[int] = None,
        onChanged=None,
        onSubmitted=None,
        enabled: Optional[bool] = None,
    ):
        super().__init__("TextField", key=key)
        self.controller = controller
        self.focusNode = focusNode
        self.decoration = decoration
        self.style = style
        self.textAlign = textAlign
        self.readOnly = readOnly
        self.autofocus = autofocus
        self.obscureText = obscureText
        self.maxLines = maxLines
        self.minLines = minLines
        self.maxLength = maxLength
        self.onChanged = onChanged
        self.onSubmitted = onSubmitted
        self.enabled = enabled

    @override
    def _flut_pack(self) -> dict:
        result = self._flut_base_props()
        if self.controller is not None:
            result["controller"] = self.controller._flut_pack()
        if self.focusNode is not None:
            result["focusNode"] = self.focusNode._flut_pack()
        if self.decoration is not None:
            result["decoration"] = self.decoration._flut_pack()
        if self.style is not None:
            result["style"] = self.style._flut_pack()
        result["textAlign"] = self.textAlign._flut_pack()
        result["readOnly"] = self.readOnly
        result["autofocus"] = self.autofocus
        result["obscureText"] = self.obscureText
        if self.maxLines is not None:
            result["maxLines"] = self.maxLines
        if self.minLines is not None:
            result["minLines"] = self.minLines
        if self.maxLength is not None:
            result["maxLength"] = self.maxLength
        if self.onChanged is not None:
            result["onChangedId"] = self._register_action(self.onChanged)
        if self.onSubmitted is not None:
            result["onSubmittedId"] = self._register_action(self.onSubmitted)
        if self.enabled is not None:
            result["enabled"] = self.enabled
        return result

