#!/bin/bash
# Test-script för att debugga wkhtmltoimage med Plandisc

echo "=== wkhtmltoimage Test för Plandisc ==="
echo ""

URL="https://create.plandisc.com/wheel/embed/LyKTaIy"
OUTPUT="/tmp/plandisc_test.png"

echo "URL: $URL"
echo "Output: $OUTPUT"
echo ""

# Test 1: Minimal (snabbast)
echo "Test 1: Minimal rendering (3 sek delay)..."
xvfb-run -a wkhtmltoimage \
  --format png \
  --quality 90 \
  --width 1080 \
  --height 1080 \
  --enable-javascript \
  --javascript-delay 3000 \
  --no-stop-slow-scripts \
  "$URL" "${OUTPUT}_test1.png"

if [ -f "${OUTPUT}_test1.png" ]; then
  SIZE=$(ls -lh "${OUTPUT}_test1.png" | awk '{print $5}')
  echo "✅ Skapad: ${OUTPUT}_test1.png ($SIZE)"
else
  echo "❌ Misslyckades"
fi
echo ""

# Test 2: Normal (10 sek delay)
echo "Test 2: Normal rendering (10 sek delay)..."
xvfb-run -a wkhtmltoimage \
  --format png \
  --quality 90 \
  --width 1080 \
  --height 1080 \
  --enable-javascript \
  --javascript-delay 10000 \
  --no-stop-slow-scripts \
  --disable-smart-width \
  --load-error-handling ignore \
  --load-media-error-handling ignore \
  "$URL" "${OUTPUT}_test2.png"

if [ -f "${OUTPUT}_test2.png" ]; then
  SIZE=$(ls -lh "${OUTPUT}_test2.png" | awk '{print $5}')
  echo "✅ Skapad: ${OUTPUT}_test2.png ($SIZE)"
else
  echo "❌ Misslyckades"
fi
echo ""

# Test 3: Maximal (20 sek delay)
echo "Test 3: Maximal rendering (20 sek delay)..."
xvfb-run -a wkhtmltoimage \
  --format png \
  --quality 90 \
  --width 1080 \
  --height 1080 \
  --enable-javascript \
  --javascript-delay 20000 \
  --no-stop-slow-scripts \
  --disable-smart-width \
  --load-error-handling ignore \
  --load-media-error-handling ignore \
  --encoding utf-8 \
  --zoom 1.0 \
  "$URL" "${OUTPUT}_test3.png"

if [ -f "${OUTPUT}_test3.png" ]; then
  SIZE=$(ls -lh "${OUTPUT}_test3.png" | awk '{print $5}')
  echo "✅ Skapad: ${OUTPUT}_test3.png ($SIZE)"
else
  echo "❌ Misslyckades"
fi
echo ""

echo "=== RESULTAT ==="
echo "Jämför bilderna för att se vilken delay som fungerar bäst:"
ls -lh /tmp/plandisc_test*.png 2>/dev/null
echo ""
echo "Kopiera bästa bilden till screenshots för att testa:"
echo "cp /tmp/plandisc_test2.png /var/www/api.signcast.se/public_html/screencapture/screenshots/plandisc_jordbruk.png"

