"""Header widget for Lettera."""

from datetime import datetime
from textual.widgets import Static
from textual.reactive import reactive


class HeaderBar(Static):
    """Header bar with app name, writing state, and current time."""

    current_time = reactive("")
    entry_name = reactive("untitled")
    is_locked = reactive(False)
    is_saved = reactive(True)

    def __init__(self, show_state: bool = False, *args, **kwargs):
        """Initialize header bar."""
        self._show_state = show_state
        super().__init__(*args, **kwargs)
        self.update_time()

    def on_mount(self) -> None:
        """Set up timer to update time."""
        self.set_interval(1.0, self.update_time)

    def update_time(self) -> None:
        """Update the current time display."""
        self.current_time = datetime.now().strftime("%H:%M:%S")
        self.refresh()

    def render(self) -> str:
        """Render the header with optional writing state."""
        if not self._show_state:
            return f"Lettera \u2014 {self.current_time}"

        mode_color = "#4a9eff" if self.is_locked else "#666666"
        mode_label = "locked" if self.is_locked else "draft"

        if self.is_saved:
            save_markup = "[#666666]saved[/#666666]"
        else:
            save_markup = "[#e06c75]unsaved[/#e06c75]"

        return (
            f"Lettera  \u00b7  "
            f"[{mode_color}]{mode_label}[/{mode_color}]"
            f" \u00b7 {save_markup}"
            f" \u00b7 [#888888]{self.entry_name}[/#888888]"
            f"  \u00b7  [#4a9eff]{self.current_time}[/#4a9eff]"
        )
