#!/bin/bash

# Test script for cremote drag and drop functionality
# This script demonstrates all three drag and drop commands

echo "🎯 Testing Cremote Drag and Drop Functionality"
echo "=============================================="
echo ""

# Check if cremote binary exists
if [ ! -f "./cremote" ]; then
    echo "❌ Error: cremote binary not found. Please run 'go build -o cremote .' first"
    exit 1
fi

# Check if daemon is running
echo "🔍 Checking if cremotedaemon is running..."
if ! ./cremote status > /dev/null 2>&1; then
    echo "❌ Error: cremotedaemon is not running"
    echo "Please start the daemon first:"
    echo "  1. Start Chrome with debugging: chromium --remote-debugging-port=9222 --user-data-dir=/tmp/chromium-debug"
    echo "  2. Start daemon: cd daemon && ./cremotedaemon"
    exit 1
fi

echo "✅ Daemon is running"
echo ""

# Test 1: Basic drag and drop between elements
echo "🎯 Test 1: Drag and drop between elements..."
echo "Command: ./cremote drag-and-drop --source=\".draggable\" --target=\".drop-zone\" --timeout=10"
echo ""
echo "Note: This test requires a page with draggable elements."
echo "You can test this on: https://www.w3schools.com/html/html5_draganddrop.asp"
echo ""
echo "Press Enter to continue or Ctrl+C to skip..."
read -r

./cremote drag-and-drop --source="#drag1" --target="#div2" --timeout=10
if [ $? -eq 0 ]; then
    echo "✅ Drag and drop between elements completed successfully"
else
    echo "⚠️  Drag and drop between elements failed (this is expected if no suitable page is loaded)"
fi

echo ""

# Test 2: Drag and drop to coordinates
echo "🎯 Test 2: Drag and drop to specific coordinates..."
echo "Command: ./cremote drag-and-drop-coordinates --source=\".draggable\" --x=300 --y=200 --timeout=10"
echo ""

./cremote drag-and-drop-coordinates --source="#drag1" --x=300 --y=200 --timeout=10
if [ $? -eq 0 ]; then
    echo "✅ Drag and drop to coordinates completed successfully"
else
    echo "⚠️  Drag and drop to coordinates failed (this is expected if no suitable page is loaded)"
fi

echo ""

# Test 3: Drag and drop by offset
echo "🎯 Test 3: Drag and drop by relative offset..."
echo "Command: ./cremote drag-and-drop-offset --source=\".draggable\" --offset-x=100 --offset-y=50 --timeout=10"
echo ""

./cremote drag-and-drop-offset --source="#drag1" --offset-x=100 --offset-y=50 --timeout=10
if [ $? -eq 0 ]; then
    echo "✅ Drag and drop by offset completed successfully"
else
    echo "⚠️  Drag and drop by offset failed (this is expected if no suitable page is loaded)"
fi

echo ""

# Test 4: Help documentation
echo "🎯 Test 4: Testing help documentation..."
echo ""

echo "📖 drag-and-drop help:"
./cremote drag-and-drop -h
echo ""

echo "📖 drag-and-drop-coordinates help:"
./cremote drag-and-drop-coordinates -h
echo ""

echo "📖 drag-and-drop-offset help:"
./cremote drag-and-drop-offset -h
echo ""

echo "🎉 Drag and drop functionality tests completed!"
echo ""
echo "📋 Summary of new features:"
echo "  • drag-and-drop:            Drag from source element to target element"
echo "  • drag-and-drop-coordinates: Drag from source element to specific x,y coordinates"
echo "  • drag-and-drop-offset:     Drag from source element by relative pixel offset"
echo ""
echo "💡 Use cases:"
echo "  • File Upload: Drag files to upload areas"
echo "  • Sortable Lists: Reorder items in sortable lists"
echo "  • Kanban Boards: Move cards between columns"
echo "  • Image Galleries: Rearrange images or media"
echo "  • Form Builders: Drag form elements to build layouts"
echo "  • Dashboard Widgets: Rearrange dashboard components"
echo "  • Game Testing: Test drag-based game mechanics"
echo "  • UI Component Testing: Test custom drag and drop components"
echo ""
echo "🔧 Technical features:"
echo "  • Uses Chrome DevTools Protocol for precise mouse event simulation"
echo "  • Performs realistic drag operations with intermediate mouse movements"
echo "  • Calculates element center points automatically for accurate targeting"
echo "  • Supports timeout handling for slow or complex drag operations"
echo "  • Works with all modern drag and drop APIs"
echo ""
echo "🌐 Test pages you can use:"
echo "  • https://www.w3schools.com/html/html5_draganddrop.asp"
echo "  • https://jqueryui.com/droppable/"
echo "  • https://sortablejs.github.io/Sortable/"
echo "  • https://dragula.js.org/"
