@ECHO OFF
REM ===============================================================================
REM GRIFO_M_PBIT - Production Mode Launcher (Target Reale)
REM ===============================================================================
REM 
REM Purpose: Execute GRIFO_M_PBIT test on real hardware target
REM 
REM This script:
REM   1. Configures the local Python environment (embedded Python 3.7)
REM   2. Sets up necessary paths for test execution with 1553 hardware
REM   3. Launches GRIFO_M_PBIT.py WITHOUT --simulate flag (production mode)
REM 
REM Usage:
REM   run_target.bat
REM 
REM Prerequisites:
REM   - Physical 1553 hardware connected and powered
REM   - Radar system available for testing
REM   - Serial terminal connection configured
REM   - Power control interface available
REM 
REM Note: This script uses the embedded Python environment in PlatformSimulator/bin
REM       and REQUIRES physical 1553 hardware and 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 - Production Mode (Target Reale)
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: Hardware Connection Check
REM ===============================================================================

ECHO ===============================================================================
ECHO HARDWARE CONNECTION CHECK
ECHO ===============================================================================
ECHO.
ECHO Before proceeding, ensure:
ECHO   [X] 1553 hardware interface connected and powered
ECHO   [X] Radar system powered and ready
ECHO   [X] Serial terminal connection configured
ECHO   [X] Power control interface accessible
ECHO.
ECHO WARNING: This test will interact with real hardware!
ECHO          The radar will be power-cycled multiple times.
ECHO.

REM Ask for user confirmation
SET /P CONFIRM="Continue with hardware test? (Y/N): "
IF /I NOT "%CONFIRM%"=="Y" (
    ECHO.
    ECHO [INFO] Test execution cancelled by user.
    ECHO.
    GOTO CLEAN_EXIT
)

ECHO.
ECHO [OK] User confirmed - proceeding with hardware test
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 PRODUCTION MODE
ECHO ===============================================================================
ECHO.
ECHO Test Configuration:
ECHO   - Mode: PRODUCTION (real hardware required)
ECHO   - Script: GRIFO_M_PBIT.py
ECHO   - Repetitions: 10 (configurable in script)
ECHO   - BIT Timeout: 180 seconds
ECHO.
ECHO Production Features:
ECHO   - Real 1553 bus communication
ECHO   - Real serial terminal monitoring
ECHO   - Real power control (radar will power cycle)
ECHO   - Real BIT execution and verification
ECHO.
ECHO Press Ctrl+C to interrupt test execution (graceful shutdown)
ECHO.
ECHO ===============================================================================
ECHO.

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

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

REM Use uPlatSim.exe with embedded Python interpreter
REM Use grifo_bus_controller.ini for real hardware interfaces

SET UPLATSIM=%PYTHON_HOME%\uPlatSim.exe
SET PRODUCTION_INI=%ROOT_DIR%PlatformSimulator\LeonardoCompany\x86_64-w64-mingw32\grifo_bus_controller.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 "%PRODUCTION_INI%" (
    ECHO [ERROR] Production INI not found: %PRODUCTION_INI%
    ECHO [ERROR] Cannot configure uPlatSim for production mode.
    GOTO ERROR_EXIT
)

ECHO [INFO] Using uPlatSim.exe with embedded Python interpreter
ECHO [INFO] Configuration: grifo_bus_controller.ini (hardware interfaces enabled)
ECHO [INFO] Script: GRIFO_M_PBIT.py (NO --simulate flag)
ECHO.
ECHO [INFO] Test execution starting...
ECHO.

REM Execute with uPlatSim in batch mode (direct script, no --simulate)
"%UPLATSIM%" --ini "%PRODUCTION_INI%" --batch "%TEST_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\GRIFO_M_PBIT_*_report.pdf
    ECHO   - CSV: %TEST_ENV%\pdf_reports\GRIFO_M_PBIT\GRIFO_M_PBIT_*_statistic.csv
    ECHO.
    ECHO Check PDF report for detailed test results and statistics.
    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 Possible causes:
    ECHO   - Hardware connection failure
    ECHO   - 1553 bus communication timeout
    ECHO   - Radar BIT execution timeout
    ECHO   - Serial terminal connection lost
    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 Clean Exit (User Cancelled)
REM ===============================================================================

:CLEAN_EXIT
ECHO Press any key to exit...
PAUSE >NUL
ENDLOCAL
EXIT /B 0

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
