from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING

import chess

from textual.app import App, ComposeResult
from textual.binding import Binding

if TYPE_CHECKING:
    pass


class ChessApp(App):
    """Chess CLI — Textual TUI application."""

    CSS_PATH = Path("styles/app.tcss")
    TITLE = "Chess CLI"

    BINDINGS = [
        Binding("ctrl+c", "quit", "Quit", show=False),
    ]

    def __init__(
        self,
        start_screen: str = "menu",
        color: chess.Color | None = None,
        difficulty: str = "intermediate",
        pgn_file: str | None = None,
        threads: int = 1,
        hash_mb: int = 16,
        move_time: float | None = None,
    ) -> None:
        super().__init__()
        self.start_screen = start_screen
        self.initial_color = color
        self.initial_difficulty = difficulty
        self.pgn_file = pgn_file
        self.initial_threads = threads
        self.initial_hash_mb = hash_mb
        self.initial_move_time = move_time

    def on_mount(self) -> None:
        if self.start_screen == "game" and self.initial_color is not None:
            from chess_cli.tui.screens.game import GameScreen
            self.push_screen(GameScreen(
                color=self.initial_color,
                difficulty=self.initial_difficulty,
                threads=self.initial_threads,
                hash_mb=self.initial_hash_mb,
                move_time=self.initial_move_time,
            ))
        elif self.start_screen == "analyze" and self.pgn_file:
            self._start_analyze()
        else:
            from chess_cli.tui.screens.menu import MenuScreen
            self.push_screen(MenuScreen())

    def _start_analyze(self) -> None:
        """Start analysis of a PGN file."""
        import chess.pgn as pgn_mod
        from chess_cli.engine import EnginePlayer, EngineNotFoundError
        from chess_cli.tui.screens.analysis import AnalysisScreen

        try:
            with open(self.pgn_file) as f:
                pgn_game = pgn_mod.read_game(f)
        except FileNotFoundError:
            self.notify(f"File not found: {self.pgn_file}", severity="error")
            from chess_cli.tui.screens.menu import MenuScreen
            self.push_screen(MenuScreen())
            return
        except Exception as e:
            self.notify(f"Error reading PGN: {e}", severity="error")
            from chess_cli.tui.screens.menu import MenuScreen
            self.push_screen(MenuScreen())
            return

        if pgn_game is None:
            self.notify("No game found in PGN file.", severity="error")
            from chess_cli.tui.screens.menu import MenuScreen
            self.push_screen(MenuScreen())
            return

        try:
            engine = EnginePlayer(difficulty="maximum")
        except EngineNotFoundError as e:
            self.notify(str(e), severity="error")
            from chess_cli.tui.screens.menu import MenuScreen
            self.push_screen(MenuScreen())
            return

        self.push_screen(AnalysisScreen(pgn_game=pgn_game, engine=engine))
