from textual.containers import Container, Horizontal, Vertical
from textual.screen import ModalScreen
from textual.widgets import Button, Input, Label


class ConfirmDialog(ModalScreen[bool | None]):
    def __init__(self, message: str, id="confirm_dialog"):
        super().__init__(id=id)
        self.message = message

    def compose(self):
        yield Container(
            Label(self.message, id="dialog_message"),
            Horizontal(
                Button("Yes", id="yes", variant="primary"),
                Button("No", id="cancel"),
                classes="dialog_buttons",
            ),
            id="dialog_container",
        )

    def on_button_pressed(self, event):
        if event.button.id == "cancel":
            self.dismiss(False)
            return
        self.dismiss(event.button.id == "yes")

    def on_screen_resume(self) -> None:
        if getattr(self, "_dismiss_on_resume", False):
            self.dismiss(None)


class BulkRenameConfirmDialog(ModalScreen[bool | None]):
    def __init__(
        self,
        title: str,
        body: str,
    ) -> None:
        super().__init__(id="bulk_rename_confirm_dialog")
        self.dialog_title: str = title or ""
        self.body: str = body or ""

    def compose(self):
        yield Container(
            Label(self.dialog_title, id="bulk_rename_dialog_title"),
            Label(self.body, id="bulk_rename_dialog_message"),
            Horizontal(
                Button("Yes", id="bulk_rename_yes", variant="primary"),
                Button("No", id="bulk_rename_cancel"),
                classes="dialog_buttons",
            ),
            id="bulk_rename_dialog_container",
        )

    def on_button_pressed(self, event):
        if event.button.id == "bulk_rename_cancel":
            self.dismiss(False)
            return
        self.dismiss(event.button.id == "bulk_rename_yes")

    def on_screen_resume(self) -> None:
        if getattr(self, "_dismiss_on_resume", False):
            self.dismiss(None)


class InputDialog(ModalScreen[str | None]):
    def __init__(
        self, message: str, id: str = "input_dialog", default: str | None = None
    ) -> None:
        super().__init__(id=id)
        self._message = message
        self._default = default

    def compose(self):
        yield Vertical(
            Label(self._message, id="dialog_message"),
            Container(
                Input(value=self._default or "", id="input"),
                id="input_container",
            ),
            Horizontal(
                Button("OK", id="ok", variant="primary"),
                Button("Cancel", id="cancel"),
                classes="dialog_buttons",
            ),
            id="dialog_container",
        )

    def on_mount(self) -> None:
        self.query_one(Input).focus()

    def on_button_pressed(self, event: Button.Pressed) -> None:
        if event.button.id == "ok":
            value = self.query_one(Input).value
            self.dismiss(value)
        else:
            self.dismiss(None)
