#!/usr/bin/env bash
# =============================================================================
# CRIMSON SLIM UI  —  demo (no root required)
# Lessons from common autoscript menus (znand / Krainium / AutoScriptX style):
#   • One screen, not a wall of text
#   • ONLINE / OFFLINE on a single status strip
#   • Two-column actions
#   • System line: IP · RAM · uptime
#   • Fast redraw, no heavy API calls in the header
# =============================================================================
export LC_ALL=C.UTF-8 2>/dev/null || true
[ -t 1 ] || { echo "Use a real terminal"; exit 1; }

# ── colors ──────────────────────────────────────────────────────────────────
R=$'\e[38;5;196m'; G=$'\e[38;5;46m'; Y=$'\e[38;5;214m'
C=$'\e[38;5;203m'; W=$'\e[1;37m'; D=$'\e[2m'; N=$'\e[0m'
X=$'\e[38;5;240m'

# ── tiny helpers (demo-safe: no root) ───────────────────────────────────────
on()  { printf '%sON %s'  "$G" "$N"; }
off() { printf '%sOFF%s' "$R" "$N"; }

svc() {
  # real if systemctl exists and user can query; else demo ON
  local u=$1
  if command -v systemctl >/dev/null 2>&1; then
    systemctl is-active --quiet "$u" 2>/dev/null && { on; return; }
    # unit missing → demo mode looks "up" so UI still reads well offline
    systemctl status "$u" >/dev/null 2>&1 || { on; return; }
    off; return
  fi
  on
}

ram_line() {
  local t a
  read -r t a < <(free -m 2>/dev/null | awk '/^Mem:/{print $2,$7}') || { echo "n/a"; return; }
  [ "${t:-0}" -gt 0 ] 2>/dev/null || { echo "n/a"; return; }
  printf '%sM free / %sM' "$a" "$t"
}

ip_line() {
  hostname -I 2>/dev/null | awk '{print $1; exit}' || echo "n/a"
}

up_line() {
  uptime -p 2>/dev/null | sed 's/^up //' || echo "n/a"
}

# ── frame ───────────────────────────────────────────────────────────────────
WDT=62
top() { printf '%s┌' "$R"; printf '%.0s─' $(seq 1 $((WDT-2))); printf '┐%s\n' "$N"; }
bot() { printf '%s└' "$R"; printf '%.0s─' $(seq 1 $((WDT-2))); printf '┘%s\n' "$N"; }
mid() { printf '%s├' "$R"; printf '%.0s─' $(seq 1 $((WDT-2))); printf '┤%s\n' "$N"; }
row() {
  # row "plain width text" "colored text"
  local plain=$1 col=${2:-$1}
  local pad=$((WDT-4-${#plain}))
  [ "$pad" -lt 0 ] && pad=0
  printf '%s│%s  %s%*s%s│%s\n' "$R" "$N" "$col" "$pad" "" "$R" "$N"
}

draw() {
  local ip ram up
  ip=$(ip_line); ram=$(ram_line); up=$(up_line)
  local n_ngx n_xray n_ssh n_cert
  n_ngx=$(svc nginx)
  n_xray=$(svc xray)
  n_ssh=$(svc crimson-ssh-ws 2>/dev/null; svc ssh 2>/dev/null | head -1)
  # simplify badges without nested cmd subst mess
  n_ngx=$(systemctl is-active --quiet nginx 2>/dev/null && on || off)
  n_xray=$(systemctl is-active --quiet xray 2>/dev/null && on || off)
  n_ssh=$(systemctl is-active --quiet crimson-ssh-ws 2>/dev/null && on || systemctl is-active --quiet ssh 2>/dev/null && on || off)
  # demo fallback when nothing installed: show ON so layout is visible
  if ! command -v systemctl >/dev/null 2>&1; then
    n_ngx=$(on); n_xray=$(on); n_ssh=$(on)
  elif ! systemctl list-unit-files nginx.service >/dev/null 2>&1; then
    n_ngx=$(on); n_xray=$(on); n_ssh=$(on)
  fi
  n_cert="${G}OK${N}"

  clear
  top
  row "  CRIMSON GATE          slim ui  ·  demo" \
      "  ${W}CRIMSON GATE${N}          ${D}slim ui  ·  demo${N}"
  mid
  row "  $ip  ·  $ram  ·  $up" \
      "  ${C}${ip}${N}  ·  ${ram}  ·  ${D}${up}${N}"
  mid
  # status strip — autoscript style: name + ON/OFF in one glance
  row "  Nginx  ON    Xray  ON    SSH-WS  ON    Cert  OK" \
      "  Nginx  ${n_ngx}   Xray  ${n_xray}   SSH-WS ${n_ssh}   Cert  ${n_cert}"
  mid
  row "  SETUP" "${C}  SETUP${N}"
  row "  1 Install / Reinstall     2 Links" \
      "  ${W}1${N} Install / Reinstall     ${W}2${N} Links"
  row "  3 Certificate             4 Remote Proxy" \
      "  ${W}3${N} Certificate             ${W}4${N} Remote Proxy"
  mid
  row "  PROTOCOLS" "${C}  PROTOCOLS${N}"
  row "  5 VLESS      6 VLESS none-TLS" \
      "  ${W}5${N} VLESS      ${W}6${N} VLESS none-TLS"
  row "  7 VMess      8 Trojan     9 SS" \
      "  ${W}7${N} VMess      ${W}8${N} Trojan     ${W}9${N} SS"
  row "  10 SSH-WS" \
      "  ${W}10${N} SSH-WS"
  mid
  row "  SYSTEM" "${C}  SYSTEM${N}"
  row "  11 Status     12 Test      13 Audit" \
      "  ${W}11${N} Status     ${W}12${N} Test      ${W}13${N} Audit"
  row "  14 Restart    15 Update    0  Exit" \
      "  ${W}14${N} Restart    ${W}15${N} Update    ${W}0${N}  Exit"
  bot
  printf '  %s▸%s compact · status first · two columns · no clutter\n' "$C" "$N"
}

main() {
  while true; do
    draw
    read -r -p "  › " c || exit 0
    case "$c" in
      0|q|Q) clear; printf '%sbye%s\n' "$D" "$N"; exit 0 ;;
      *) printf '  %s→%s %s %s(demo only)%s\n' "$Y" "$N" "$c" "$D" "$N"; sleep 0.55 ;;
    esac
  done
}
main

