"""No-repository fallback page."""

from __future__ import annotations

from pathlib import Path

from pygittools.tui.draw import draw_line
from pygittools.tui.pages.help import help_page
from pygittools.tui.types import PageAction, PageContext, PageResult
from pygittools.tui.ucurses import A_BOLD, KEY_RESIZE  # type: ignore[import-untyped]


class NoRepoPage:
	title = "pgt"

	def __init__(self, start: Path) -> None:
		self._start = start

	def on_enter(self, ctx: PageContext) -> None:
		del ctx

	def status_text(self) -> str:
		return "no repository"

	def draw(self, stdscr: int, height: int, width: int) -> None:
		del stdscr
		lines: list[tuple[str, int]] = [
			("pgt", A_BOLD),
			("", 0),
			(f"No git repository found under {self._start}", 0),
			("", 0),
			("Run pgt from inside a repository, or pass --repo PATH.", 0),
			("", 0),
		]
		for row, (text, attr) in enumerate(lines):
			if row >= height:
				break
			draw_line(row, 0, text, width, attr)

	def handle_key(self, key: int) -> PageResult:
		if key in (ord("q"), ord("Q")):
			return PageResult(action=PageAction.QUIT)
		if key in (ord("h"), ord("H")):
			return PageResult(action=PageAction.PUSH, next_page=help_page())
		if key == KEY_RESIZE:
			return PageResult()
		return PageResult()