from db.database import get_setting, set_setting

THEMES = {
    "dark": {
        "panel_bg":       "rgba(28,28,28,242)",
        "panel_border":   "rgba(255,255,255,18)",
        "header_bg":      "#3a3a3a",
        "header_hover":   "#484848",
        "item_name_color":"#ddd",
        "search_bg":      "rgba(255,255,255,12)",
        "search_border":  "#555",
        "search_color":   "#eee",
        "search_focus":   "#4a9eff",
        "btn_color":      "#999",
        "btn_hover":      "#fff",
        "scrollbar":      "#555",
        "menu_bg":        "#2b2b2b",
        "menu_color":     "#eee",
        "menu_border":    "#555",
        "menu_selected":  "#3a3a3a",
    },
    "light": {
        "panel_bg":       "rgba(245,245,245,250)",
        "panel_border":   "rgba(0,0,0,15)",
        "header_bg":      "#e0e0e0",
        "header_hover":   "#d0d0d0",
        "item_name_color":"#222",
        "search_bg":      "rgba(0,0,0,8)",
        "search_border":  "#bbb",
        "search_color":   "#222",
        "search_focus":   "#4a9eff",
        "btn_color":      "#555",
        "btn_hover":      "#000",
        "scrollbar":      "#bbb",
        "menu_bg":        "#f5f5f5",
        "menu_color":     "#222",
        "menu_border":    "#ccc",
        "menu_selected":  "#e0e0e0",
    },
}

_current = "dark"


def load():
    """从数据库读取上次保存的主题，应在 init_db() 之后调用"""
    global _current
    saved = get_setting("theme", "dark")
    _current = saved if saved in THEMES else "dark"


def current() -> dict:
    return THEMES[_current]


def name() -> str:
    return _current


def set_theme(theme_name: str):
    global _current
    if theme_name in THEMES:
        _current = theme_name
        set_setting("theme", theme_name)
