@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem ==========================================================
rem  PPTX Image Compressor - Drag&Drop Wrapper (robust + logging, RC-Fix)
rem ==========================================================

set "SELF_DIR=%~dp0"
set "RUNNER=%SELF_DIR%install_and_run.bat"

set "DEFAULT_THREADS=8"
set "DEFAULT_QUALITY=90"
set "PAUSE_ON_ERROR=1"
set "PAUSE_ALWAYS=1"

for /f "delims=" %%A in ('echo prompt $E^| cmd') do set "ESC=%%A"
set "GREEN=%ESC%[92m"
set "YELLOW=%ESC%[93m"
set "RED=%ESC%[91m"
set "RESET=%ESC%[0m"

if not exist "%RUNNER%" (
  echo %RED%[ERROR]%RESET% Runner nicht gefunden: "%RUNNER%"
  pause
  exit /b 2
)

if "%~1"=="" (
  echo Ziehe 1..n ^*.pptx Dateien auf "%~nx0".
  pause
  exit /b 64
)

set "LOGDIR=%SELF_DIR%logs"
if not exist "%LOGDIR%" mkdir "%LOGDIR%" >nul 2>&1
for /f "usebackq delims=" %%t in (`powershell -NoLogo -NoProfile -Command "(Get-Date).ToString('yyyy-MM-dd_HH-mm-ss')"`) do set "TS=%%t"
set "LOGFILE=%LOGDIR%\dragdrop_%TS%.log"

(
  echo ==========================================================
  echo Drag^&Drop Session  %DATE% %TIME%
  echo Runner:             "%RUNNER%"
  echo Wrapper:            "%~nx0"
  echo WorkingDir:         "%CD%"
  echo Defaults:           threads=%DEFAULT_THREADS%, quality=%DEFAULT_QUALITY%
  echo Args:
  for %%# in (%*) do @echo   "%%~f#"
  echo ==========================================================
) >>"%LOGFILE%" 2>&1

set /a TOTAL=0, OK=0, FAIL=0, SKIP=0

:loop
if "%~1"=="" goto done
set "ARG_FULL=%~f1"
set "ARG_EXT=%~x1"
set /a TOTAL+=1

if not exist "%ARG_FULL%" (
  echo %YELLOW%[SKIP]%RESET% Nicht gefunden: "%ARG_FULL%"
  echo [SKIP] Not found: "%ARG_FULL%" >>"%LOGFILE%" 2>&1
  set /a SKIP+=1
  shift & goto loop
)
if exist "%ARG_FULL%\" (
  echo %YELLOW%[SKIP]%RESET% Ist ein Ordner: "%ARG_FULL%"
  echo [SKIP] Is a directory: "%ARG_FULL%" >>"%LOGFILE%" 2>&1
  set /a SKIP+=1
  shift & goto loop
)
if /I not "%ARG_EXT%"==".pptx" (
  echo %YELLOW%[SKIP]%RESET% Keine PPTX: "%ARG_FULL%"
  echo [SKIP] Not a .pptx: "%ARG_FULL%" >>"%LOGFILE%" 2>&1
  set /a SKIP+=1
  shift & goto loop
)

echo.
echo ===== Verarbeite: "%ARG_FULL%" =====
echo ----- Processing "%ARG_FULL%" ----- >>"%LOGFILE%" 2>&1

REM --- Runner aufrufen + vollständige Ausgabe loggen
call "%RUNNER%" -i "%ARG_FULL%" -t %DEFAULT_THREADS% -q %DEFAULT_QUALITY% >>"%LOGFILE%" 2>&1
echo ----- [INFO] Errorlevel = "%ERRORLEVEL%"
set "RC=%ERRORLEVEL%"
echo ----- [INFO] ReturnCode = "%RC%"

REM --- ROBUSTE NUMERISCHE PRÜFUNG STATT STRINGVERGLEICH
REM     (GEQ 1 => Fehler; EQ 0 => OK)

if "%RC%"=="" set "RC=1"
set /a RC+=0
echo ----- [INFO] ReturnCodeAgain = "%RC%"

if %RC% GEQ 1 (
  echo ----- [WARN] Assuming RC GEQ 1
  echo %RED%[FAIL]%RESET% "%ARG_FULL%"  (Code %RC%)
  echo [FAIL] "%ARG_FULL%" Code=%RC% >>"%LOGFILE%" 2>&1
  set /a FAIL+=1
) else (
  echo %GREEN%[OK]%RESET%   "%ARG_FULL%"
  echo [OK] "%ARG_FULL%" >>"%LOGFILE%" 2>&1
  set /a OK+=1
  set /a FAIL=0
)

echo ---- [INFO] Fail-State = "%FAIL%"

shift
goto loop

:done
echo.
echo ------------------ Zusammenfassung ------------------
echo Dateien gesamt:      %TOTAL%
echo Erfolgreich:         %OK%
echo Fehlgeschlagen:      %FAIL%
echo Uebersprungen:       %SKIP%
echo Log-Datei:           "%LOGFILE%"
echo ----------------------------------------------------
echo.>>"%LOGFILE%" & echo Summary: total=%TOTAL% ok=%OK% fail=%FAIL% skip=%SKIP%>>"%LOGFILE%"

if %FAIL% GTR 0 (
  echo %RED%Ergebnis:%RESET% teils fehlgeschlagen. Bitte Log pruefen:
  echo   "%LOGFILE%"
  if "%PAUSE_ON_ERROR%"=="1" (
    echo.
    echo [ENTER] druecken, um das Log in Notepad zu oeffnen...
    pause >nul
    start "" notepad "%LOGFILE%"
    echo [CMD-Fenster bleibt bis zum Schliessen von Notepad geoeffnet.]
    pause
  )
  endlocal & exit /b 1
) else (
  echo %GREEN%Ergebnis:%RESET% alle erfolgreich.
  if "%PAUSE_ALWAYS%"=="1" (
    echo.
    pause
  )
  endlocal & exit /b 0
)
