#!/bin/bash

echo "🍓 Adhaan Desktop Kiosk Setup"
echo "============================="

# Zorg dat Docker en Docker Compose er zijn
echo "🐳 Docker controleren..."
if ! command -v docker &> /dev/null; then
    echo "Docker installeren..."
    curl -fsSL https://get.docker.com -o get-docker.sh
    sudo sh get-docker.sh
    sudo usermod -aG docker $USER
    rm get-docker.sh
fi

if ! command -v docker-compose &> /dev/null; then
    echo "Docker Compose installeren..."
    sudo apt install -y docker-compose
fi

# Zorg dat Chromium er is
echo "🌐 Browser controleren..."
sudo apt install -y chromium-browser

# Configureer HDMI audio automatisch
echo "🔊 HDMI audio automatisch configureren..."
sudo raspi-config nonint do_audio 1  # 0=auto, 1=hdmi, 2=headphones

# Maak kiosk script voor DESKTOP gebruik
echo "📝 Desktop kiosk script aanmaken..."
cat > /home/$USER/kiosk.sh << 'KIOSK_EOF'
#!/bin/bash
# Wacht tot desktop volledig geladen is
sleep 15

echo "Starting Adhaan Kiosk..."

# Forceer HDMI audio output
echo "🔊 HDMI audio configureren..."
amixer cset numid=3 2  # 0=auto, 1=headphones, 2=hdmi
sudo raspi-config nonint do_audio 2  # Force HDMI audio

# Zet volume op redelijk niveau (70%)
amixer set PCM 70%

# Start Docker containers
cd /home/pi/adhan
echo "Starting Docker containers..."
docker-compose -f docker-compose-pi.yml up -d

# Wacht tot containers draaien
sleep 10

# Sluit eventuele andere browsers
pkill -f chromium-browser 2>/dev/null || true

# Start browser in kiosk mode met audio optimalisaties
echo "Starting browser..."
DISPLAY=:0 chromium-browser \
    --kiosk \
    --start-maximized \
    --noerrdialogs \
    --disable-infobars \
    --no-first-run \
    --disable-session-crashed-bubble \
    --disable-restore-session-state \
    --disable-web-security \
    --disable-features=VizDisplayCompositor \
    --autoplay-policy=no-user-gesture-required \
    --allow-running-insecure-content \
    --disable-background-timer-throttling \
    --disable-renderer-backgrounding \
    --disable-translate \
    --disable-features=Translate \
    --disable-component-extensions-with-background-pages \
    --lang=nl \
    http://localhost:5090 &

echo "Kiosk started!"
KIOSK_EOF

chmod +x /home/$USER/kiosk.sh

# Configureer DESKTOP autostart (niet openbox!)
echo "⚙️ Desktop autostart configureren..."
mkdir -p ~/.config/autostart

cat > ~/.config/autostart/adhaan-kiosk.desktop << 'AUTOSTART_EOF'
[Desktop Entry]
Type=Application
Name=Adhaan Kiosk
Comment=Start Adhaan in fullscreen
Exec=/home/pi/kiosk.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
StartupNotify=false
Terminal=false
AUTOSTART_EOF

chmod +x ~/.config/autostart/adhaan-kiosk.desktop

# Zorg voor DESKTOP autologin (niet console!)
echo "👤 Desktop autologin configureren..."
sudo raspi-config nonint do_boot_behaviour B4

# Zorg dat we naar DESKTOP booten (niet console!)
echo "🖥️ Desktop boot configureren..."
sudo systemctl set-default graphical.target
sudo systemctl unmask lightdm 2>/dev/null || true
sudo systemctl enable lightdm

# Verwijder eventuele console configuraties
sudo rm -f /etc/systemd/system/getty@tty1.service.d/override.conf 2>/dev/null || true
sudo rm -f /etc/systemd/system/adhan-docker.service 2>/dev/null || true

# Schakel power management uit voor kiosk
echo "⚡ Power management uitschakelen..."
cat > ~/.config/autostart/disable-power-management.desktop << 'POWER_EOF'
[Desktop Entry]
Type=Application
Name=Disable Power Management
Exec=sh -c 'xset -dpms; xset s noblank; xset s off'
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
StartupNotify=false
Terminal=false
POWER_EOF

# 💀 HARDCORE BOOT OPTIMALISATIES voor DESKTOP
echo "💀 Desktop hardcore optimalisaties..."

# Disable services die desktop niet nodig heeft
sudo systemctl disable bluetooth.service 2>/dev/null || true
sudo systemctl disable ModemManager.service 2>/dev/null || true
sudo systemctl disable systemd-timesyncd.service 2>/dev/null || true
sudo systemctl disable NetworkManager-wait-online.service 2>/dev/null || true

# Boot optimalisaties
echo "# Desktop boot optimalisaties" | sudo tee -a /boot/config.txt
echo "boot_delay=0" | sudo tee -a /boot/config.txt
echo "gpu_mem=128" | sudo tee -a /boot/config.txt
echo "arm_freq=1200" | sudo tee -a /boot/config.txt

# Kernel optimalisaties
if ! grep -q "quiet" /boot/cmdline.txt; then
    sudo sed -i 's/$/ quiet fastboot/' /boot/cmdline.txt
fi

# Test Docker setup
echo "🧪 Docker setup testen..."
cd /home/$USER/adhan
if docker-compose -f docker-compose-pi.yml config > /dev/null 2>&1; then
    echo "✅ Docker configuratie OK"
else
    echo "❌ Docker configuratie probleem"
fi

echo ""
echo "✅ Desktop kiosk setup voltooid!"
echo ""
echo "📋 Volgende stappen:"
echo "1. Test handmatig: ~/kiosk.sh"
echo "2. Als het werkt: sudo reboot"
echo "3. Pi start naar DESKTOP en opent automatisch kiosk"
echo ""
echo "🔧 Handmatige bediening:"
echo "- Alt+F4: Sluit browser (terug naar desktop)"
echo "- ~/kiosk.sh: Start kiosk opnieuw"
echo "- Desktop blijft beschikbaar!"
echo ""
echo "🎯 Belangrijke verschillen met origineel script:"
echo "- Behoudt Desktop functionaliteit"
echo "- Gebruikt desktop autostart (niet console)"
echo "- Lightdm blijft enabled"
echo "- Geen complexe boot configuraties"
