@ECHO OFF
REM ===============================================================================
REM GRIFO_M_PBIT - Simulation Mode Launcher
REM ===============================================================================
REM 
REM Purpose: Execute GRIFO_M_PBIT test in simulation mode without hardware
REM 
REM This script:
REM   1. Configures the local Python environment (embedded Python 3.7)
REM   2. Sets up necessary paths for test execution
REM   3. Launches GRIFO_M_PBIT.py with --simulate flag
REM 
REM Usage:
REM   run_simulate.bat
REM 
REM Note: This script uses the embedded Python environment in PlatformSimulator/bin
REM       and does NOT require physical 1553 hardware or radar connection.
REM 
REM ===============================================================================

SETLOCAL EnableDelayedExpansion

REM Get script directory
SET SCRIPT_DIR=%~dp0
SET ROOT_DIR=%SCRIPT_DIR%

ECHO ===============================================================================
ECHO GRIFO M-PBIT Test - Simulation Mode
ECHO ===============================================================================
ECHO.

REM ===============================================================================
REM STEP 1: Locate Python Environment
REM ===============================================================================

REM Check for embedded Python in PlatformSimulator/bin
SET PYTHON_HOME=%ROOT_DIR%PlatformSimulator\bin
SET PYTHON_DLL=%PYTHON_HOME%\python37.dll
SET PYTHON_ZIP=%PYTHON_HOME%\Python37.zip

IF NOT EXIST "%PYTHON_DLL%" (
    ECHO [ERROR] Python DLL not found: %PYTHON_DLL%
    ECHO [ERROR] The embedded Python environment is missing.
    GOTO ERROR_EXIT
)

ECHO [OK] Found Python environment: %PYTHON_HOME%
ECHO.

REM ===============================================================================
REM STEP 2: Configure Python Path and Environment
REM ===============================================================================

REM Set Python paths for embedded environment
SET PYTHONHOME=%PYTHON_HOME%
SET PYTHONPATH=%PYTHON_HOME%;%PYTHON_HOME%\DLLs;%PYTHON_ZIP%

REM Add test environment paths
SET TEST_ENV=%ROOT_DIR%TestEnvironment
SET TEST_SCRIPTS=%TEST_ENV%\scripts
SET TEST_LIB=%TEST_ENV%\env
SET TEST_SITE_PACKAGES=%TEST_LIB%\site-packages

REM Combine all Python paths
SET PYTHONPATH=%PYTHONPATH%;%TEST_SCRIPTS%;%TEST_LIB%;%TEST_SITE_PACKAGES%

ECHO [OK] Python environment configured
ECHO      PYTHONHOME=%PYTHONHOME%
ECHO      Scripts: %TEST_SCRIPTS%
ECHO      Libs: %TEST_LIB%
ECHO.

REM ===============================================================================
REM STEP 3: Locate Test Script
REM ===============================================================================

SET TEST_SCRIPT=%TEST_SCRIPTS%\GRIFO_M_PBIT.py

IF NOT EXIST "%TEST_SCRIPT%" (
    ECHO [ERROR] Test script not found: %TEST_SCRIPT%
    GOTO ERROR_EXIT
)

ECHO [OK] Found test script: %TEST_SCRIPT%
ECHO.

REM ===============================================================================
REM STEP 4: Check Mock Module
REM ===============================================================================

SET MOCK_MODULE=%TEST_SCRIPTS%\GRIFO_M_PBIT_mock.py

IF NOT EXIST "%MOCK_MODULE%" (
    ECHO [WARNING] Mock module not found: %MOCK_MODULE%
    ECHO [WARNING] Simulation mode may not work correctly.
    ECHO.
)

REM ===============================================================================
REM STEP 5: Prepare Execution Environment
REM ===============================================================================

REM Change to test scripts directory (for relative imports)
PUSHD "%TEST_SCRIPTS%"

ECHO ===============================================================================
ECHO Starting GRIFO M-PBIT Test in SIMULATION MODE
ECHO ===============================================================================
ECHO.
ECHO Test Configuration:
ECHO   - Mode: SIMULATION (no hardware required)
ECHO   - Script: GRIFO_M_PBIT.py
ECHO   - Repetitions: 10 (configurable in script)
ECHO   - BIT Timeout: 180 seconds
ECHO.
ECHO Simulation Features:
ECHO   - Simulated 1553 bus messages
ECHO   - Simulated serial terminal
ECHO   - Simulated power control
ECHO   - Configurable scenarios and timing
ECHO.
ECHO Press Ctrl+C to interrupt test execution
ECHO.
ECHO ===============================================================================
ECHO.

REM Small delay to allow user to read
TIMEOUT /T 2 /NOBREAK >NUL

REM ===============================================================================
REM STEP 6: Execute Test with Embedded Python via uPlatSim
REM ===============================================================================

REM Use uPlatSim.exe with embedded Python interpreter (same as production)
REM We use a launcher script that injects the --simulate flag

SET UPLATSIM=%PYTHON_HOME%\uPlatSim.exe
SET LAUNCHER_SCRIPT=%TEST_SCRIPTS%\GRIFO_M_PBIT_simulate_launcher.py
SET SIMULATION_INI=%ROOT_DIR%PlatformSimulator\LeonardoCompany\x86_64-w64-mingw32\grifo_simulation.ini

IF NOT EXIST "%UPLATSIM%" (
    ECHO [ERROR] uPlatSim.exe not found: %UPLATSIM%
    ECHO [ERROR] Cannot execute test without embedded Python interpreter.
    GOTO ERROR_EXIT
)

IF NOT EXIST "%LAUNCHER_SCRIPT%" (
    ECHO [ERROR] Launcher script not found: %LAUNCHER_SCRIPT%
    ECHO [ERROR] Cannot execute test in simulation mode.
    GOTO ERROR_EXIT
)

IF NOT EXIST "%SIMULATION_INI%" (
    ECHO [ERROR] Simulation INI not found: %SIMULATION_INI%
    ECHO [ERROR] Cannot configure uPlatSim for simulation mode.
    GOTO ERROR_EXIT
)

ECHO [INFO] Using uPlatSim.exe with embedded Python interpreter
ECHO [INFO] Configuration: grifo_simulation.ini (no hardware interfaces)
ECHO [INFO] Launcher: GRIFO_M_PBIT_simulate_launcher.py
ECHO.

REM Execute with uPlatSim in batch mode using the launcher script
"%UPLATSIM%" --ini "%SIMULATION_INI%" --batch "%LAUNCHER_SCRIPT%"

SET EXIT_CODE=!ERRORLEVEL!

REM Return to original directory
POPD

REM ===============================================================================
REM STEP 7: Report Results
REM ===============================================================================

ECHO.
ECHO ===============================================================================
ECHO Test Execution Completed
ECHO ===============================================================================
ECHO.

IF !EXIT_CODE! EQU 0 (
    ECHO [OK] Test completed successfully
    COLOR 2F
    ECHO.
    ECHO Results available in:
    ECHO   - Log: %TEST_ENV%\LOG\GRIFO_M_PBIT.log
    ECHO   - PDF: %TEST_ENV%\pdf_reports\GRIFO_M_PBIT_*.pdf
    ECHO.
) ELSE (
    ECHO [ERROR] Test failed with exit code: !EXIT_CODE!
    COLOR 4F
    ECHO.
    ECHO Check log file for details:
    ECHO   %TEST_ENV%\LOG\GRIFO_M_PBIT.log
    ECHO.
)

ECHO ===============================================================================
ECHO.

REM Pause to allow user to read results
ECHO Press any key to exit...
PAUSE >NUL

COLOR
ENDLOCAL
EXIT /B !EXIT_CODE!

REM ===============================================================================
REM Error Handler
REM ===============================================================================

:ERROR_EXIT
ECHO.
ECHO ===============================================================================
ECHO [ERROR] Test execution failed - see messages above
ECHO ===============================================================================
ECHO.
COLOR 4F
ECHO Press any key to exit...
PAUSE >NUL
COLOR
ENDLOCAL
EXIT /B 1
