@echo off
REM Atomizer WFE Analyzer - Double-click to analyze OP2 file
REM Opens file dialog to select OP2, then runs Zernike analysis
REM Generates 3 HTML files: 40 vs 20, 60 vs 20, 90 Manufacturing

echo ======================================================================
echo   ATOMIZER WFE ANALYZER
echo   Generates Zernike HTML Reports (40 vs 20, 60 vs 20, 90 Mfg)
echo ======================================================================
echo.

REM Use PowerShell to open file dialog
for /f "delims=" %%I in ('powershell -NoProfile -Command "Add-Type -AssemblyName System.Windows.Forms; $f = New-Object System.Windows.Forms.OpenFileDialog; $f.Filter = 'OP2 Files (*.op2)|*.op2|All Files (*.*)|*.*'; $f.Title = 'Select OP2 Results File'; $f.InitialDirectory = '%USERPROFILE%'; if ($f.ShowDialog() -eq 'OK') { $f.FileName }"') do set "OP2_FILE=%%I"

if "%OP2_FILE%"=="" (
    echo No file selected. Exiting.
    pause
    exit /b 1
)

echo Selected: %OP2_FILE%
echo.

REM Get the directory of the OP2 file for finding output HTMLs
for %%F in ("%OP2_FILE%") do set "OP2_DIR=%%~dpF"
for %%F in ("%OP2_FILE%") do set "OP2_BASE=%%~nF"

REM Initialize conda and activate atomizer environment
call "%USERPROFILE%\anaconda3\Scripts\activate.bat" atomizer

echo Running Zernike analysis...
echo.

REM Run the HTML generator
python "%~dp0zernike_html_generator.py" "%OP2_FILE%"

if %ERRORLEVEL% neq 0 (
    echo.
    echo ======================================================================
    echo   ERROR: Analysis failed. See errors above.
    echo ======================================================================
    pause
    exit /b 1
)

echo.
echo ======================================================================
echo   Opening generated HTML files in browser...
echo ======================================================================

REM Find and open the most recent HTML files (with timestamp pattern)
REM They follow pattern: {basename}_{timestamp}_*.html

REM Open all matching HTML files
for /f "delims=" %%H in ('powershell -NoProfile -Command "Get-ChildItem -Path '%OP2_DIR%' -Filter '%OP2_BASE%_*_40_vs_20.html' | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName"') do (
    echo Opening: %%~nxH
    start "" "%%H"
)

for /f "delims=" %%H in ('powershell -NoProfile -Command "Get-ChildItem -Path '%OP2_DIR%' -Filter '%OP2_BASE%_*_60_vs_20.html' | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName"') do (
    echo Opening: %%~nxH
    start "" "%%H"
)

for /f "delims=" %%H in ('powershell -NoProfile -Command "Get-ChildItem -Path '%OP2_DIR%' -Filter '%OP2_BASE%_*_90_mfg.html' | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty FullName"') do (
    echo Opening: %%~nxH
    start "" "%%H"
)

echo.
echo ======================================================================
echo   ANALYSIS COMPLETE
echo ======================================================================
echo.
pause
