@echo off

echo ============================================
echo  Blastoise Native Android - Dev Setup
echo  Installs all prerequisites via Scoop
echo ============================================
echo.

:: Ensure Scoop shims are in PATH
set "PATH=%PATH%;%USERPROFILE%\scoop\shims"

:: Step 1: Scoop
where scoop >nul 2>&1
if errorlevel 1 (
    echo [1/5] Installing Scoop...
    powershell -ExecutionPolicy Bypass -Command "Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force; iwr -useb get.scoop.sh | iex"
    set "PATH=%PATH%;%USERPROFILE%\scoop\shims"
    where scoop >nul 2>&1
    if errorlevel 1 (
        echo ERROR: Scoop installation failed. Install manually from https://scoop.sh
        pause
        exit /b 1
    )
    echo Scoop installed.
) else (
    echo [1/5] Scoop already installed.
)
echo.

:: Step 2: Buckets
echo [2/5] Adding Scoop buckets...
call scoop bucket add java 2>nul
call scoop bucket add extras 2>nul
echo Done.
echo.

:: Step 3: Install tools
echo [3/5] Installing JDK 17 and Android CLI tools...
echo        This may take several minutes.
echo.

call scoop install temurin17-jdk
if errorlevel 1 (
    echo ERROR: Failed to install JDK 17
    pause & exit /b 1
)

call scoop install android-clt
if errorlevel 1 (
    echo ERROR: Failed to install Android CLI tools
    pause & exit /b 1
)

echo All tools installed.
echo.

:: Step 4: Environment variables
echo [4/5] Setting environment variables...

set "JAVA_HOME=%USERPROFILE%\scoop\apps\temurin17-jdk\current"
if not exist "%JAVA_HOME%" (
    for /f "delims=" %%i in ('where java 2^>nul') do (
        set "JAVA_PATH=%%i"
        goto :found_java
    )
    :found_java
    if defined JAVA_PATH (
        for %%i in ("%JAVA_PATH%") do set "JAVA_HOME=%%~dpi.."
    )
)

set "ANDROID_HOME=%LOCALAPPDATA%\Android\Sdk"

echo        JAVA_HOME    = %JAVA_HOME%
echo        ANDROID_HOME = %ANDROID_HOME%

powershell -Command "[Environment]::SetEnvironmentVariable('JAVA_HOME', '%JAVA_HOME%', 'User')"
powershell -Command "[Environment]::SetEnvironmentVariable('ANDROID_HOME', '%ANDROID_HOME%', 'User')"

set "SDK_TOOLS=%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\cmdline-tools\latest\bin"
powershell -Command "$p = [Environment]::GetEnvironmentVariable('Path','User'); if($p -notlike '*platform-tools*'){[Environment]::SetEnvironmentVariable('Path', $p + ';%SDK_TOOLS%', 'User')}"
set "PATH=%PATH%;%SDK_TOOLS%"
echo Done.
echo.

:: Step 5: Android SDK (headless via sdkmanager)
echo [5/5] Installing Android SDK components...

set "SDKMANAGER=%ANDROID_HOME%\cmdline-tools\latest\bin\sdkmanager.bat"

if not exist "%SDKMANAGER%" (
    echo        sdkmanager not found at:
    echo        %SDKMANAGER%
    echo.
    echo        Trying Scoop android-clt path...
    set "SDKMANAGER=%USERPROFILE%\scoop\apps\android-clt\current\bin\sdkmanager.bat"
)

if not exist "%SDKMANAGER%" (
    echo        ERROR: sdkmanager not found.
    echo        Install android-clt via Scoop first.
    pause & exit /b 1
)

echo        Accepting licenses...
echo y | call "%SDKMANAGER%" --licenses >nul 2>&1

echo        Installing platform-tools, emulator, android-36, build-tools-36, and an API 36 system image...
call "%SDKMANAGER%" "platform-tools" "emulator" "platforms;android-36" "build-tools;36.0.0" "system-images;android-36;google_apis;x86_64"
echo Done.
echo.

echo        Creating CLI emulator if needed...
avdmanager list avd | findstr "Medium_Phone_API_36" >nul
if errorlevel 1 (
    echo no | avdmanager create avd -n Medium_Phone_API_36 -k "system-images;android-36;google_apis;x86_64" -d "medium_phone"
)
echo Done.
echo.

echo.
echo ============================================
echo  Setup complete!
echo.
echo  Tools installed via Scoop:
echo    JDK 17, Android CLI
echo.
echo  Android SDK installed headlessly:
echo    platform-tools, emulator, android-36, build-tools-36
echo.
echo  Environment:
echo    JAVA_HOME    = %JAVA_HOME%
echo    ANDROID_HOME = %ANDROID_HOME%
echo.
echo  NOTE: Restart your terminal for
echo        permanent env var changes.
echo ============================================

pause
