# btop — themed from per-theme assets, baked at eval time.
# A hand-made themes/<slug>/btop.theme asset wins; otherwise a theme is
# generated from the palette, so custom themes get a themed btop too.
{ config, lib, ... }:

let
  t = config.nomarchy.theme;
  c = t.colors;

  asset = config.nomarchy.themesDir + "/${t.slug}/btop.theme";

  generated =
    let
      pairs = {
        main_bg = c.base;
        main_fg = c.text;
        title = c.text;
        hi_fg = c.accent;
        selected_bg = c.overlay;
        selected_fg = c.text;
        inactive_fg = c.muted;
        graph_text = c.subtext;
        meter_bg = c.surface;
        proc_misc = c.accentAlt;
        cpu_box = c.accent;
        mem_box = c.good;
        net_box = c.accentAlt;
        proc_box = c.surface;
        div_line = c.overlay;
        temp_start = c.good;
        temp_mid = c.warn;
        temp_end = c.bad;
        cpu_start = c.good;
        cpu_mid = c.warn;
        cpu_end = c.bad;
        free_start = c.good;
        used_start = c.warn;
        download_start = c.accent;
        upload_start = c.accentAlt;
      };
    in
    lib.concatStringsSep "\n"
      (lib.mapAttrsToList (k: v: "theme[${k}]=\"${v}\"") pairs) + "\n";
in
{
  config = lib.mkIf config.nomarchy.btop.enable {
    programs.btop = {
      enable = true;
      settings = {
        color_theme = "nomarchy";
        theme_background = false; # let the terminal's opacity show through
        vim_keys = true;
      };
    };

    xdg.configFile."btop/themes/nomarchy.theme" =
      if builtins.pathExists asset
      then { source = asset; }
      else { text = generated; };
  };
}
