@echo off
setlocal enabledelayedexpansion

:: Run as administrator check
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo This script requires administrator privileges.
    echo Right-click the file and select "Run as administrator"
    pause
    exit /b 1
)

:: Get the directory where this batch file is located
set "ROOT_DIR=%~dp0"
set "ROOT_DIR=%ROOT_DIR:~0,-1%"

echo ====================================
echo Project Setup Script
echo ====================================
echo.

:: ===================================
:: 1. SET WALLPAPER
:: ===================================
echo [1/6] Setting wallpaper...
set "WALLPAPER=%ROOT_DIR%\Assets\kikkeli_wp.png"

if not exist "%WALLPAPER%" (
    echo ERROR: Wallpaper file not found: %WALLPAPER%
    pause
    exit /b 1
)

powershell -Command "Add-Type -TypeDefinition 'using System; using System.Runtime.InteropServices; public class Wallpaper { [DllImport(\"user32.dll\", CharSet=CharSet.Auto)] public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni); }'; [Wallpaper]::SystemParametersInfo(0x0014, 0, '%WALLPAPER%', 0x0003)"

echo Wallpaper set successfully!
echo.

:: ===================================
:: 2. MOVE START MENU TO LEFT
:: ===================================
echo [2/6] Moving Start Menu to the left...

:: Set TaskbarAl to 0 (0 = left, 1 = center)
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v TaskbarAl /t REG_DWORD /d 0 /f >nul 2>&1

echo Start Menu alignment changed to left!
echo Restarting Explorer to apply changes...

:: Restart Explorer to apply the change
taskkill /f /im explorer.exe >nul 2>&1
start explorer.exe
timeout /t 2 /nobreak >nul

echo.

:: ===================================
:: 3. INSTALL WINDOWS DESKTOP RUNTIME
:: ===================================
echo [3/6] Installing Windows Desktop Runtime 6.0.36...
set "RUNTIME=%ROOT_DIR%\Installables\windowsdesktop-runtime-6.0.36-win-x64.exe"

if not exist "%RUNTIME%" (
    echo ERROR: Runtime installer not found: %RUNTIME%
    pause
    exit /b 1
)

echo Installing... (this may take a moment)
"%RUNTIME%" /install /quiet /norestart
if %errorLevel% equ 0 (
    echo Runtime installation completed!
) else (
    echo Runtime installation completed with code: %errorLevel%
)
echo.

:: ===================================
:: 4. LAUNCH RETROBAR
:: ===================================
echo [4/6] Preparing RetroBar...
set "RETROBAR=%ROOT_DIR%\RB\RetroBar.exe"
set "RB_DIR=%ROOT_DIR%\RB"

if not exist "%RETROBAR%" (
    echo ERROR: RetroBar.exe not found: %RETROBAR%
    pause
    exit /b 1
)

:: Kill any existing RetroBar instances
taskkill /F /IM RetroBar.exe >nul 2>&1
timeout /t 1 /nobreak >nul

:: ===================================
:: 5. CONFIGURE RETROBAR THEME
:: ===================================
echo [5/6] Configuring RetroBar theme to Windows XP Silver...

:: For portable version, settings are in the RB folder
set "CONFIG_FILE=%RB_DIR%\Settings.xml"

:: Create the settings file with Windows XP Silver theme
(
echo ^<?xml version="1.0" encoding="utf-8"?^>
echo ^<Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"^>
echo   ^<CurrentTheme^>Windows XP Silver^</CurrentTheme^>
echo   ^<EdgeMode^>None^</EdgeMode^>
echo ^</Settings^>
) > "%CONFIG_FILE%"

echo Theme configured!
echo.

:: ===================================
:: 6. ADD RETROBAR TO STARTUP
:: ===================================
echo [6/6] Adding RetroBar to startup...
set "STARTUP=%APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup"

:: Create startup shortcut using PowerShell
powershell -Command "$WshShell = New-Object -comObject WScript.Shell; $Shortcut = $WshShell.CreateShortcut('%STARTUP%\RetroBar.lnk'); $Shortcut.TargetPath = '%RETROBAR%'; $Shortcut.WorkingDirectory = '%RB_DIR%'; $Shortcut.Save()"

echo RetroBar added to startup!
echo.

:: Start RetroBar with new settings
echo Starting RetroBar with Windows XP Silver theme...
start "" "%RETROBAR%"

echo.
echo ====================================
echo Setup Complete!
echo ====================================
echo.
echo ✓ Wallpaper changed to kikkeli_wp.png
echo ✓ Start Menu moved to the left
echo ✓ Windows Desktop Runtime 6.0.36 installed
echo ✓ RetroBar launched with Windows XP Silver theme
echo ✓ RetroBar configured to start on boot
echo.
echo Your system is now configured!
echo.
pause