import ctypes
from PySide6.QtCore import QSettings, Signal, QObject
from PySide6.QtWidgets import QMessageBox

def set_dark_title_bar(hwnd, enable=True):
    """Включает тёмный заголовок окна в Windows 10+."""
    DWMWA_USE_IMMERSIVE_DARK_MODE = 20
    value = ctypes.c_int(1 if enable else 0)
    ctypes.windll.dwmapi.DwmSetWindowAttribute(
        hwnd,
        DWMWA_USE_IMMERSIVE_DARK_MODE,
        ctypes.byref(value),
        ctypes.sizeof(value)
    )

class ThemeManager(QObject):
    theme_changed = Signal(str)

    def __init__(self):
        super().__init__()
        self.settings = QSettings("yobble_messenger", "Theme")
        self.theme = self.settings.value("theme", "dark")
        self.color_palette = {
            "dark": {
                "background": "#2a2a2a",
                "primary": "#3c3c3c",
                "secondary": "#4a4a4a",
                "text": "#ffffff",
                "text_secondary": "#aaaaaa",
                "accent": "#5a9bcf",
                "border": "#4a4a4a",
                "hover": "#4a4a4a",
                "selected": "#5a9bcf",
            },
            "light": {
                "background": "#ffffff",
                "primary": "#f5f5f5",
                "secondary": "#e0e0e0",
                "text": "#000000",
                "text_secondary": "#555555",
                "accent": "#0078d7",
                "border": "#e0e0e0",
                "hover": "#e9e9e9",
                "selected": "#dcdcdc",
            }
        }

    def get_current_palette(self):
        return self.color_palette[self.theme]

    def get_theme(self):
        return self.theme

    def is_dark(self):
        return self.theme == "dark"

    def set_theme(self, theme: str):
        if theme in ("dark", "light"):
            self.theme = theme
            self.settings.setValue("theme", theme)
            self.theme_changed.emit(self.theme)

    def toggle_theme(self):
        """Переключает тему между светлой и темной."""
        new_theme = "light" if self.is_dark() else "dark"
        self.set_theme(new_theme)

# Глобальный экземпляр
theme_manager = ThemeManager()
