@echo off
chcp 65001 >nul 2>&1
setlocal enabledelayedexpansion

echo ========================================
echo   Build - Install - Launch - Debug
echo ========================================
echo.

:: Check for rebuild flag
if "%1"=="rebuild" (
    echo [0/5] Rebuild requested - deleting tsslib.aar to recompile Go code...
    if exist "app\libs\tsslib.aar" (
        del /f "app\libs\tsslib.aar"
        echo [INFO] tsslib.aar deleted, will be rebuilt
    ) else (
        echo [INFO] tsslib.aar not found, will be built fresh
    )
    echo.

    :: Build tsslib.aar
    echo [0/5] Building tsslib.aar...

    :: Get GOPATH for bin directory
    for /f "tokens=*" %%G in ('go env GOPATH') do set "GOPATH_DIR=%%G"
    if not defined GOPATH_DIR set "GOPATH_DIR=%USERPROFILE%\go"
    set "GOBIN_DIR=!GOPATH_DIR!\bin"

    :: Add GOPATH/bin to PATH if not already there
    echo !PATH! | findstr /i /c:"!GOBIN_DIR!" >nul 2>nul
    if !errorlevel! neq 0 (
        set "PATH=!PATH!;!GOBIN_DIR!"
    )

    pushd tsslib
    "!GOBIN_DIR!\gomobile.exe" bind -target=android -androidapi 21 -o "..\app\libs\tsslib.aar" .
    if !errorlevel! neq 0 (
        echo [ERROR] gomobile bind failed!
        popd
        pause
        exit /b 1
    )
    popd

    echo [SUCCESS] tsslib.aar rebuilt!
    for %%F in ("app\libs\tsslib.aar") do echo   Size: %%~zF bytes
    echo.
)

:: Show help
if "%1"=="help" (
    echo Usage: build-install-debug.bat [option]
    echo.
    echo Options:
    echo   rebuild  - Delete and rebuild tsslib.aar before building APK
    echo   help     - Show this help message
    echo.
    echo Examples:
    echo   build-install-debug.bat          - Build and install debug APK
    echo   build-install-debug.bat rebuild  - Rebuild Go code, then build and install
    echo.
    pause
    exit /b 0
)

:: Step 1: Build Debug APK
echo [1/5] Building Debug APK...
call gradlew.bat assembleDebug --no-daemon
if %errorlevel% neq 0 (
    echo [ERROR] Build failed!
    pause
    exit /b 1
)
echo [SUCCESS] Build completed!
echo.

:: Step 2: Check device connection
echo [2/5] Checking device connection...
adb devices
adb devices | find "device" | find /v "List" >nul
if %errorlevel% neq 0 (
    echo [ERROR] No device detected! Please connect your phone and enable USB debugging.
    pause
    exit /b 1
)
echo [SUCCESS] Device connected!
echo.

:: Step 3: Uninstall old version (to avoid signature conflicts)
echo [3/5] Uninstalling old version (if exists)...
adb uninstall com.durian.tssparty 2>nul
echo Done!
echo.

:: Step 4: Install APK
echo [4/5] Installing APK...
adb install app\build\outputs\apk\debug\app-debug.apk
if %errorlevel% neq 0 (
    echo [ERROR] Installation failed!
    pause
    exit /b 1
)
echo [SUCCESS] Installation completed!
echo.

:: Step 5: Launch app
echo [5/5] Launching app...
adb shell am start -n com.durian.tssparty/.MainActivity
if %errorlevel% neq 0 (
    echo [ERROR] Launch failed!
    pause
    exit /b 1
)
echo [SUCCESS] App launched!
echo.

:: Clear old logs
echo Clearing old logs...
adb logcat -c
echo.

:: Show instructions
echo ========================================
echo   App successfully launched!
echo ========================================
echo.
echo Starting log monitoring...
echo.
echo Key log tags:
echo   - MainViewModel    (ViewModel layer)
echo   - TssRepository    (Repository layer)
echo   - GrpcClient       (Network communication)
echo   - TssNativeBridge  (TSS native library)
echo   - AndroidRuntime   (Crash logs)
echo.
echo Press Ctrl+C to stop log monitoring
echo.
timeout /t 2 /nobreak >nul

:: Start monitoring logs
adb logcat -v time MainViewModel:D TssRepository:D GrpcClient:D TssNativeBridge:D AndroidRuntime:E *:S

:: If user stops log monitoring
echo.
echo Log monitoring stopped.
echo.
pause
