@echo off

echo ============================================
echo  Blastoise Native Android - Run App
echo ============================================
echo.

:: Ensure Scoop shims and SDK tools are in PATH
set "PATH=%PATH%;%USERPROFILE%\scoop\shims"
set "JAVA_HOME=%USERPROFILE%\scoop\apps\temurin17-jdk\current"
set "ANDROID_HOME=%LOCALAPPDATA%\Android\Sdk"
set "PATH=%PATH%;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\emulator;%ANDROID_HOME%\cmdline-tools\latest\bin"
set "PROJECT_DIR=%~dp0BlastoiseNative"

:: Start emulator if not already running
adb devices 2>nul | findstr "emulator" >nul
if errorlevel 1 (
    echo Starting emulator...
    start "" "%ANDROID_HOME%\emulator\emulator.exe" -avd Medium_Phone_API_36
    echo Waiting for emulator to boot...
    adb wait-for-device
    :wait_boot
    adb shell getprop sys.boot_completed 2>nul | findstr "1" >nul
    if errorlevel 1 (
        timeout /t 2 /nobreak >nul
        goto :wait_boot
    )
    echo Emulator ready.
    echo.
)

echo Building and installing Blastoise...
echo.
pushd "%PROJECT_DIR%"
call gradlew.bat :app:installDebug
if errorlevel 1 (
    popd
    echo.
    echo Build or install failed.
    pause
    exit /b 1
)
popd

echo Launching app...
adb shell am start -n com.peterino.blastoise/.MainActivity

echo.
echo ============================================
echo  App is installed and launched.
echo ============================================
pause
