@echo off
REM ======================================
REM CAN BIN to CSV Batch Converter (Advanced)
REM Multiple format options
REM ======================================

REM Find DBC file
set DBC_FILE=
for %%f in (*.dbc) do (
    set DBC_FILE=%%f
    goto found_dbc
)

:found_dbc
if "%DBC_FILE%"=="" (
    echo [ERROR] DBC file not found!
    pause
    exit /b 1
)

echo ======================================
echo CAN BIN to CSV Converter (Advanced)
echo ======================================
echo.
echo DBC File: %DBC_FILE%
echo.

REM Count BIN files
set COUNT=0
for %%f in (*.bin) do set /a COUNT+=1

if %COUNT%==0 (
    echo [ERROR] No BIN files to convert!
    pause
    exit /b 1
)

echo Total files: %COUNT%
echo.

REM Select CSV format
echo Select CSV format:
echo ======================================
echo [1] Wide format (Excel-friendly) - RECOMMENDED
echo     - One row per timestamp
echo     - All signals as columns
echo     - Easy to use in Excel
echo     - Output: filename_wide.csv
echo.
echo [2] Long format (Database-friendly)
echo     - One row per signal value
echo     - Columns: Time, Signal, Value, Unit
echo     - Good for databases
echo     - Output: filename_long.csv
echo.
echo [3] Message format (Separate files per message)
echo     - One CSV file per CAN message
echo     - Organized by message
echo     - Output: csv_files/MessageName_*.csv
echo.
echo [4] Raw format (No decoding)
echo     - Raw CAN frames
echo     - No signal decoding
echo     - Output: filename_raw.csv
echo ======================================
echo.

set /p FORMAT="Select (1/2/3/4) [default=1]: "
if "%FORMAT%"=="" set FORMAT=1

if "%FORMAT%"=="1" (
    set FORMAT_NAME=wide
    set OUTPUT_DIR=./
    echo Selected: Wide format - Excel-friendly
) else if "%FORMAT%"=="2" (
    set FORMAT_NAME=long
    set OUTPUT_DIR=./
    echo Selected: Long format - Database-friendly
) else if "%FORMAT%"=="3" (
    set FORMAT_NAME=message
    set OUTPUT_DIR=csv_files/
    echo Selected: Message format - Separate files
    if not exist csv_files mkdir csv_files
) else if "%FORMAT%"=="4" (
    set FORMAT_NAME=raw
    set OUTPUT_DIR=./
    echo Selected: Raw format - No decoding
) else (
    echo [ERROR] Invalid selection. Please select 1, 2, 3, or 4.
    pause
    exit /b 1
)

echo.
echo Starting conversion...
echo ======================================
echo.

REM Record start time
set START_TIME=%TIME%

REM Convert each BIN file
setlocal enabledelayedexpansion
set PROCESSED=0
set SUCCESS=0
set FAILED=0

for %%f in (*.bin) do (
    set /a PROCESSED+=1
    echo [!PROCESSED!/%COUNT%] Converting: %%f
    
    python can_bin_to_csv.py -d "%DBC_FILE%" -i "%%f" -o "%OUTPUT_DIR%" -f %FORMAT_NAME%
    
    if errorlevel 1 (
        echo   [FAIL] %%f
        set /a FAILED+=1
    ) else (
        echo   [OK] %%f
        set /a SUCCESS+=1
    )
    echo.
)

REM Record end time
set END_TIME=%TIME%

echo ======================================
echo Conversion Complete!
echo ======================================
echo Total:    %COUNT%
echo Success:  %SUCCESS%
echo Failed:   %FAILED%
echo Start:    %START_TIME%
echo End:      %END_TIME%
echo ======================================
echo.

REM List generated CSV files
if "%FORMAT%"=="3" (
    echo Generated CSV files in csv_files folder:
    dir /b csv_files\*.csv 2>nul | find /c /v "" > temp_count.txt
    set /p CSV_COUNT=<temp_count.txt
    del temp_count.txt
    echo   Total CSV files: !CSV_COUNT!
    echo   Location: csv_files\
) else (
    echo Generated CSV files:
    for %%f in (*_%FORMAT_NAME%.csv) do (
        set SIZE=%%~zf
        set /a SIZE_KB=!SIZE! / 1024
        echo   - %%f (!SIZE_KB! KB^)
    )
)
echo.

pause
