@echo off
setlocal EnableExtensions EnableDelayedExpansion

set "SCRIPT_DIR=%~dp0"
set "GO_DIR=%SCRIPT_DIR%go"
set "ANDROID_DIR=%SCRIPT_DIR%android"
set "LIBS_DIR=%ANDROID_DIR%\app\libs"
set "ANDROID_API=26"

echo === TeamSpeak Mobile Build Script ===

REM 1. Check dependencies
echo.
echo [1/4] Checking dependencies...

where go >nul 2>&1
if %errorlevel% neq 0 (
    echo ERROR: Go not installed. Download from https://go.dev/dl/
    exit /b 1
)
for /f "tokens=*" %%i in ('go version') do echo   Go: %%i

where gomobile >nul 2>&1
if %errorlevel% neq 0 (
    echo   gomobile not found, installing...
    go install golang.org/x/mobile/cmd/gomobile@latest
    gomobile init
)
echo   gomobile: OK

set "SDK_ROOT=%ANDROID_HOME%"
if not defined SDK_ROOT set "SDK_ROOT=%ANDROID_SDK_ROOT%"
if not defined SDK_ROOT if exist "%ANDROID_DIR%\local.properties" (
    for /f "tokens=1,* delims==" %%A in ('findstr /B "sdk.dir=" "%ANDROID_DIR%\local.properties"') do set "SDK_ROOT=%%B"
)
if not defined SDK_ROOT (
    echo ERROR: Android SDK not found. Set ANDROID_HOME or ANDROID_SDK_ROOT.
    exit /b 1
)
set "SDK_ROOT=%SDK_ROOT:\=/%"
echo   Android SDK: %SDK_ROOT%

set "NDK_ROOT=%ANDROID_NDK_HOME%"
if not defined NDK_ROOT set "NDK_ROOT=%ANDROID_NDK_ROOT%"
if not defined NDK_ROOT if exist "%SDK_ROOT%/ndk" (
    for /f "delims=" %%A in ('dir /B /AD /O-N "%SDK_ROOT%/ndk"') do if not defined NDK_ROOT set "NDK_ROOT=%SDK_ROOT%/ndk/%%A"
)
if not exist "%NDK_ROOT%/build/cmake/android.toolchain.cmake" (
    echo ERROR: Android NDK not found. Set ANDROID_NDK_HOME or install an NDK.
    exit /b 1
)
set "NDK_ROOT=%NDK_ROOT:\=/%"
echo   Android NDK: %NDK_ROOT%

REM 2. Download Go dependencies
echo.
echo [2/4] Downloading Go dependencies...
cd /d "%GO_DIR%"
go mod tidy
if errorlevel 1 exit /b 1

REM 3. Build Go -> AAR
echo.
echo [3/4] Building Android libopus and Go bridge -> AAR...
call "%GO_DIR%\build-opus-android.bat"
if errorlevel 1 exit /b 1
if not exist "%LIBS_DIR%" mkdir "%LIBS_DIR%"
set "JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8"
REM gomobile must use the Android NDK C compiler so android && cgo files are selected.
set "CGO_ENABLED=1"
set "GOOS=android"
set "GO111MODULE=on"
set "CC=%NDK_ROOT%/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"
set "CXX=%NDK_ROOT%/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe"
echo   CGO_ENABLED=%CGO_ENABLED%
echo   GOOS=%GOOS%
echo   CC=%CC%
cd /d "%GO_DIR%"
gomobile bind -target=android -androidapi=%ANDROID_API% -ldflags="-linkmode=external -extldflags=-Wl,--hash-style=both" -o "%LIBS_DIR%\teamspeak.aar" ./teamspeak
if errorlevel 1 (
    echo ERROR: gomobile bind failed.
    exit /b 1
)
echo   Output: %LIBS_DIR%\teamspeak.aar

REM 4. Build Android app
echo.
echo [4/4] Building Android app...
if exist "%ANDROID_DIR%\gradlew.bat" (
    call "%ANDROID_DIR%\gradlew.bat" -p "%ANDROID_DIR%" assembleDebug
    if !errorlevel! neq 0 (
        echo ERROR: Android build failed.
        exit /b !errorlevel!
    )
) else (
    echo ERROR: gradlew.bat not found in %ANDROID_DIR%
    exit /b 1
)

echo.
echo === Build Complete ===
echo APK: %ANDROID_DIR%\app\build\outputs\apk\debug\app-debug.apk

set "GOOS="
set "CC="
set "CXX="
endlocal
