"""
Password input modal window for sudo authentication.

This module provides a secure password input modal for sudo operations.
"""

from textual.app import ComposeResult
from textual.containers import Vertical
from textual.widgets import Static, Button, Input
from textual.screen import ModalScreen
from textual.binding import Binding

from .styles import PASSWORD_MODAL_CSS


class PasswordModal(ModalScreen):
    """
    Modal screen for secure password input.

    Provides a floating window for entering sudo password with proper masking.
    """

    CSS = PASSWORD_MODAL_CSS

    BINDINGS = [
        Binding("escape", "cancel", "Cancel"),
        Binding("enter", "submit", "Submit"),
    ]

    def __init__(self):
        super().__init__()
        self.error_message = ""

    def compose(self) -> ComposeResult:
        """Create the password modal layout."""
        with Vertical(classes="password-container"):
            yield Static("Sudo Authentication", classes="password-title")

            with Vertical(classes="default-section") as password_input:
                password_input.border_title = "Enter sudo Password"
                yield Input(
                    placeholder="Password",
                    password=True,
                    id="password-input",
                    classes="default-input",
                )

            # Error message placeholder (initially empty)
            yield Static("", id="error-message", classes="error-message")

    def on_mount(self) -> None:
        """Focus the password input when modal opens."""
        password_input = self.query_one("#password-input", Input)
        password_input.focus()

    def on_button_pressed(self, event: Button.Pressed) -> None:
        """Handle button presses."""
        if event.button.id == "ok-button":
            self.action_submit()
        elif event.button.id == "cancel-button":
            self.action_cancel()

    def on_input_submitted(self, event: Input.Submitted) -> None:
        """Handle Enter key in password input field."""
        if event.input.id == "password-input":
            self.action_submit()

    def action_submit(self) -> None:
        """Submit the password and close modal."""
        password_input = self.query_one("#password-input", Input)
        password = password_input.value

        if not password:
            self.show_error("Password cannot be empty")
            return

        # Clear any previous error
        self.clear_error()

        # Return the password
        self.dismiss(password)

    def action_cancel(self) -> None:
        """Cancel password input and close modal."""
        self.dismiss(None)

    def show_error(self, message: str) -> None:
        """Show an error message in the modal."""
        error_static = self.query_one("#error-message", Static)
        error_static.update(message)
        # Keep focus on password input
        password_input = self.query_one("#password-input", Input)
        password_input.focus()

    def clear_error(self) -> None:
        """Clear the error message."""
        error_static = self.query_one("#error-message", Static)
        error_static.update("")
