#!/usr/bin/env bash

module_description() {
  printf "SDDM Theme - install custom login theme with current colors\n"
}

module_required() { return 1; }
module_should_skip() {
  if have sddm; then
    return 1
  fi
  return 0
}

module_prereqs() {
  if ! have sddm; then
    log_warn "SDDM is not installed. Install with: sudo pacman -S sddm"
    return 1
  fi
}

module_main() {
  log_section "SDDM Theme Installation"

  local sddm_theme_dir="$OMERON_PROJECT_DIR/dotfiles/hypr/sddm-theme"

  if [[ ! -d "$sddm_theme_dir/pascal-hypr" ]]; then
    log_warn "SDDM theme not found at $sddm_theme_dir"
    return 1
  fi

  log_info "Installing SDDM theme..."
  sudo_run mkdir -p /usr/share/sddm/themes /etc/sddm.conf.d

  if [[ -d "/usr/share/sddm/themes/pascal-hypr" ]]; then
    sudo_run rm -rf "/usr/share/sddm/themes/pascal-hypr"
  fi

  sudo_run cp -a "$sddm_theme_dir/pascal-hypr" /usr/share/sddm/themes/

  sudo_run mkdir -p /var/lib/pascal-sddm-theme
  sudo_run chown "$(id -u):$(id -g)" /var/lib/pascal-sddm-theme

  local current_theme_conf="$HOME/.config/hypr/current-theme.conf"
  local current_wallpaper="$HOME/.config/hypr/current-wallpaper"
  local hyprpaper_conf="$HOME/.config/hypr/hyprpaper.conf"

  local wallpaper=""
  local accent="#f38ba8"
  local accent2="#cba6f7"
  local background="#18141f"
  local panel="#313244"
  local foreground="#f5e0dc"
  local muted="#cdd6f4"
  local selected="#11111b"
  local theme_name="Pascal Hypr"

  if [[ -f "$current_wallpaper" ]]; then
    wallpaper="$(< "$current_wallpaper")"
  fi

  if [[ -f "$current_theme_conf" ]]; then
    local line
    while IFS= read -r line; do
      [[ "$line" =~ ^# ]] && continue
      [[ "$line" =~ ^[[:space:]]*$ ]] && continue
      if [[ "$line" =~ col\.active_border[[:space:]]*=[[:space:]]*(.*) ]]; then
        local border="${BASH_REMATCH[1]}"
        border="${border#rgba(}"
        border="${border%ee)}"
        accent="#${border:0:2}${border:2:2}${border:4:2}"
      fi
    done < "$current_theme_conf"
  fi

  if [[ -f "$hyprpaper_conf" ]]; then
    if [[ -z "$wallpaper" ]]; then
      local wl
      wl="$(grep 'path' "$hyprpaper_conf" 2>/dev/null | head -1 | sed 's/.*=[[:space:]]*//' | tr -d ' "')"
      [[ -n "$wl" ]] && wallpaper="$wl"
    fi

    if [[ "$wallpaper" == /home/pascal/* ]]; then
      wallpaper="${wallpaper/#\/home\/pascal/$HOME}"
    fi
  fi

  if [[ -n "$wallpaper" && -f "$wallpaper" ]]; then
    local ext="${wallpaper##*.}"
    [[ "$ext" == "$wallpaper" ]] && ext="jpg"
    sudo_run cp "$wallpaper" "/var/lib/pascal-sddm-theme/wallpaper.$ext"
  fi

  local theme_conf="/usr/share/sddm/themes/pascal-hypr/theme.conf"
  sudo_run tee "$theme_conf" >/dev/null <<SDDMEOF
[General]
background=/var/lib/pascal-sddm-theme/wallpaper.jpg
themeName=$theme_name
accent=$accent
accent2=$accent2
backgroundColor=$background
panelColor=$panel
foreground=$foreground
muted=$muted
selectedText=$selected
SDDMEOF

  if [[ -f "$sddm_theme_dir/sddm.conf" ]]; then
    sudo_run cp -a "$sddm_theme_dir/sddm.conf" /etc/sddm.conf.d/90-pascal-hypr.conf
  fi

  log_success "SDDM theme installed"

  if tui_confirm "Enable SDDM as display manager?"; then
    sudo_run systemctl enable sddm --now 2>/dev/null || log_warn "Could not enable SDDM"
  fi
}
