"""Card widgets for ganban UI."""

from textual.app import ComposeResult
from textual.geometry import Offset
from textual.message import Message
from textual.widgets import Static

from ganban.model.node import Node
from ganban.model.writer import create_card
from ganban.ui.detail import CardDetailModal
from ganban.ui.drag import DraggableMixin, DragStarted
from ganban.ui.menu import ContextMenu, MenuItem, MenuSeparator
from ganban.ui.edit import EditableText, TextEditor
from ganban.ui.static import PlainStatic


def _card_title(board: Node, card_id: str) -> str:
    """Get the display title for a card."""
    card = board.cards[card_id]
    if card and card.sections:
        keys = card.sections.keys()
        if keys:
            return keys[0]
    return card_id


class CardWidget(DraggableMixin, Static):
    """A single card in a column."""

    class MoveRequested(Message):
        """Posted when card should be moved to another column."""

        def __init__(self, card: "CardWidget", target_column: str):
            super().__init__()
            self.card = card
            self.target_column = target_column

    class DeleteRequested(Message):
        """Posted when card should be deleted."""

        def __init__(self, card: "CardWidget"):
            super().__init__()
            self.card = card

    DEFAULT_CSS = """
    CardWidget {
        width: 100%;
        height: auto;
        padding: 0 1;
        border: solid $primary-lighten-2;
        background: $surface;
    }
    CardWidget:hover {
        border: solid $primary;
    }
    CardWidget.dragging {
        display: none;
    }
    """

    def __init__(self, card_id: str, board: Node):
        Static.__init__(self)
        self._init_draggable()
        self.card_id = card_id
        self.board = board
        self.title = _card_title(board, card_id)

    def compose(self) -> ComposeResult:
        yield PlainStatic(self.title or self.card_id)

    def draggable_drag_started(self, mouse_pos: Offset) -> None:
        self.post_message(DragStarted(self, mouse_pos))

    def draggable_clicked(self, click_pos: Offset) -> None:
        card = self.board.cards[self.card_id]
        if card:
            self.app.push_screen(CardDetailModal(card), self._on_modal_closed)

    def _on_modal_closed(self, result: None) -> None:
        self.title = _card_title(self.board, self.card_id)
        self.query_one(PlainStatic).update(self.title or self.card_id)

    def _find_column(self) -> Node | None:
        """Find the column containing this card."""
        for col in self.board.columns:
            if self.card_id in (col.links or []):
                return col
        return None

    def on_click(self, event) -> None:
        if event.button == 3:  # Right click
            event.stop()
            current_col = self._find_column()

            # Build move submenu from visible columns (current disabled)
            move_items = [
                MenuItem(col.name, f"move:{col.name}", disabled=(col is current_col))
                for col in self.board.columns
                if not col.hidden
            ]

            items = [
                MenuItem("Edit", "edit"),
                MenuItem("Move to", submenu=move_items),
            ]
            items.append(MenuSeparator())
            items.append(MenuItem("Delete", "delete"))

            menu = ContextMenu(items, event.screen_x, event.screen_y)
            self.app.push_screen(menu, self._on_menu_closed)

    def _on_menu_closed(self, item: MenuItem | None) -> None:
        if item is None:
            return
        match item.item_id:
            case "edit":
                card = self.board.cards[self.card_id]
                if card:
                    self.app.push_screen(CardDetailModal(card), self._on_modal_closed)
            case "delete":
                self._delete_card()
            case s if s and s.startswith("move:"):
                col_name = s[5:]
                self._move_to_column(col_name)

    def _delete_card(self) -> None:
        """Request deletion of this card."""
        self.post_message(self.DeleteRequested(self))

    def _move_to_column(self, col_name: str) -> None:
        """Request move to the named column."""
        self.post_message(self.MoveRequested(self, col_name))


class AddCard(Static):
    """Widget to add a new card to a column."""

    class CardCreated(Message):
        """Posted when a new card is created."""

        def __init__(self, column: Node, card_id: str, title: str):
            super().__init__()
            self.column = column
            self.card_id = card_id
            self.title = title

    DEFAULT_CSS = """
    AddCard {
        width: 100%;
        height: auto;
        padding: 0 1;
        margin-bottom: 1;
        border: dashed $surface-lighten-2;
    }
    AddCard > EditableText > ContentSwitcher > Static {
        text-align: center;
        color: $text-muted;
    }
    """

    def __init__(self, column: Node, board: Node):
        super().__init__()
        self.column = column
        self.board = board

    def compose(self) -> ComposeResult:
        yield EditableText("", Static("+"), TextEditor(), placeholder="+")

    def on_click(self, event) -> None:
        if event.button == 3:
            event.stop()

    def on_editable_text_changed(self, event: EditableText.Changed) -> None:
        event.stop()
        if event.new_value:
            card_id, card = create_card(self.board, event.new_value, column=self.column)
            self.post_message(self.CardCreated(self.column, card_id, event.new_value))
        self.query_one(EditableText).value = ""
