@echo off
setlocal EnableExtensions EnableDelayedExpansion

echo ============================================
echo  Blastoise Native Android - Build and Launch
echo ============================================
echo.

set "ROOT_DIR=%~dp0"
set "PROJECT_DIR=%ROOT_DIR%android\BlastoiseNative"
set "APK_PATH=%PROJECT_DIR%\app\build\outputs\apk\debug\app-debug.apk"
set "PACKAGE_ACTIVITY=com.peterino.blastoise/.MainActivity"

if "%~1"=="" (
    set "AVD_NAME=Medium_Phone_API_36.0"
) else (
    set "AVD_NAME=%~1"
)

set "PATH=%USERPROFILE%\scoop\shims;%PATH%"
if not defined JAVA_HOME (
    if exist "%USERPROFILE%\scoop\apps\temurin17-jdk\current" (
        set "JAVA_HOME=%USERPROFILE%\scoop\apps\temurin17-jdk\current"
    )
)
if not defined ANDROID_HOME set "ANDROID_HOME=%LOCALAPPDATA%\Android\Sdk"
set "PATH=%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\emulator;%ANDROID_HOME%\cmdline-tools\latest\bin;%PATH%"
set "ADB_EXE=%ANDROID_HOME%\platform-tools\adb.exe"
set "EMULATOR_EXE=%ANDROID_HOME%\emulator\emulator.exe"

if not exist "%PROJECT_DIR%\gradlew.bat" (
    echo ERROR: Native Android project not found:
    echo   %PROJECT_DIR%
    goto :fail
)

if not exist "%ADB_EXE%" (
    echo ERROR: adb not found at:
    echo   %ADB_EXE%
    echo Run android\dev-setup.bat first.
    goto :fail
)

if not exist "%EMULATOR_EXE%" (
    echo ERROR: Android emulator not found at:
    echo   %EMULATOR_EXE%
    echo Run android\dev-setup.bat first.
    goto :fail
)

"%EMULATOR_EXE%" -list-avds | findstr /X /C:"%AVD_NAME%" >nul
if errorlevel 1 (
    echo ERROR: AVD "%AVD_NAME%" was not found.
    echo.
    echo Available AVDs:
    "%EMULATOR_EXE%" -list-avds
    echo.
    echo Pass a different AVD name as the first argument, for example:
    echo   run-android-native.bat Your_AVD_Name
    goto :fail
)

echo Building debug APK...
pushd "%PROJECT_DIR%"
call gradlew.bat :app:assembleDebug
if errorlevel 1 (
    popd
    echo.
    echo ERROR: Gradle build failed.
    goto :fail
)
popd

if not exist "%APK_PATH%" (
    echo ERROR: APK was not created:
    echo   %APK_PATH%
    goto :fail
)

"%ADB_EXE%" devices | findstr /R /C:"emulator-[0-9][0-9]*[	 ]*device" >nul
if errorlevel 1 (
    echo.
    echo Starting emulator "%AVD_NAME%"...
    start "" "%EMULATOR_EXE%" -avd "%AVD_NAME%"
    echo Waiting for emulator device...
    set /a WAIT_DEVICE_COUNT=0
    :wait_device
    "%ADB_EXE%" devices | findstr /R /C:"emulator-[0-9][0-9]*[	 ]*device" >nul
    if errorlevel 1 (
        set /a WAIT_DEVICE_COUNT+=1
        if !WAIT_DEVICE_COUNT! GEQ 90 (
            echo ERROR: Emulator did not appear within 180 seconds.
            goto :fail
        )
        timeout /t 2 /nobreak >nul
        goto :wait_device
    )
)

echo Waiting for Android boot completion...
set /a WAIT_BOOT_COUNT=0
:wait_boot
set "BOOT_DONE="
for /f "delims=" %%b in ('"%ADB_EXE%" shell getprop sys.boot_completed 2^>nul') do set "BOOT_DONE=%%b"
if not "%BOOT_DONE%"=="1" (
    set /a WAIT_BOOT_COUNT+=1
    if !WAIT_BOOT_COUNT! GEQ 120 (
        echo ERROR: Android did not finish booting within 240 seconds.
        goto :fail
    )
    timeout /t 2 /nobreak >nul
    goto :wait_boot
)

echo Installing APK...
"%ADB_EXE%" install -r "%APK_PATH%"
if errorlevel 1 (
    echo.
    echo ERROR: APK install failed.
    goto :fail
)

echo Launching app...
"%ADB_EXE%" shell am start -n %PACKAGE_ACTIVITY%
if errorlevel 1 (
    echo.
    echo ERROR: App launch failed.
    goto :fail
)

echo.
echo ============================================
echo  Blastoise is built, installed, and running.
echo ============================================
goto :done

:fail
echo.
echo ============================================
echo  Failed.
echo ============================================
if not "%NO_PAUSE%"=="1" pause
exit /b 1

:done
if not "%NO_PAUSE%"=="1" pause
exit /b 0
