@echo off
setlocal enabledelayedexpansion
echo.
echo ================================================
echo   LineDance Player - Windows Build + Installer
echo ================================================
echo.

:: ── Aktiver venv ──────────────────────────────────────────────────────────────
if exist "venv\Scripts\activate.bat" (
    call venv\Scripts\activate.bat
) else (
    echo ADVARSEL: venv ikke fundet - bruger system Python
)

:: ── Tjek Python ───────────────────────────────────────────────────────────────
python --version >nul 2>&1
if errorlevel 1 (
    echo FEJL: Python ikke fundet
    pause & exit /b 1
)

:: ── Installer/opdater PyInstaller ─────────────────────────────────────────────
echo [1/4] Installerer PyInstaller...
pip install pyinstaller --quiet

:: ── Ryd gamle builds ──────────────────────────────────────────────────────────
echo [2/4] Rydder gamle builds...
if exist "dist\LineDancePlayer"    rmdir /s /q "dist\LineDancePlayer"
if exist "build\LineDancePlayer"   rmdir /s /q "build\LineDancePlayer"

:: ── PyInstaller build ─────────────────────────────────────────────────────────
echo [3/4] Bygger med PyInstaller (2-5 minutter)...
echo.
pyinstaller build_windows.spec --clean --noconfirm

if errorlevel 1 (
    echo.
    echo FEJL: PyInstaller fejlede - se fejlbesked ovenfor
    pause & exit /b 1
)

echo.
echo OK: dist\LineDancePlayer\ er klar
echo.

:: ── Kopiér VLC DLL-filer ind i app-mappen ─────────────────────────────────────
echo Kopierer VLC DLL-filer...
set "VLC_PATH="
if exist "C:\Program Files\VideoLAN\VLC\libvlc.dll" set "VLC_PATH=C:\Program Files\VideoLAN\VLC"
if exist "C:\Program Files (x86)\VideoLAN\VLC\libvlc.dll" set "VLC_PATH=C:\Program Files (x86)\VideoLAN\VLC"

if defined VLC_PATH (
    copy /Y "!VLC_PATH!\libvlc.dll"     "dist\LineDancePlayer\libvlc.dll"     >nul
    copy /Y "!VLC_PATH!\libvlccore.dll" "dist\LineDancePlayer\libvlccore.dll" >nul
    :: Kopiér også plugins-mappen som VLC kræver
    if exist "!VLC_PATH!\plugins" (
        xcopy /E /I /Y /Q "!VLC_PATH!\plugins" "dist\LineDancePlayer\plugins" >nul
    )
    echo OK: VLC DLL-filer kopieret fra !VLC_PATH!
) else (
    echo ADVARSEL: VLC ikke fundet - brugere skal have VLC installeret selv
)
echo.

:: ── NSIS installer ────────────────────────────────────────────────────────────
echo [4/4] Bygger NSIS installer...
echo.

:: Find NSIS
set "NSIS="
if exist "C:\Program Files (x86)\NSIS\makensis.exe" set "NSIS=C:\Program Files (x86)\NSIS\makensis.exe"
if exist "C:\Program Files\NSIS\makensis.exe"       set "NSIS=C:\Program Files\NSIS\makensis.exe"

if not defined NSIS (
    echo ADVARSEL: NSIS ikke fundet.
    echo.
    echo Download NSIS fra https://nsis.sourceforge.io/Download
    echo og koer derefter: makensis installer.nsi
    echo.
    echo PyInstaller-buildet ligger klar i dist\LineDancePlayer\
    goto :done
)

:: Tjek installer-mappe og billeder
if not exist "installer" mkdir installer

:: Generer et simpelt .ico hvis det mangler (kræver PowerShell)
if not exist "installer\icon.ico" (
    echo Genererer standard ikon...
    powershell -NoProfile -Command ^
        "$bmp = New-Object System.Drawing.Bitmap(64,64); " ^
        "$g = [System.Drawing.Graphics]::FromImage($bmp); " ^
        "$g.Clear([System.Drawing.Color]::FromArgb(14,15,17)); " ^
        "$b = New-Object System.Drawing.SolidBrush([System.Drawing.Color]::FromArgb(232,160,32)); " ^
        "$g.FillEllipse($b, 8, 8, 48, 48); " ^
        "$bmp.Save('installer\icon.png'); " ^
        "$g.Dispose(); $bmp.Dispose()" 2>nul
    echo   (Sæt et rigtigt icon.ico i installer\ for bedre resultat)
)

:: Byg NSIS — uden ikon-linjer hvis .ico mangler
if exist "installer\icon.ico" (
    "%NSIS%" /V2 installer.nsi
) else (
    :: Lav midlertidig .nsi uden ikon
    powershell -NoProfile -Command ^
        "(Get-Content installer.nsi) | " ^
        "Where-Object { $_ -notmatch 'MUI_ICON|MUI_UNICON|MUI_HEADERIMAGE' } | " ^
        "Set-Content installer_tmp.nsi"
    "%NSIS%" /V2 installer_tmp.nsi
    del installer_tmp.nsi
)

if errorlevel 1 (
    echo.
    echo FEJL: NSIS fejlede - se fejlbesked ovenfor
    pause & exit /b 1
)

:done
echo.
echo ================================================
echo   FAERDIG!
echo ================================================
if exist "dist\LineDancePlayer-Setup.exe" (
    echo.
    echo   Installer: dist\LineDancePlayer-Setup.exe
    for %%A in ("dist\LineDancePlayer-Setup.exe") do (
        set /a SIZEMB=%%~zA / 1048576
        echo   Stoerrelse: !SIZEMB! MB
    )
    echo.
    echo   Upload til server:
    echo   scp dist\LineDancePlayer-Setup.exe bruger@linedanceplayer.dk:/opt/docker/linedanceafspiller/linedance-api/web/public/download/
) else (
    echo.
    echo   PyInstaller-build: dist\LineDancePlayer\
    echo   Kør makensis installer.nsi manuelt naar NSIS er installeret
)
echo.
pause