@echo off
chcp 65001 >nul
title Android Remote Controller - Development

echo Checking environment and installing dependencies...
powershell -ExecutionPolicy Bypass -File enviroment-check.ps1

if %ERRORLEVEL% EQU 0 (
    echo.
    echo Starting project...
    echo Starting Vite dev server...
    start /B npm run dev
    echo Waiting for Vite server to be ready...
    timeout /t 3 /nobreak >nul
    :wait_loop
    powershell -Command "try { $response = Invoke-WebRequest -Uri 'http://localhost:9527' -TimeoutSec 1 -UseBasicParsing; exit 0 } catch { exit 1 }" >nul 2>&1
    if %ERRORLEVEL% NEQ 0 (
        timeout /t 1 /nobreak >nul
        goto wait_loop
    )
    echo Vite server is ready!
    echo Starting Electron...
    npx electron .
) else (
    echo.
    echo Environment check failed. Please fix the errors above.
    pause
    exit /b 1
)
