"""Dracula Pro 256-color theme and curses color pair initialization."""

import curses

# Dracula Pro palette (256-color approximations)
DRACULA = {
    "bg":       236,   # ~#303030
    "fg":       255,   # white
    "comment":  97,    # purple-grey
    "string":   43,    # cyan/teal
    "number":   141,   # light purple
    "keyword":  212,   # pink
    "function": 84,    # green
    "operator": 209,   # orange
    "class":    141,   # purple
    "builtin":  87,    # bright cyan
    "error":    196,   # red
    "lineno":   240,   # grey
    "status":   141,   # purple (status bar bg)
    "statusfg": 16,    # black (status bar fg)
    "visual":   60,    # selection highlight bg
    "cursor":   255,   # cursor line
    "search":   220,   # yellow highlight
}

# Tab bar colors
TAB_BG = 236
TAB_ACTIVE_FG = 141
TAB_INACTIVE_FG = 245

# Named color pair IDs (assigned during init)
_color_pairs = {}
_next_pair = [1]


def init_colors():
    """Initialize all color pairs. Call once after curses.start_color()."""
    global _color_pairs
    _color_pairs = {}
    pid = 1
    bg = DRACULA["bg"]

    # One pair per named foreground color on Dracula bg
    for name, fg in DRACULA.items():
        if name == "bg":
            continue
        curses.init_pair(pid, fg, bg)
        _color_pairs[name] = pid
        pid += 1

    # Visual selection: fg on visual bg
    curses.init_pair(pid, DRACULA["fg"], DRACULA["visual"])
    _color_pairs["visual_sel"] = pid
    pid += 1

    # Search highlight: black on yellow
    curses.init_pair(pid, 16, DRACULA["search"])
    _color_pairs["search_hl"] = pid
    pid += 1

    # Status bar: black on purple
    curses.init_pair(pid, DRACULA["statusfg"], DRACULA["status"])
    _color_pairs["statusbar"] = pid
    pid += 1

    # Default: fg on bg
    curses.init_pair(pid, DRACULA["fg"], DRACULA["bg"])
    _color_pairs["default"] = pid
    pid += 1

    # Tab bar: active tab
    curses.init_pair(pid, TAB_ACTIVE_FG, TAB_BG)
    _color_pairs["tab_active"] = pid
    pid += 1

    # Tab bar: inactive tab
    curses.init_pair(pid, TAB_INACTIVE_FG, TAB_BG)
    _color_pairs["tab_inactive"] = pid
    pid += 1

    # Tab bar background
    curses.init_pair(pid, DRACULA["fg"], TAB_BG)
    _color_pairs["tab_bg"] = pid
    pid += 1

    # Explorer-specific pairs
    # Dirs: cyan on bg
    curses.init_pair(pid, curses.COLOR_CYAN, bg)
    _color_pairs["explorer_dir"] = pid
    pid += 1

    # Selected: yellow on bg
    curses.init_pair(pid, curses.COLOR_YELLOW, bg)
    _color_pairs["explorer_sel"] = pid
    pid += 1

    # Cursor: black on cyan
    curses.init_pair(pid, 16, 44)
    _color_pairs["explorer_cursor"] = pid
    pid += 1

    # Cursor+selected: black on yellow
    curses.init_pair(pid, 16, DRACULA["search"])
    _color_pairs["explorer_cur_sel"] = pid
    pid += 1

    # Symlink: magenta on bg
    curses.init_pair(pid, curses.COLOR_MAGENTA, bg)
    _color_pairs["explorer_link"] = pid
    pid += 1

    # Executable: green on bg
    curses.init_pair(pid, 84, bg)
    _color_pairs["explorer_exec"] = pid
    pid += 1

    # Header: green on bg
    curses.init_pair(pid, 84, bg)
    _color_pairs["explorer_header"] = pid
    pid += 1

    # Fold line highlight: fg on slightly lighter bg
    curses.init_pair(pid, DRACULA["fg"], 238)
    _color_pairs["fold_line"] = pid
    pid += 1

    # Fold marker text: comment color on fold bg
    curses.init_pair(pid, DRACULA["comment"], 238)
    _color_pairs["fold_marker"] = pid
    pid += 1

    _next_pair[0] = pid + 1


def cp(name):
    """Get curses color_pair attribute by name."""
    return curses.color_pair(_color_pairs.get(name, 0))
