@echo off
echo ====================================
echo ADB Device Connection Script
echo ====================================
echo.

REM Set ADB path
set ADB_PATH=D:\__sistem__\ProgramFiles\AndroidSdk\platform-tools\adb.exe

echo [1/5] Checking ADB installation...
if not exist "%ADB_PATH%" (
    echo ERROR: ADB not found at %ADB_PATH%
    pause
    exit /b 1
)
echo ADB found!
echo.

echo [2/5] Killing existing ADB server...
"%ADB_PATH%" kill-server
timeout /t 2 /nobreak >nul
echo.

echo [3/5] Starting ADB server...
"%ADB_PATH%" start-server
timeout /t 2 /nobreak >nul
echo.

echo [4/5] Listing connected devices...
"%ADB_PATH%" devices -l
echo.

echo [5/5] Checking device status...
for /f "tokens=1,2" %%a in ('"%ADB_PATH%" devices ^| findstr /v "List"') do (
    if "%%b"=="device" (
        echo.
        echo SUCCESS: Device %%a is connected and ready!
        echo.
        echo Device Info:
        "%ADB_PATH%" -s %%a shell getprop ro.product.model
        "%ADB_PATH%" -s %%a shell getprop ro.build.version.release
        goto :found
    )
)

echo.
echo WARNING: No devices found!
echo.
echo Troubleshooting:
echo 1. Connect your Android device via USB
echo 2. Enable "USB Debugging" in Developer Options
echo 3. Authorize the computer on your device
echo 4. Try different USB cable/port
echo.
echo For WSL subsystem:
echo - Run: adb connect 127.0.0.1:58526
echo - Or: adb connect localhost:58526
echo.

:found
echo.
echo To connect to Flutter:
echo   flutter devices
echo   flutter run -d [device-id]
echo.
pause
