@echo off
setlocal

:: Haal systeempaden op via PowerShell (werkt altijd, ook via PowerShell gestart)
for /f "delims=" %%i in ('powershell.exe -NoProfile -Command "$env:USERPROFILE"') do set "USERPROFILE=%%i"
for /f "delims=" %%i in ('powershell.exe -NoProfile -Command "$env:LOCALAPPDATA"') do set "LOCALAPPDATA=%%i"
for /f "delims=" %%i in ('powershell.exe -NoProfile -Command "$env:APPDATA"') do set "APPDATA=%%i"
for /f "delims=" %%i in ('powershell.exe -NoProfile -Command "$env:TEMP"') do set "TEMP=%%i"
for /f "delims=" %%i in ('powershell.exe -NoProfile -Command "$env:PROGRAMFILES"') do set "PROGRAMFILES=%%i"

set "BASE_URL=https://python.coderdojohasselt.be"

echo =============================================
echo  CoderDojo Python - Een-klik installatie
echo =============================================
echo.
echo Dit script installeert Python, uv, Thonny en alle game-bibliotheken.
echo Het downloadt ook de startercode voor alle 10 sessies.
echo.
echo De installatie duurt 5-10 minuten, afhankelijk van je internet.
echo.

:: ---------------------------------------------------------------
:: Kies installatiemap
:: ---------------------------------------------------------------
if not "%~1"=="" goto :have_dir

    set /p USER_DIR="In welke map wil je CoderDojo installeren? [%USERPROFILE%\Documents\cdj_python]: "
    if "%USER_DIR%"=="" (set "CODERDOJO_DIR=%USERPROFILE%\Documents\cdj_python") else (set "CODERDOJO_DIR=%USER_DIR%")
goto :dir_done

:have_dir
set "CODERDOJO_DIR=%~1"

:dir_done
echo.
echo Installatiemap: %CODERDOJO_DIR%
echo.
pause

:: ---------------------------------------------------------------
:: Stap 1: Python 3.13
:: ---------------------------------------------------------------
echo [1/6] Python 3.13...

set "PYTHON_EXE=%LOCALAPPDATA%\Programs\Python\Python313\python.exe"
if exist "%PYTHON_EXE%" goto :python_found
    echo Python 3.13 downloaden en installeren...
    curl.exe -L --connect-timeout 30 --max-time 600 --retry 2 -o "%TEMP%\python-3.13-installer.exe" "https://www.python.org/ftp/python/3.13.5/python-3.13.5-amd64.exe"
    if errorlevel 1 goto :err_python_dl
    "%TEMP%\python-3.13-installer.exe" /quiet InstallAllUsers=0 PrependPath=1 Include_test=0
    del "%TEMP%\python-3.13-installer.exe"
    echo Python 3.13 geinstalleerd.
:python_found

:: ---------------------------------------------------------------
:: Stap 2: uv
:: ---------------------------------------------------------------
echo [2/6] uv...

set "UV_EXE=%USERPROFILE%\.cargo\bin\uv.exe"
if not exist "%UV_EXE%" set "UV_EXE=%USERPROFILE%\.local\bin\uv.exe"

if exist "%UV_EXE%" goto :uv_found
    echo uv installeren...
    powershell.exe -ExecutionPolicy Bypass -Command "irm https://astral.sh/uv/install.ps1 | iex"
    if errorlevel 1 goto :err_uv
    :: uv 0.11+ installeert naar .local, ouder naar .cargo
    set "UV_EXE=%USERPROFILE%\.local\bin\uv.exe"
    if not exist "%UV_EXE%" set "UV_EXE=%USERPROFILE%\.cargo\bin\uv.exe"
    echo uv geinstalleerd.
:uv_found
echo UV_EXE = %UV_EXE%

:: ---------------------------------------------------------------
:: Stap 3: Project + virtuele omgeving
:: ---------------------------------------------------------------
echo [3/6] Project en virtuele omgeving aanmaken...

if not exist "%CODERDOJO_DIR%" mkdir "%CODERDOJO_DIR%"
cd /d "%CODERDOJO_DIR%"
if errorlevel 1 goto :err_cd

if exist "pyproject.toml" goto :project_exists
    (
        echo [project]
        echo name = "coderdojo"
        echo version = "0.1.0"
        echo requires-python = ">=3.13,<3.14"
        echo dependencies = ["pgzero", "pygame", "pillow"]
    ) > "pyproject.toml"
:project_exists

"%UV_EXE%" sync --python "%PYTHON_EXE%"
if errorlevel 1 goto :err_sync

set "VENV_PYTHON=%CODERDOJO_DIR%\.venv\Scripts\python.exe"
if exist "%VENV_PYTHON%" goto :venv_ok
    echo FOUT: Virtuele omgeving niet aangemaakt.
    echo Verwacht pad: %VENV_PYTHON%
    pause
    exit /b 1
:venv_ok

:: ---------------------------------------------------------------
:: Stap 4: Thonny
:: ---------------------------------------------------------------
echo [4/6] Thonny...

set "THONNY_EXE=%LOCALAPPDATA%\Programs\Thonny\thonny.exe"
if exist "%THONNY_EXE%" goto :thonny_found
set "THONNY_EXE=%LOCALAPPDATA%\Programs\Thonny\bin\thonny.exe"
if exist "%THONNY_EXE%" goto :thonny_found
set "THONNY_EXE=%ProgramFiles%\Thonny\thonny.exe"
if exist "%THONNY_EXE%" goto :thonny_found
set "THONNY_EXE=%ProgramFiles%\Thonny\bin\thonny.exe"
if exist "%THONNY_EXE%" goto :thonny_found

    echo.
    echo =============================================
    echo  Thonny is niet geinstalleerd!
    echo  Open https://thonny.org in je browser
    echo  en download Thonny voor Windows.
    echo  Installeer Thonny en start dit script opnieuw.
    echo =============================================
    start "" "https://thonny.org"
    pause
    exit /b 1

:thonny_found
echo Thonny gevonden: %THONNY_EXE%

:: ---------------------------------------------------------------
:: Stap 5: Startercode downloaden
:: ---------------------------------------------------------------
echo [5/6] Startercode downloaden...

call :dl 01-catch-the-stars
call :dl 02-dodge-meteors
call :dl 03-space-blaster
call :dl 04-pong
call :dl 05-breakout
call :dl 06-platformer
call :dl 07-bug-zapper
call :dl 08-sky-highway
call :dl 09-boss-battle
call :dl 10-my-game
echo Startercode gedownload naar %CODERDOJO_DIR%

:: ---------------------------------------------------------------
:: Stap 6: Thonny configureren en openen
:: ---------------------------------------------------------------
echo [6/6] Thonny configureren en openen...

set "SESSION1_DIR=%CODERDOJO_DIR%\01-catch-the-stars"
if not exist "%SESSION1_DIR%" set "SESSION1_DIR=%CODERDOJO_DIR%"

if not exist "%APPDATA%\Thonny" mkdir "%APPDATA%\Thonny"

(
    echo [run]
    echo interpreter = %VENV_PYTHON%
    echo backend_name = LocalCPython
    echo working_directory = %SESSION1_DIR%
    echo.
    echo [LocalCPython]
    echo executable = %VENV_PYTHON%
    echo.
    echo [view]
    echo FilesView.visible = True
) > "%APPDATA%\Thonny\configuration.ini"

cd /d "%SESSION1_DIR%"
start "" "%THONNY_EXE%"

:: ---------------------------------------------------------------
:: Klaar
:: ---------------------------------------------------------------
echo.
echo =============================================
echo  Installatie voltooid!
echo.
echo  Thonny opent in les 1 (01-catch-the-stars).
echo  De virtuele omgeving is ingesteld als interpreter.
echo  Open main.py en klik de groene Play-knop.
echo.
echo  Je kunt ook de terminal gebruiken:
echo  cd %CODERDOJO_DIR%\01-catch-the-stars
echo  uv run main.py
echo.
echo  Veel plezier op de CoderDojo!
echo =============================================
pause
exit /b 0

:: ---------------------------------------------------------------
:: Foutafhandeling
:: ---------------------------------------------------------------
:err_python_dl
echo FOUT: Kon Python niet downloaden. Controleer je internetverbinding.
pause
exit /b 1

:err_uv
echo FOUT: Kon uv niet installeren.
echo Probeer handmatig: https://docs.astral.sh/uv/getting-started/installation/
pause
exit /b 1

:err_cd
echo FOUT: Kan niet naar %CODERDOJO_DIR% gaan.
echo Controleer of het pad bestaat en je schrijfrechten hebt.
pause
exit /b 1

:err_sync
echo FOUT: uv sync mislukt.
echo Controleer of Python 3.13 correct is geinstalleerd.
pause
exit /b 1

:: ---------------------------------------------------------------
:: Subroutine: download een sessie
:: ---------------------------------------------------------------
:dl
set "SDIR=%CODERDOJO_DIR%\%~1"
if exist "%SDIR%" (
    echo   %~1 al aanwezig - overslaan.
    exit /b 0
)
echo   %~1 downloaden...
curl.exe -L -f --connect-timeout 30 -o "%TEMP%\%~1.zip" "%BASE_URL%/sessions/%~1/starter.zip" 2>nul
if errorlevel 1 (
    echo   %~1 nog niet beschikbaar - overslaan.
    exit /b 0
)
powershell.exe -Command "Expand-Archive -Path '%TEMP%\%~1.zip' -DestinationPath '%SDIR%' -Force"
del "%TEMP%\%~1.zip"
exit /b 0
