#!/bin/bash
# Generated by matugen — recolors Adwaita folder icons with wallpaper palette
# and pulls in Papirus mimetype icons for better file type distinction
ICON_DIR="$HOME/.local/share/icons/WallpaperAdwaita"
ADWAITA="/run/current-system/sw/share/icons/Adwaita"
PAPIRUS="/run/current-system/sw/share/icons/Papirus-Dark"

mkdir -p "$ICON_DIR/scalable/places"

# Create index.theme with all mimetype sizes
cat > "$ICON_DIR/index.theme" << 'THEME'
[Icon Theme]
Name=WallpaperAdwaita
Comment=Adwaita with wallpaper-colored folders and Papirus mimetypes
Inherits=Adwaita,hicolor
DisplayDepth=32

[scalable/places]
Size=128
MinSize=16
MaxSize=512
Type=Scalable
Context=Places

[16x16/mimetypes]
Size=16
Type=Fixed
Context=MimeTypes

[22x22/mimetypes]
Size=22
Type=Fixed
Context=MimeTypes

[32x32/mimetypes]
Size=32
Type=Fixed
Context=MimeTypes

[48x48/mimetypes]
Size=48
Type=Fixed
Context=MimeTypes

[64x64/mimetypes]
Size=64
MinSize=32
MaxSize=128
Type=Scalable
Context=MimeTypes

[96x96/mimetypes]
Size=96
MinSize=64
MaxSize=256
Type=Scalable
Context=MimeTypes

[128x128/mimetypes]
Size=128
MinSize=64
MaxSize=512
Type=Scalable
Context=MimeTypes
THEME

# Recolor all Adwaita place SVGs with wallpaper palette
for svg in "$ADWAITA/scalable/places"/*.svg; do
  name=$(basename "$svg")
  sed \
    -e 's/#438de6/{{colors.primary_container.default.hex}}/gi' \
    -e 's/#62a0ea/{{colors.primary_fixed_dim.default.hex}}/gi' \
    -e 's/#a4caee/{{colors.primary_fixed.default.hex}}/gi' \
    -e 's/#afd4ff/{{colors.primary_fixed.default.hex}}/gi' \
    -e 's/#c0d5ea/{{colors.primary_fixed.default.hex}}/gi' \
    "$svg" > "$ICON_DIR/scalable/places/$name"
done

# Copy Papirus mimetype icons at all available sizes
for size in 16x16 22x22 32x32 48x48 64x64 96x96 128x128; do
  if [ -d "$PAPIRUS/$size/mimetypes" ]; then
    rm -rf "$ICON_DIR/$size/mimetypes"
    mkdir -p "$ICON_DIR/$size/mimetypes"
    cp -rL "$PAPIRUS/$size/mimetypes"/* "$ICON_DIR/$size/mimetypes/"
  fi
done

# Update icon cache
gtk-update-icon-cache -f "$ICON_DIR" 2>/dev/null || true

# Map wallpaper palette to closest GNOME accent color
# Primary color RGB: {{colors.primary.default.red}}, {{colors.primary.default.green}}, {{colors.primary.default.blue}}
R={{colors.primary.default.red}}
G={{colors.primary.default.green}}
B={{colors.primary.default.blue}}

# Simple hue-based mapping to GNOME accent presets
if [ "$R" -gt "$G" ] && [ "$R" -gt "$B" ]; then
  if [ "$B" -gt "$((G + 30))" ]; then
    ACCENT="purple"
  elif [ "$G" -gt "$((R / 2))" ]; then
    ACCENT="orange"
  else
    if [ "$B" -gt "$((R / 3))" ]; then
      ACCENT="pink"
    else
      ACCENT="red"
    fi
  fi
elif [ "$G" -gt "$R" ] && [ "$G" -gt "$B" ]; then
  if [ "$B" -gt "$((R + 20))" ]; then
    ACCENT="teal"
  else
    ACCENT="green"
  fi
elif [ "$B" -gt "$R" ] && [ "$B" -gt "$G" ]; then
  if [ "$R" -gt "$((B / 2))" ]; then
    ACCENT="purple"
  else
    ACCENT="blue"
  fi
else
  ACCENT="slate"
fi

gsettings set org.gnome.desktop.interface accent-color "$ACCENT"

# Merge VSCodium color customizations into settings.json
VSCODE_SETTINGS="$HOME/.config/VSCodium/User/settings.json"
VSCODE_COLORS="$HOME/.local/share/matugen/vscodium-colors.json"
if [ -f "$VSCODE_SETTINGS" ] && [ -f "$VSCODE_COLORS" ]; then
  COLORS=$(cat "$VSCODE_COLORS")
  # Remove existing workbench.colorCustomizations and merge new ones
  TMP=$(mktemp)
  if command -v jq &>/dev/null; then
    jq --argjson colors "$COLORS" '. * $colors' "$VSCODE_SETTINGS" > "$TMP" && mv "$TMP" "$VSCODE_SETTINGS"
  fi
fi

# Reload GNOME Shell theme to apply new colors
dconf write /org/gnome/shell/extensions/user-theme/name "''"
sleep 1
dconf write /org/gnome/shell/extensions/user-theme/name "'WallpaperShell'"
