"""Application-level dark/light theme management.

Provides an explicit toggle independent of the host OS setting.
Default is dark regardless of OS theme.
"""
from __future__ import annotations
from typing import Callable

_dark: bool = True
_listeners: list[Callable[[], None]] = []


def is_dark() -> bool:
    return _dark


def set_dark(dark: bool) -> None:
    global _dark
    if _dark == dark:
        return
    _dark = dark
    for cb in list(_listeners):
        try:
            cb()
        except Exception:
            pass


def add_listener(cb: Callable[[], None]) -> None:
    if cb not in _listeners:
        _listeners.append(cb)


def remove_listener(cb: Callable[[], None]) -> None:
    try:
        _listeners.remove(cb)
    except ValueError:
        pass


def palette() -> dict[str, str]:
    if _dark:
        return {
            "bg_deep":          "#0f0f1e",
            "bg":               "#1a1a2e",
            "panel":            "#2a2a2a",
            "surface":          "#16162a",
            "border":           "#1e1e38",
            "border2":          "#333355",
            "accent":           "#f0c040",
            "text":             "#ddddff",
            "text_dim":         "#7777aa",
            "text_muted":       "#555577",
            "btn_bg":           "#252535",
            "input_bg":         "#1e1e38",
            "scrollbar_bg":     "#1a1a2e",
            "scrollbar_handle": "#3a3a5a",
        }
    # Light — "Clinical Interface": cool blue-grey tones, professional medical aesthetic
    return {
        "bg_deep":          "#dce0f0",   # deep bg: status strips, bottom bars
        "bg":               "#f3f5fc",   # main window background
        "panel":            "#ffffff",   # pure white content panels
        "surface":          "#e8ecf7",   # toolbar/header bg (replaces dark #16162a)
        "border":           "#c8ceeb",   # light separator lines
        "border2":          "#a0aad6",   # input borders, stronger dividers
        "accent":           "#8b6200",   # dark amber — brand-consistent, WCAG AA on white
        "text":             "#1a1e32",   # near-black with blue tint
        "text_dim":         "#48507a",   # secondary labels
        "text_muted":       "#787faa",   # placeholder / dimmed
        "btn_bg":           "#eaecf8",   # button backgrounds
        "input_bg":         "#ffffff",   # input field bg
        "scrollbar_bg":     "#dce0f0",
        "scrollbar_handle": "#a0aad6",
    }


def make_app_stylesheet() -> str:
    p = palette()
    return f"""
QWidget {{
    background-color: {p['bg']};
    color: {p['text']};
    selection-background-color: {p['accent']};
    selection-color: #000000;
}}
QMainWindow {{
    background-color: {p['bg_deep']};
}}
QDialog {{
    background-color: {p['bg']};
}}
QLabel {{
    background-color: transparent;
    color: {p['text']};
}}
QGroupBox {{
    background-color: {p['panel']};
    border: 1px solid {p['border2']};
    border-radius: 4px;
    margin-top: 8px;
    padding-top: 4px;
    color: {p['text']};
    font-weight: bold;
}}
QGroupBox::title {{
    subcontrol-origin: margin;
    left: 8px;
    padding: 0 3px;
    color: {p['text_dim']};
    font-weight: normal;
}}
QPushButton {{
    background-color: {p['btn_bg']};
    color: {p['text']};
    border: 1px solid {p['border2']};
    border-radius: 3px;
    padding: 3px 10px;
    min-height: 22px;
}}
QPushButton:hover {{
    border-color: {p['accent']};
}}
QPushButton:pressed {{
    background-color: {p['bg_deep']};
}}
QPushButton:disabled {{
    color: {p['text_muted']};
    border-color: {p['border']};
    background-color: {p['surface']};
}}
QComboBox {{
    background-color: {p['input_bg']};
    color: {p['text']};
    border: 1px solid {p['border2']};
    border-radius: 3px;
    padding: 2px 24px 2px 6px;
    min-height: 22px;
}}
QComboBox:hover {{ border-color: {p['accent']}; }}
QComboBox::drop-down {{ border: none; width: 18px; }}
QComboBox QAbstractItemView {{
    background-color: {p['panel']};
    color: {p['text']};
    border: 1px solid {p['border2']};
    selection-background-color: {p['accent']};
    selection-color: #000000;
}}
QLineEdit, QTextEdit, QPlainTextEdit {{
    background-color: {p['input_bg']};
    color: {p['text']};
    border: 1px solid {p['border2']};
    border-radius: 3px;
    padding: 2px 4px;
}}
QLineEdit:hover, QTextEdit:hover {{ border-color: {p['accent']}; }}
QSpinBox, QDoubleSpinBox {{
    background-color: {p['input_bg']};
    color: {p['text']};
    border: 1px solid {p['border2']};
    border-radius: 3px;
    padding: 2px 4px;
}}
QTableWidget, QTableView {{
    background-color: {p['panel']};
    color: {p['text']};
    gridline-color: {p['border']};
    border: 1px solid {p['border2']};
    alternate-background-color: {p['surface']};
}}
QTableWidget QHeaderView::section, QTableView QHeaderView::section {{
    background-color: {p['surface']};
    color: {p['text_dim']};
    border: 1px solid {p['border']};
    padding: 3px 6px;
    font-weight: bold;
}}
QListWidget {{
    background-color: {p['panel']};
    color: {p['text']};
    border: 1px solid {p['border2']};
}}
QListWidget::item:selected {{
    background-color: {p['accent']};
    color: #000000;
}}
QScrollBar:vertical {{
    background: {p['scrollbar_bg']};
    width: 8px;
    border: none;
    margin: 0;
}}
QScrollBar::handle:vertical {{
    background: {p['scrollbar_handle']};
    border-radius: 4px;
    min-height: 20px;
}}
QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {{ height: 0; border: none; }}
QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {{ background: none; }}
QScrollBar:horizontal {{
    background: {p['scrollbar_bg']};
    height: 8px;
    border: none;
    margin: 0;
}}
QScrollBar::handle:horizontal {{
    background: {p['scrollbar_handle']};
    border-radius: 4px;
    min-width: 20px;
}}
QScrollBar::add-line:horizontal, QScrollBar::sub-line:horizontal {{ width: 0; border: none; }}
QScrollBar::add-page:horizontal, QScrollBar::sub-page:horizontal {{ background: none; }}
QSplitter::handle {{ background: {p['border']}; }}
QStatusBar {{
    background: {p['bg_deep']};
    color: {p['text_muted']};
    font-size: 11px;
}}
QTabWidget::pane {{
    border: 1px solid {p['border2']};
    background: {p['bg']};
}}
QTabBar::tab {{
    background: {p['surface']};
    color: {p['text_dim']};
    border: 1px solid {p['border']};
    border-bottom: none;
    padding: 4px 12px;
    margin-right: 2px;
    border-top-left-radius: 3px;
    border-top-right-radius: 3px;
}}
QTabBar::tab:selected {{
    background: {p['bg']};
    color: {p['text']};
    border-bottom: 2px solid {p['accent']};
}}
QTabBar::tab:hover:!selected {{ background: {p['panel']}; }}
QCheckBox {{ color: {p['text']}; spacing: 6px; }}
QCheckBox::indicator {{
    width: 14px; height: 14px;
    border: 1px solid {p['border2']};
    border-radius: 2px;
    background: {p['input_bg']};
}}
QCheckBox::indicator:checked {{ background: {p['accent']}; border-color: {p['accent']}; }}
QProgressBar {{
    background-color: {p['input_bg']};
    color: {p['text']};
    border: 1px solid {p['border2']};
    border-radius: 3px;
    text-align: center;
}}
QProgressBar::chunk {{ background-color: {p['accent']}; border-radius: 2px; }}
QToolTip {{
    background-color: {p['panel']};
    color: {p['text']};
    border: 1px solid {p['border2']};
    padding: 2px 6px;
}}
QScrollArea {{ border: none; background: transparent; }}
"""
