; LineDance Player — NSIS Installer Script
; Kræver NSIS 3.x installeret: https://nsis.sourceforge.io/

!define APP_NAME      "LineDance Player"
!define APP_EXE       "LineDancePlayer.exe"
!define APP_VERSION   "1.0.0"
!define APP_PUBLISHER "LineDance Player"
!define APP_URL       "https://linedanceplayer.dk"
!define INSTALL_DIR   "$LOCALAPPDATA\LineDancePlayer"
!define REG_KEY       "Software\Microsoft\Windows\CurrentVersion\Uninstall\LineDancePlayer"

; ── Generelle indstillinger ───────────────────────────────────────────────────
Name "${APP_NAME}"
OutFile "dist\LineDancePlayer-Setup.exe"
InstallDir "${INSTALL_DIR}"
InstallDirRegKey HKCU "${REG_KEY}" "InstallLocation"
RequestExecutionLevel user   ; Ingen admin-rettigheder nødvendige
SetCompressor /SOLID lzma
Unicode True

; ── Moderne UI ────────────────────────────────────────────────────────────────
!include "MUI2.nsh"
!include "FileFunc.nsh"

!define MUI_ABORTWARNING
!define MUI_ICON "installer\icon.ico"
!define MUI_UNICON "installer\icon.ico"
!define MUI_WELCOMEFINISHPAGE_BITMAP "installer\welcome.bmp"
!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "installer\header.bmp"

; Farver (orange tema)
!define MUI_BGCOLOR "0C0D10"
!define MUI_TEXTCOLOR "ECEEF4"

; ── Sider ─────────────────────────────────────────────────────────────────────
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

!insertmacro MUI_LANGUAGE "Danish"
!insertmacro MUI_LANGUAGE "English"

; ── Installer ─────────────────────────────────────────────────────────────────
Section "LineDance Player" SecMain
  SectionIn RO  ; Obligatorisk

  SetOutPath "$INSTDIR"
  File "dist\LineDancePlayer\LineDancePlayer.exe"

  SetOutPath "$INSTDIR\_internal"
  File /r "dist\LineDancePlayer\_internal\*"

  ; Start-menu genvej
  CreateDirectory "$SMPROGRAMS\${APP_NAME}"
  CreateShortcut "$SMPROGRAMS\${APP_NAME}\${APP_NAME}.lnk" \
    "$INSTDIR\${APP_EXE}" "" "$INSTDIR\${APP_EXE}" 0

  ; Skrivebords-genvej
  CreateShortcut "$DESKTOP\${APP_NAME}.lnk" \
    "$INSTDIR\${APP_EXE}" "" "$INSTDIR\${APP_EXE}" 0

  ; Gem installationsinfo i registry (til afinstallation)
  WriteRegStr HKCU "${REG_KEY}" "DisplayName"      "${APP_NAME}"
  WriteRegStr HKCU "${REG_KEY}" "DisplayVersion"   "${APP_VERSION}"
  WriteRegStr HKCU "${REG_KEY}" "Publisher"        "${APP_PUBLISHER}"
  WriteRegStr HKCU "${REG_KEY}" "URLInfoAbout"     "${APP_URL}"
  WriteRegStr HKCU "${REG_KEY}" "InstallLocation"  "$INSTDIR"
  WriteRegStr HKCU "${REG_KEY}" "UninstallString"  '"$INSTDIR\Uninstall.exe"'
  WriteRegStr HKCU "${REG_KEY}" "DisplayIcon"      "$INSTDIR\${APP_EXE}"
  WriteRegDWORD HKCU "${REG_KEY}" "NoModify"       1
  WriteRegDWORD HKCU "${REG_KEY}" "NoRepair"       1

  ; Beregn installationsstørrelse
  ${GetSize} "$INSTDIR" "/S=0K" $0 $1 $2
  IntFmt $0 "0x%08X" $0
  WriteRegDWORD HKCU "${REG_KEY}" "EstimatedSize" "$0"

  ; Skriv afinstaller
  WriteUninstaller "$INSTDIR\Uninstall.exe"

SectionEnd

; ── VLC tjek ──────────────────────────────────────────────────────────────────
Section -VLCCheck
  ; Tjek alle kendte VLC registry-stier
  ReadRegStr $0 HKLM "SOFTWARE\VideoLAN\VLC" ""
  ReadRegStr $1 HKCU "SOFTWARE\VideoLAN\VLC" ""
  ReadRegStr $2 HKLM "SOFTWARE\WOW6432Node\VideoLAN\VLC" ""
  ; Tjek også om vlc.exe eksisterer
  IfFileExists "$PROGRAMFILES\VideoLAN\VLC\vlc.exe" VLCFound 0
  IfFileExists "$PROGRAMFILES64\VideoLAN\VLC\vlc.exe" VLCFound 0
  ${If} $0 != ""
    Goto VLCFound
  ${EndIf}
  ${If} $1 != ""
    Goto VLCFound
  ${EndIf}
  ${If} $2 != ""
    Goto VLCFound
  ${EndIf}
  ; VLC ikke fundet
  MessageBox MB_YESNO|MB_ICONINFORMATION \
    "LineDance Player bruger VLC til afspilning.$\n$\nVLC ser ikke ud til at vaere installeret.$\n$\nVil du aabne download-siden nu?$\n$\n(Du kan installere VLC senere)" \
    IDNO VLCSkip
  ExecShell "open" "https://www.videolan.org/vlc/"
  VLCSkip:
  Goto VLCDone
  VLCFound:
  VLCDone:
SectionEnd

; ── Afinstaller ───────────────────────────────────────────────────────────────
Section "Uninstall"
  ; Slet programfiler
  RMDir /r "$INSTDIR"

  ; Slet genveje
  Delete "$SMPROGRAMS\${APP_NAME}\${APP_NAME}.lnk"
  RMDir "$SMPROGRAMS\${APP_NAME}"
  Delete "$DESKTOP\${APP_NAME}.lnk"

  ; Slet registry
  DeleteRegKey HKCU "${REG_KEY}"

  ; Spørg om brugerdata skal slettes
  IfFileExists "$PROFILE\.linedance\*" AskDelete SkipDelete
  AskDelete:
    MessageBox MB_YESNO|MB_ICONQUESTION \
      "Vil du slette dine lokale data?$\n$\nDette fjerner din musikdatabase, indstillinger og playlister fra:$\n$PROFILE\.linedance$\n$\nVaelg Nej for at beholde dine data." \
      IDNO SkipDelete
    RMDir /r "$PROFILE\.linedance"
  SkipDelete:

SectionEnd
