"""Accessible application themes and background-overlay colors."""

from __future__ import annotations


THEME_PALETTES: dict[str, dict[str, object]] = {
    "dark": {
        "window": "#0d1117",
        "panel": "rgba(17, 21, 28, 124)",
        "panel_strong": "rgba(13, 17, 23, 239)",
        "field": "rgba(21, 27, 36, 148)",
        "alternate": "rgba(25, 33, 44, 156)",
        "text": "#e8edf6",
        "muted": "#aab7c8",
        "button": "#202a36",
        "hover": "#2a394a",
        "border": "#455a72",
        "grid": "#344459",
        "selection": "#315f85",
        "selection_text": "#ffffff",
        "accent": "#75c9ff",
        "accent_strong": "#397cab",
        "checked": "#1c4b6b",
        "disabled": "#677587",
        "danger": "#352229",
        "danger_hover": "#512b37",
        "danger_text": "#ffd6df",
        "edit": "#243145",
        "editor": "#151b24",
        "edit_checked": "#8a5a18",
        "badge": "#183448",
        "badge_text": "#9edbff",
        "canvas": "#0d1117",
        "smoke": (7, 12, 18, 48),
        "image_opacity": 0.94,
    },
    "white": {
        "window": "#edf2f7",
        "panel": "rgba(250, 252, 255, 116)",
        "panel_strong": "rgba(242, 246, 250, 244)",
        "field": "rgba(255, 255, 255, 138)",
        "alternate": "rgba(231, 238, 246, 148)",
        "text": "#172133",
        "muted": "#4f6075",
        "button": "#e0e8f1",
        "hover": "#cfdeec",
        "border": "#8497ad",
        "grid": "#aebdcd",
        "selection": "#276f9f",
        "selection_text": "#ffffff",
        "accent": "#176895",
        "accent_strong": "#2b78aa",
        "checked": "#246b98",
        "disabled": "#8793a3",
        "danger": "#f4dce2",
        "danger_hover": "#edc2cd",
        "danger_text": "#7c233b",
        "edit": "#d9e5f0",
        "editor": "#ffffff",
        "edit_checked": "#a6660d",
        "badge": "#d8effc",
        "badge_text": "#164f73",
        "canvas": "#e6edf4",
        "smoke": (247, 250, 253, 52),
        "image_opacity": 0.94,
    },
    "blue": {
        "window": "#06182d",
        "panel": "rgba(7, 28, 52, 124)",
        "panel_strong": "rgba(5, 21, 40, 241)",
        "field": "rgba(9, 35, 64, 148)",
        "alternate": "rgba(13, 44, 77, 156)",
        "text": "#edf7ff",
        "muted": "#afc9df",
        "button": "#16466f",
        "hover": "#1e5b8c",
        "border": "#467ca3",
        "grid": "#315e82",
        "selection": "#2178b4",
        "selection_text": "#ffffff",
        "accent": "#77d3ff",
        "accent_strong": "#2388c6",
        "checked": "#14618f",
        "disabled": "#6f879b",
        "danger": "#482238",
        "danger_hover": "#69304d",
        "danger_text": "#ffd6e7",
        "edit": "#173b61",
        "editor": "#092540",
        "edit_checked": "#9a6514",
        "badge": "#0d4b70",
        "badge_text": "#b5e8ff",
        "canvas": "#06182d",
        "smoke": (3, 19, 37, 48),
        "image_opacity": 0.94,
    },
    "pink": {
        "window": "#26101e",
        "panel": "rgba(43, 16, 34, 124)",
        "panel_strong": "rgba(34, 11, 26, 241)",
        "field": "rgba(51, 20, 40, 148)",
        "alternate": "rgba(65, 24, 49, 156)",
        "text": "#fff1f7",
        "muted": "#dfb6c9",
        "button": "#5a2944",
        "hover": "#733453",
        "border": "#97617b",
        "grid": "#71415a",
        "selection": "#a83f76",
        "selection_text": "#ffffff",
        "accent": "#ff9bc8",
        "accent_strong": "#c94f88",
        "checked": "#8f3565",
        "disabled": "#9a7486",
        "danger": "#5e2634",
        "danger_hover": "#7c3043",
        "danger_text": "#ffdce5",
        "edit": "#5a2944",
        "editor": "#37152b",
        "edit_checked": "#a05c16",
        "badge": "#6c2950",
        "badge_text": "#ffd3e7",
        "canvas": "#26101e",
        "smoke": (35, 7, 25, 50),
        "image_opacity": 0.94,
    },
    "yellow": {
        "window": "#2d2508",
        "panel": "rgba(47, 38, 8, 128)",
        "panel_strong": "rgba(38, 30, 5, 242)",
        "field": "rgba(54, 44, 11, 150)",
        "alternate": "rgba(68, 55, 12, 158)",
        "text": "#fff9dc",
        "muted": "#dfd19a",
        "button": "#5c4b12",
        "hover": "#746018",
        "border": "#9c8431",
        "grid": "#746121",
        "selection": "#8a6f0b",
        "selection_text": "#ffffff",
        "accent": "#f4d35e",
        "accent_strong": "#c49a18",
        "checked": "#83680b",
        "disabled": "#988c62",
        "danger": "#572d22",
        "danger_hover": "#763d2e",
        "danger_text": "#ffe1d7",
        "edit": "#514516",
        "editor": "#443811",
        "edit_checked": "#ad6712",
        "badge": "#5f5014",
        "badge_text": "#fff0a8",
        "canvas": "#2d2508",
        "smoke": (39, 30, 3, 52),
        "image_opacity": 0.94,
    },
}


def normalize_theme(theme: object) -> str:
    key = str(theme or "dark").lower()
    return key if key in THEME_PALETTES else "dark"


def background_paint_values(theme: object) -> tuple[str, tuple[int, ...], float]:
    palette = THEME_PALETTES[normalize_theme(theme)]
    return (
        str(palette["canvas"]),
        tuple(palette["smoke"]),
        float(palette["image_opacity"]),
    )


def build_stylesheet(theme: object = "dark") -> str:
    p = THEME_PALETTES[normalize_theme(theme)]
    return f"""
QWidget {{
    background-color: transparent;
    color: {p['text']};
    font-family: "Yu Gothic UI", "Meiryo UI", "Segoe UI";
    font-size: 10pt;
}}
QMainWindow, QDialog, QMessageBox, QProgressDialog {{
    background-color: {p['window']};
}}
QWidget#topBar, QWidget#playlistPanel,
QWidget#nowPlayingPanel, QFrame#transportFrame {{
    background-color: {p['panel']};
}}
QWidget#tablePanel {{
    background-color: transparent;
}}
QScrollArea#nowPlayingScroll,
QWidget#nowPlayingContent {{
    background-color: transparent;
    border: 0;
}}
QMenuBar, QMenu, QStatusBar {{
    background-color: {p['panel_strong']};
    color: {p['text']};
}}
QMenu {{
    border: 1px solid {p['border']};
    padding: 5px;
}}
QMenu::item {{
    padding: 6px 28px 6px 22px;
    margin: 1px 3px;
}}
QMenu::separator {{
    height: 1px;
    background-color: {p['grid']};
    margin: 5px 8px;
}}
QMenuBar::item:selected, QMenu::item:selected {{
    background-color: {p['selection']};
    color: {p['selection_text']};
}}
QMenu::item:checked {{
    background-color: {p['checked']};
    color: {p['selection_text']};
    border-left: 3px solid {p['accent']};
}}
QPushButton, QToolButton, QComboBox, QSpinBox {{
    background-color: {p['button']};
    color: {p['text']};
    border: 1px solid {p['border']};
    border-radius: 5px;
    padding: 6px 10px;
}}
QPushButton:hover, QToolButton:hover, QComboBox:hover, QSpinBox:hover {{
    background-color: {p['hover']};
    border-color: {p['accent']};
}}
QPushButton:pressed, QToolButton:pressed {{
    background-color: {p['selection']};
}}
QPushButton:disabled, QToolButton:disabled, QComboBox:disabled,
QSpinBox:disabled {{
    color: {p['disabled']};
    border-color: {p['grid']};
}}
QCheckBox, QRadioButton {{
    background-color: {p['button']};
    color: {p['text']};
    border: 1px solid {p['border']};
    border-radius: 5px;
    padding: 5px 8px;
    spacing: 8px;
    min-height: 18px;
}}
QCheckBox:hover, QRadioButton:hover {{
    background-color: {p['hover']};
    border-color: {p['accent']};
}}
QCheckBox:checked, QRadioButton:checked {{
    background-color: {p['checked']};
    border-color: {p['accent']};
    color: {p['selection_text']};
    font-weight: 600;
}}
QCheckBox:pressed, QRadioButton:pressed {{
    background-color: {p['selection']};
}}
QCheckBox:disabled, QRadioButton:disabled {{
    background-color: {p['panel_strong']};
    border-color: {p['grid']};
    color: {p['disabled']};
}}
QCheckBox::indicator, QRadioButton::indicator {{
    width: 17px;
    height: 17px;
}}
QPushButton#dangerButton, QToolButton#dangerButton {{
    background-color: {p['danger']};
    border-color: {p['border']};
    color: {p['danger_text']};
}}
QPushButton#dangerButton:hover, QToolButton#dangerButton:hover {{
    background-color: {p['danger_hover']};
    border-color: {p['accent']};
}}
QPushButton#editModeButton {{
    background-color: {p['edit']};
    border-color: {p['border']};
}}
QPushButton#editModeButton:checked {{
    background-color: {p['edit_checked']};
    border-color: {p['accent']};
    color: #ffffff;
    font-weight: 600;
}}
QPushButton#playButton {{
    background-color: {p['accent_strong']};
    color: #ffffff;
    border-radius: 18px;
    font-size: 13pt;
    min-width: 36px;
    min-height: 36px;
    padding: 0;
}}
QPushButton#skipButton {{
    font-size: 9pt;
    padding: 0;
}}
QLineEdit, QPlainTextEdit, QListWidget, QTableView {{
    background-color: {p['field']};
    color: {p['text']};
    border: 1px solid {p['grid']};
    border-radius: 4px;
    selection-background-color: {p['selection']};
    selection-color: {p['selection_text']};
}}
QLineEdit:disabled, QPlainTextEdit:disabled {{
    color: {p['disabled']};
}}
QPlainTextEdit#lyricsEditor {{
    background-color: {p['editor']};
    color: {p['text']};
    border: 1px solid {p['border']};
    padding: 8px;
}}
QLineEdit#tableCellEditor {{
    background-color: {p['editor']};
    color: {p['text']};
    border: 2px solid {p['accent']};
    border-radius: 0;
    padding: 0 3px;
    selection-background-color: {p['selection']};
    selection-color: {p['selection_text']};
}}
QTableView {{
    gridline-color: {p['grid']};
    alternate-background-color: {p['alternate']};
}}
QTableView::item:selected, QListWidget::item:selected {{
    background-color: {p['selection']};
    color: {p['selection_text']};
}}
QHeaderView::section {{
    background-color: {p['button']};
    color: {p['text']};
    border: 0;
    border-right: 1px solid {p['grid']};
    border-bottom: 1px solid {p['grid']};
    padding: 7px 5px;
}}
QGroupBox {{
    background-color: {p['panel']};
    border: 1px solid {p['grid']};
    border-radius: 6px;
    margin-top: 9px;
    padding-top: 8px;
}}
QGroupBox::title {{
    subcontrol-origin: margin;
    left: 9px;
    padding: 0 4px;
    color: {p['text']};
}}
QSlider::groove:horizontal {{
    height: 4px;
    background-color: {p['grid']};
    border-radius: 2px;
}}
QSlider::handle:horizontal {{
    background-color: {p['accent']};
    width: 14px;
    margin: -5px 0;
    border-radius: 7px;
}}
QSlider::sub-page:horizontal {{
    background-color: {p['accent_strong']};
}}
QLabel#titleLabel {{
    font-size: 15pt;
    font-weight: 600;
}}
QLabel#mutedLabel {{
    color: {p['muted']};
}}
QLabel#tableHint {{
    background-color: {p['field']};
    color: {p['muted']};
    border-top: 1px solid {p['grid']};
}}
QLabel#qualityBadge {{
    background-color: {p['badge']};
    color: {p['badge_text']};
    border: 1px solid {p['selection']};
    border-radius: 4px;
    padding: 4px 7px;
}}
QLabel#artworkDropLabel {{
    background-color: {p['field']};
    color: {p['muted']};
    border: 1px solid {p['grid']};
}}
QLabel#artworkDropLabel[dropActive="true"] {{
    background-color: {p['badge']};
    color: {p['badge_text']};
    border: 2px solid {p['accent']};
}}
QSplitter::handle {{
    background-color: {p['grid']};
    width: 1px;
}}
QScrollBar:vertical, QScrollBar:horizontal {{
    background-color: {p['panel_strong']};
}}
QScrollBar::handle:vertical, QScrollBar::handle:horizontal {{
    background-color: {p['border']};
    border-radius: 4px;
    min-width: 20px;
    min-height: 20px;
}}
QToolTip {{
    background-color: {p['panel_strong']};
    color: {p['text']};
    border: 1px solid {p['accent']};
    padding: 4px;
}}
"""


APP_STYLE = build_stylesheet("dark")
