"""Mini settings window for MathSolver mode switching."""
from __future__ import annotations

import logging
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from .math_solver import MathSolver

log = logging.getLogger(__name__)

try:
    from PyQt6.QtWidgets import (
        QWidget, QVBoxLayout, QHBoxLayout, QLabel,
        QComboBox, QPushButton,
    )
    from PyQt6.QtCore import Qt
    HAS_QT = True
except ImportError:
    HAS_QT = False


if HAS_QT:
    class MathSettingsWindow(QWidget):
        """Small floating window for mode selection."""

        def __init__(self, solver: MathSolver) -> None:
            super().__init__()
            self._solver = solver

            self.setWindowTitle("MuraveyChina — Settings")
            self.setWindowFlags(
                Qt.WindowType.WindowStaysOnTopHint
                | Qt.WindowType.Tool
            )
            self.setFixedSize(250, 100)

            layout = QVBoxLayout()

            # Mode selector
            mode_layout = QHBoxLayout()
            mode_label = QLabel("Mode:")
            self._combo = QComboBox()
            self._combo.addItem("Mathematics", "math")
            self._combo.addItem("Physics", "physics")
            self._combo.addItem("Chemistry", "chemistry")
            self._combo.addItem("STEM Chinese", "stem")
            self._combo.addItem("Humanities Chinese", "humanities")
            # Set current
            idx = self._combo.findData(solver.mode)
            if idx >= 0:
                self._combo.setCurrentIndex(idx)
            self._combo.currentIndexChanged.connect(self._on_mode_changed)
            mode_layout.addWidget(mode_label)
            mode_layout.addWidget(self._combo)
            layout.addLayout(mode_layout)

            # Status label
            self._status = QLabel(f"Current: {solver.mode.capitalize()}")
            layout.addWidget(self._status)

            # Close button
            close_btn = QPushButton("Close")
            close_btn.clicked.connect(self.hide)
            layout.addWidget(close_btn)

            self.setLayout(layout)

        def _on_mode_changed(self, index: int) -> None:
            mode = self._combo.currentData()
            if mode:
                self._solver.set_mode(mode)
                self._status.setText(f"Current: {mode.capitalize()}")

        def toggle(self) -> None:
            if self.isVisible():
                self.hide()
            else:
                self.show()
                self.raise_()
                self.activateWindow()

else:
    class MathSettingsWindow:
        """Stub when PyQt6 not available."""
        def __init__(self, solver) -> None:
            pass
        def toggle(self) -> None:
            pass
