#!/usr/bin/env bash
# =============================================================
# SOLAR NET HUB — Panel v2 (lanzador + registro en menú)
# =============================================================
set -euo pipefail

REPO_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
PANEL_PY="$REPO_DIR/INSTALLER_V2/panel.py"
FONT_SRC="$REPO_DIR/INSTALLER_V2/Dune_Rise.otf"
ICON_SRC="$REPO_DIR/INSTALLER_V2/oasis-logo.png"

APPS_DIR="$HOME/.local/share/applications"
ICONS_DIR="$HOME/.local/share/icons/hicolor/256x256/apps"
DESKTOP_FILE="$APPS_DIR/solarnethub-panel.desktop"

C_RESET='\033[0m'; C_OK='\033[1;32m'; C_FAIL='\033[1;31m'
C_WARN='\033[1;33m'; C_INFO='\033[1;36m'

have(){ command -v "$1" >/dev/null 2>&1; }

check_display(){
  if [ -z "${DISPLAY:-}" ] && [ -z "${WAYLAND_DISPLAY:-}" ]; then
    echo -e "${C_FAIL}✘  Sin sesión gráfica (DISPLAY/WAYLAND_DISPLAY vacíos).${C_RESET}"
    exit 1
  fi
}

install_font(){
  local dst="$HOME/.local/share/fonts/Dune_Rise.otf"
  if [ -f "$FONT_SRC" ] && [ ! -f "$dst" ]; then
    mkdir -p "$HOME/.local/share/fonts"
    cp -f "$FONT_SRC" "$dst"
    fc-cache -f >/dev/null 2>&1 || true
    echo -e "${C_OK}✔  Fuente Dune Rise instalada.${C_RESET}"
  fi
}

register_desktop(){
  mkdir -p "$APPS_DIR" "$ICONS_DIR"

  # Copiar icono al directorio de iconos del usuario
  if [ -f "$ICON_SRC" ]; then
    cp -f "$ICON_SRC" "$ICONS_DIR/solarnethub.png"
  fi

  # Crear .desktop apuntando a este lanzador
  cat > "$DESKTOP_FILE" <<EOF
[Desktop Entry]
Type=Application
Name=Solar Net Hub
GenericName=OASIS & ECOIN Panel
Comment=Panel de control para OASIS y ECOIN
Exec=/bin/bash -lc '$REPO_DIR/start_panel_v2.sh'
Icon=solarnethub
Terminal=false
Categories=Network;System;Utility;
Keywords=oasis;ecoin;solar;hub;crypto;
StartupWMClass=solarnethub.panel.v2
StartupNotify=true
EOF

  chmod 644 "$DESKTOP_FILE"

  # Copiar también al Escritorio si existe
  local desktop_dir
  desktop_dir="$(xdg-user-dir DESKTOP 2>/dev/null || echo "$HOME/Desktop")"
  if [ -d "$desktop_dir" ]; then
    cp -f "$DESKTOP_FILE" "$desktop_dir/solarnethub-panel.desktop"
    chmod +x "$desktop_dir/solarnethub-panel.desktop" 2>/dev/null || true
  fi

  # Refrescar cache de aplicaciones e iconos
  have update-desktop-database && \
    update-desktop-database "$APPS_DIR" >/dev/null 2>&1 || true
  have gtk-update-icon-cache && \
    gtk-update-icon-cache -f "$HOME/.local/share/icons/hicolor" >/dev/null 2>&1 || true

  echo -e "${C_OK}✔  Registrado en el menú: 'Solar Net Hub'${C_RESET}"
}

check_gi(){
  python3 -c "import gi; gi.require_version('Gtk','3.0'); from gi.repository import Gtk" \
    >/dev/null 2>&1
}

install_gi(){
  echo -e "${C_WARN}⚠  python3-gi no encontrado. Instalando...${C_RESET}"
  if have apt-get;  then sudo apt-get install -y python3-gi python3-gi-cairo gir1.2-gtk-3.0
  elif have pacman; then sudo pacman -Sy --noconfirm python-gobject
  elif have dnf;    then sudo dnf install -y python3-gobject gtk3
  elif have zypper; then sudo zypper --non-interactive install python3-gobject gtk3
  else
    echo -e "${C_FAIL}✘  Gestor no soportado. Instala manualmente: python3-gi${C_RESET}"
    exit 1
  fi
}

# ── Main ──────────────────────────────────────────────────────────────────
echo -e "${C_INFO}== SOLAR NET HUB :: Panel v2 ==${C_RESET}"

check_display
install_font 2>/dev/null || true
register_desktop 2>/dev/null || true

if ! check_gi; then
  install_gi
  if ! check_gi; then
    echo -e "${C_FAIL}✘  No se pudo cargar python3-gi.${C_RESET}"
    exit 1
  fi
fi

echo -e "${C_OK}✔  Lanzando panel...${C_RESET}"
exec python3 "$PANEL_PY" "$@"
