#!/usr/bin/env bash
# Запуск Jagged Alliance 3 (GOG) без Steam client.
set -euo pipefail
GOG_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=config.sh
source "${GOG_DIR}/config.sh"
# shellcheck source=gog-process.sh
source "${GOG_DIR}/gog-process.sh"

JA3_WIN='C:\GOG Games\Jagged Alliance 3\JA3.exe'
JA3_UNIX="${BOTTLE_DIR}/drive_c/GOG Games/Jagged Alliance 3"
JA3_EXE="${JA3_UNIX}/JA3.exe"
GALAXY_SVC='C:\Program Files (x86)\GOG Galaxy\GalaxyClientService.exe'

if [[ ! -f "${JA3_EXE}" ]]; then
  echo "❌ Нет ${JA3_EXE}"
  echo "   Установите: bash ${GOG_DIR}/install-ja3-innoextract.sh"
  exit 1
fi

bash "${GOG_DIR}/emergency-stop.sh" >/dev/null 2>&1 || gog_kill_all
sleep 2

if [[ "${JA3_IGNORE_SWAP:-0}" == "1" ]]; then
  echo "⚠️  JA3_IGNORE_SWAP=1 — swap $(gog_swap_used_mb 2>/dev/null || echo '?')MB"
elif ! gog_swap_guard_ja3_launch "${JA3_SWAP_SOFT_MB:-1200}" "${JA3_SWAP_HARD_MB:-4096}"; then
  used="$(gog_swap_used_mb 2>/dev/null || echo '?')"
  echo ""
  echo "Swap ~${used}MB — bash ${GOG_DIR}/reset-ja3-crossover.sh или reboot Mac"
  echo "Или: JA3_IGNORE_SWAP=1 bash ${GOG_DIR}/run-ja3.sh"
  exit 1
fi

gog_assert_no_steam_client || exit 1

if ! ja3_gptk_available && ! ja3_bottle_uses_d3dmetal; then
  if [[ -d "/Applications/Game Porting Toolkit.app" ]]; then
    bash "${GOG_DIR}/install-gptk-whisky.sh" >/dev/null 2>&1 || true
    bash "${GOG_DIR}/configure-ja3-d3dmetal.sh" >/dev/null 2>&1 || true
  else
    echo ""
    echo "⚠️  JA3 нужен D3D12 (GPTK). Установите:"
    echo "   brew install --cask gcenx/wine/game-porting-toolkit"
    echo "   bash ${GOG_DIR}/install-gptk-whisky.sh && bash ${GOG_DIR}/configure-ja3-d3dmetal.sh"
    echo ""
  fi
fi

CX="/Applications/CrossOver.app/Contents/SharedSupport/CrossOver"
if [[ ! -x "${CX}/bin/wine" ]]; then
  :
elif [[ ! -f "${HOME}/Library/Application Support/CrossOver/Bottles/JA3-GOG/drive_c/GOG Games/Jagged Alliance 3/JA3.exe" ]]; then
  echo ""
  echo "ℹ️  JA3 (D3D12) на Mac: Whisky/Gcenx не поддерживает D3DMetal."
  echo "   brew install --cask crossover"
  echo "   bash ${GOG_DIR}/setup-ja3-crossover.sh && bash ${GOG_DIR}/run-ja3-crossover.sh"
  echo ""
fi

# Whisky D3DMetal path — не работает для JA3/D3D12 на Gcenx (см. JA3_MAC_WITHOUT_GALAXY.md)
if ja3_gptk_available || ja3_bottle_uses_d3dmetal; then
  bash "${GOG_DIR}/prepare-ja3-d3dmetal.sh" >/dev/null 2>&1 || true
fi
bash "${GOG_DIR}/finish-ja3-layout.sh" >/dev/null 2>&1 || true
if ja3_gptk_available || ja3_bottle_uses_d3dmetal; then
  :
else
  bash "${GOG_DIR}/force-ja3-d3d11.sh" >/dev/null 2>&1 || true
fi

export_wine_env_ja3
gog_wine_check

# Фоновый Galaxy service (Music/Matchmaking wrapper — не Steam)
if [[ -f "${BOTTLE_DIR}/drive_c/Program Files (x86)/GOG Galaxy/GalaxyClientService.exe" ]]; then
  if ! pgrep -f 'GalaxyClientService' >/dev/null 2>&1; then
    nohup "${WINE64}" "${GALAXY_SVC}" >>/tmp/ja3-galaxy-service.log 2>&1 &
    sleep 2
  fi
fi

if ja3_gptk_available || ja3_bottle_uses_d3dmetal; then
  echo "⚠️  Whisky D3DMetal не поддерживает JA3/D3D12 (Gcenx без D3DMetal hooks)."
  echo "   Используйте: bash ${GOG_DIR}/run-ja3-crossover.sh"
  echo ""
fi
if ja3_gptk_available || ja3_bottle_uses_d3dmetal; then
  echo "Запуск JA3 (Whisky — эксперимент; D3D12 → CrossOver)…"
else
  echo "Запуск JA3 (GOG, DXVK D3D11 fallback; без Steam.app)…"
fi
echo "Cmd+Tab → wine64. В графике: отключить heat haze."
echo "(лог «Steam … GOG» = GOG API-шим, не Steam client)"
if ja3_gptk_available || ja3_bottle_uses_d3dmetal; then
  _ja3_args="${JA3_EXTRA_ARGS:--unpacked}"
  export MTL_HUD_ENABLED="${MTL_HUD_ENABLED:-0}"
else
  _ja3_args="${JA3_EXTRA_ARGS:--unpacked -graphicsapi d3d11}"
fi
# start /d — CWD = папка игры (CommonLua/, иначе «graphics subsystem»/autorun.lua из Whisky Run)
# innoextract не запускает Inno-постскрипты; -unpacked + CommonLua на диске обязательны
# shellcheck disable=SC2086
exec "${WINE64}" start /d "C:\\GOG Games\\Jagged Alliance 3" "${JA3_WIN}" ${_ja3_args}
