"""Menu bar — File, Camera, Video format, Image, Model and Debug controls."""

from __future__ import annotations

import logging

from PySide6.QtCore import Signal
from PySide6.QtGui import QAction, QActionGroup
from PySide6.QtWidgets import QFileDialog, QMenuBar, QWidget

from app.camera.camera_enumerator import CameraFormat, CameraInfo
from app.config import MODEL_FILE_EXTENSIONS, VIDEO_FILE_EXTENSIONS
from app.logging_setup import set_console_level

logger = logging.getLogger(__name__)


class AppMenuBar(QMenuBar):
    """
    Application menu bar.

    Signals:
        video_file_selected(str)    — user picked a video file path
        video_closed()              — user chose to close video and return to camera
        model_file_selected(str)    — user picked a .pt model file path
        inference_toggled(bool)     — user toggled inference on/off
        camera_selected(CameraInfo)
        format_selected(CameraFormat)
        reconnect_requested()
        overlay_toggled(bool)
        log_toggled(bool)
        camera_settings_requested()
    """

    # File / video
    video_file_selected = Signal(str)
    video_closed = Signal()

    # Model / inference
    model_file_selected = Signal(str)
    inference_toggled = Signal(bool)

    # Camera
    camera_selected = Signal(object)        # CameraInfo
    format_selected = Signal(object)        # CameraFormat
    reconnect_requested = Signal()

    # View / debug
    overlay_toggled = Signal(bool)
    log_toggled = Signal(bool)
    camera_settings_requested = Signal()

    def __init__(self, parent: QWidget | None = None) -> None:
        super().__init__(parent)

        self._camera_group: QActionGroup | None = None
        self._format_group: QActionGroup | None = None
        self._cameras: list[CameraInfo] = []

        self._build_menus()

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    def populate_cameras(self, cameras: list[CameraInfo]) -> None:
        self._cameras = cameras
        menu = self._camera_menu

        for action in list(menu.actions()):
            if action not in (self._reconnect_action, self._cam_separator):
                menu.removeAction(action)

        self._camera_group = QActionGroup(self)
        self._camera_group.setExclusive(True)

        for cam in cameras:
            action = QAction(cam.name, self)
            action.setCheckable(True)
            action.setData(cam)
            self._camera_group.addAction(action)
            menu.insertAction(self._cam_separator, action)
            action.triggered.connect(self._on_camera_action)

        if cameras:
            self._camera_group.actions()[0].setChecked(True)

    def populate_formats(self, camera_info: CameraInfo) -> None:
        self._populate_format_menu(camera_info)

    def set_active_camera(self, camera_info: CameraInfo) -> None:
        if self._camera_group is None:
            return
        for action in self._camera_group.actions():
            if action.data() is camera_info:
                action.setChecked(True)
                return

    def set_active_format(self, fmt: CameraFormat) -> None:
        if self._format_group is None:
            return
        for action in self._format_group.actions():
            f: CameraFormat = action.data()
            if (
                f.width == fmt.width
                and f.height == fmt.height
                and abs(f.max_fps - fmt.max_fps) < 0.5
                and f.pixel_format == fmt.pixel_format
            ):
                action.setChecked(True)
                return

    def set_log_file_path(self, path: str) -> None:
        display = path if len(path) <= 60 else "\u2026" + path[-57:]
        self._log_file_action.setText(f"Log: {display}")
        self._log_file_action.setToolTip(path)

    def set_video_source_active(self, is_video: bool) -> None:
        """Update File menu state when source switches between camera and video."""
        self._close_video_action.setEnabled(is_video)

    def set_inference_available(self, available: bool) -> None:
        """Enable/disable the inference toggle (requires model to be loaded)."""
        self._inference_toggle_action.setEnabled(available)

    def set_inference_checked(self, checked: bool) -> None:
        self._inference_toggle_action.setChecked(checked)

    def set_model_label(self, name: str) -> None:
        """Show loaded model name as disabled info item."""
        self._model_info_action.setText(f"Model: {name}")

    # ------------------------------------------------------------------
    # Menu construction
    # ------------------------------------------------------------------

    def _build_menus(self) -> None:
        # --- File menu ---
        file_menu = self.addMenu("File")

        open_video_action = QAction("Open Video\u2026", self)
        open_video_action.triggered.connect(self._on_open_video)
        file_menu.addAction(open_video_action)

        self._close_video_action = QAction("Close Video", self)
        self._close_video_action.setEnabled(False)
        self._close_video_action.triggered.connect(self.video_closed)
        file_menu.addAction(self._close_video_action)

        # --- Camera menu ---
        self._camera_menu = self.addMenu("Camera")
        self._cam_separator = self._camera_menu.addSeparator()
        self._reconnect_action = QAction("Reconnect", self)
        self._reconnect_action.triggered.connect(self.reconnect_requested)
        self._camera_menu.addAction(self._reconnect_action)

        # --- Video menu ---
        self._video_menu = self.addMenu("Video")
        self._res_menu = self._video_menu.addMenu("Resolution")

        # --- Image menu ---
        self._image_menu = self.addMenu("Image")
        self._settings_action = QAction("Camera Settings\u2026", self)
        self._settings_action.triggered.connect(self.camera_settings_requested)
        self._image_menu.addAction(self._settings_action)

        # --- Model menu ---
        model_menu = self.addMenu("Model")

        load_model_action = QAction("Load Model\u2026", self)
        load_model_action.triggered.connect(self._on_load_model)
        model_menu.addAction(load_model_action)

        self._inference_toggle_action = QAction("Enable Inference", self)
        self._inference_toggle_action.setCheckable(True)
        self._inference_toggle_action.setChecked(False)
        self._inference_toggle_action.setEnabled(False)   # enabled after model loaded
        self._inference_toggle_action.toggled.connect(self.inference_toggled)
        model_menu.addAction(self._inference_toggle_action)

        model_menu.addSeparator()

        self._model_info_action = QAction("Model: (none)", self)
        self._model_info_action.setEnabled(False)
        model_menu.addAction(self._model_info_action)

        # --- Debug menu ---
        debug_menu = self.addMenu("Debug")

        self._overlay_action = QAction("Show Overlay", self)
        self._overlay_action.setCheckable(True)
        self._overlay_action.setChecked(True)
        self._overlay_action.toggled.connect(self.overlay_toggled)
        debug_menu.addAction(self._overlay_action)

        self._log_action = QAction("Console Logging", self)
        self._log_action.setCheckable(True)
        self._log_action.setChecked(False)
        self._log_action.toggled.connect(self._on_log_toggled)
        debug_menu.addAction(self._log_action)

        debug_menu.addSeparator()

        self._log_file_action = QAction("Log: (not started)", self)
        self._log_file_action.setEnabled(False)
        debug_menu.addAction(self._log_file_action)

    def _populate_format_menu(self, camera_info: CameraInfo) -> None:
        self._res_menu.clear()
        self._format_group = QActionGroup(self)
        self._format_group.setExclusive(True)

        for fmt in camera_info.formats:
            label = (
                f"{fmt.width}\u00d7{fmt.height}"
                f" @ {fmt.max_fps:.4g}fps"
                f" ({fmt.pixel_format})"
            )
            action = QAction(label, self)
            action.setCheckable(True)
            action.setData(fmt)
            self._format_group.addAction(action)
            self._res_menu.addAction(action)
            action.triggered.connect(self._on_format_action)

        actions = self._format_group.actions()
        if actions:
            actions[0].setChecked(True)

    # ------------------------------------------------------------------
    # Slots
    # ------------------------------------------------------------------

    def _on_open_video(self) -> None:
        path, _ = QFileDialog.getOpenFileName(
            self.parentWidget(),
            "Open Video File",
            "",
            VIDEO_FILE_EXTENSIONS,
        )
        if path:
            logger.debug("Video file selected: %s", path)
            self.video_file_selected.emit(path)

    def _on_load_model(self) -> None:
        path, _ = QFileDialog.getOpenFileName(
            self.parentWidget(),
            "Load YOLO Model",
            "",
            MODEL_FILE_EXTENSIONS,
        )
        if path:
            logger.debug("Model file selected: %s", path)
            self.model_file_selected.emit(path)

    def _on_camera_action(self) -> None:
        action = self.sender()
        if action is None:
            return
        cam: CameraInfo = action.data()
        logger.debug("Camera selected: %s", cam.name)
        self.camera_selected.emit(cam)
        self._populate_format_menu(cam)

    def _on_format_action(self) -> None:
        action = self.sender()
        if action is None:
            return
        fmt: CameraFormat = action.data()
        logger.debug(
            "Format selected: %dx%d @ %.4g fps (%s)",
            fmt.width, fmt.height, fmt.max_fps, fmt.pixel_format,
        )
        self.format_selected.emit(fmt)

    def _on_log_toggled(self, enabled: bool) -> None:
        set_console_level(enabled)
        self.log_toggled.emit(enabled)
