#!/usr/bin/env python3
"""Generate NexusOS.colors (KDE KColorScheme format) from _palette.py.

Run from anywhere:
    python3 assets/themes/KDE/generate_plasma_colors.py

Writes to assets/themes/KDE/plasma/NexusOS/NexusOS.colors (relative to repo root).
"""
import os
import sys

# Resolve repo root relative to this script's location
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
REPO_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, "../../.."))
sys.path.insert(0, os.path.join(REPO_ROOT, "assets/themes"))

import _palette as p  # noqa: E402

OUT = os.path.join(SCRIPT_DIR, "plasma/NexusOS/NexusOS.colors")


def rgb(hexstr):
    h = hexstr.lstrip("#")
    return f"{int(h[0:2],16)},{int(h[2:4],16)},{int(h[4:6],16)}"


def section(name, bg_normal, bg_alt, fg_normal, fg_inactive, fg_active,
            fg_link, fg_visited, fg_neg, fg_neutral, fg_pos,
            deco_focus, deco_hover):
    return f"""[Colors:{name}]
BackgroundNormal={rgb(bg_normal)}
BackgroundAlternate={rgb(bg_alt)}
ForegroundNormal={rgb(fg_normal)}
ForegroundInactive={rgb(fg_inactive)}
ForegroundActive={rgb(fg_active)}
ForegroundLink={rgb(fg_link)}
ForegroundVisited={rgb(fg_visited)}
ForegroundNegative={rgb(fg_neg)}
ForegroundNeutral={rgb(fg_neutral)}
ForegroundPositive={rgb(fg_pos)}
DecorationFocus={rgb(deco_focus)}
DecorationHover={rgb(deco_hover)}
"""


SHARED = dict(
    fg_normal   = "#" + p.TEXT_PRIMARY,
    fg_inactive = "#" + p.TEXT_SECONDARY,
    fg_active   = "#" + p.BRAND_GREEN,
    fg_link     = "#" + p.BRAND_GREEN_LIGHT,
    fg_visited  = "#" + p.BRAND_PURPLE_LIGHT,
    fg_neg      = "#" + p.ERROR,
    fg_neutral  = "#" + p.WARNING,
    fg_pos      = "#" + p.SUCCESS,
    deco_focus  = "#" + p.BRAND_GREEN,
    deco_hover  = "#" + p.BRAND_PURPLE,
)

content = f"""\
[ColorEffects:Disabled]
Color=56,56,56
ColorAmount=0
ColorEffect=0
ContrastAmount=0.1
ContrastEffect=2
IntensityAmount=0
IntensityEffect=0

[ColorEffects:Inactive]
ChangeSelectionColor=true
Color=112,111,110
ColorAmount=0.025
ColorEffect=0
ContrastAmount=0.1
ContrastEffect=2
Enable=false
IntensityAmount=0
IntensityEffect=0

""" + section(
    "Window",
    bg_normal="#" + p.BASE_BG,
    bg_alt   ="#" + p.SURFACE_BG,
    **SHARED,
) + section(
    "View",
    bg_normal="#" + p.BASE_BG,
    bg_alt   ="#" + p.SURFACE_BG,
    **SHARED,
) + section(
    "Button",
    bg_normal="#" + p.SURFACE_BG_ALT,
    bg_alt   ="#" + p.BASE_BG,
    **SHARED,
) + section(
    "Selection",
    bg_normal ="#" + p.BRAND_PURPLE,
    bg_alt    ="#" + p.BRAND_GREEN_DARK,
    fg_normal ="#" + p.TEXT_ON_SELECTION,
    fg_inactive="#" + p.TEXT_ON_SELECTION,
    fg_active ="#" + p.BRAND_GREEN,
    fg_link   ="#" + p.BRAND_GREEN_LIGHT,
    fg_visited="#" + p.BRAND_PURPLE_LIGHT,
    fg_neg    ="#" + p.ERROR,
    fg_neutral="#" + p.WARNING,
    fg_pos    ="#" + p.SUCCESS,
    deco_focus="#" + p.BRAND_GREEN,
    deco_hover="#" + p.BRAND_PURPLE,
) + section(
    "Tooltip",
    bg_normal="#" + p.OVERLAY_BG,
    bg_alt   ="#" + p.BORDER_STRONG,
    **SHARED,
) + section(
    "Complementary",
    bg_normal="#" + p.SURFACE_BG_ALT,
    bg_alt   ="#" + p.OVERLAY_BG,
    **SHARED,
) + section(
    "Header",
    bg_normal="#" + p.BASE_BG,
    bg_alt   ="#" + p.SURFACE_BG_ALT,
    **SHARED,
) + f"""\
[General]
ColorScheme=NexusOS
Name=NexusOS
shadeSortColumn=true

[KDE]
contrast=4
"""

os.makedirs(os.path.dirname(OUT), exist_ok=True)
with open(OUT, "w") as f:
    f.write(content)

print(f"Written: {OUT}")
