#!/bin/bash
# clipimage-paste - Advanced clipboard image handler for Claude Code
# Provides multiple output formats and clipboard inspection
#
# Usage:
#   clipimage-paste                    # Paste to temp file, output path
#   clipimage-paste --json             # Output JSON with metadata
#   clipimage-paste --base64           # Output base64 encoded
#   clipimage-paste --info             # Show clipboard image info only
#   clipimage-paste --watch            # Watch for new images

set -e

ACTION="paste"
OUTPUT_PATH="/tmp/clipboard-image-$(date +%s).png"

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --json)
            ACTION="json"
            shift
            ;;
        --base64)
            ACTION="base64"
            shift
            ;;
        --info)
            ACTION="info"
            shift
            ;;
        --watch)
            ACTION="watch"
            shift
            ;;
        -o|--output)
            OUTPUT_PATH="$2"
            shift 2
            ;;
        -h|--help)
            echo "Usage: clipimage-paste [OPTIONS]"
            echo ""
            echo "Options:"
            echo "  -o, --output PATH    Output file path"
            echo "  --json               Output as JSON"
            echo "  --base64             Output as base64"
            echo "  --info               Show clipboard info only"
            echo "  --watch              Watch for new images"
            echo "  -h, --help           Show this help"
            echo ""
            echo "Examples:"
            echo "  clipimage-paste                    # Save to temp file"
            echo "  clipimage-paste -o shot.png        # Save to specific file"
            echo "  clipimage-paste --json             # Get JSON output"
            echo "  clipimage-paste --base64 | pbcopy  # Copy base64 to clipboard"
            exit 0
            ;;
        *)
            echo "Unknown option: $1" >&2
            exit 1
            ;;
    esac
done

# Function to get image from clipboard
get_clipboard_image() {
    local output_file="$1"

    # Try Wayland first
    if command -v wl-paste &> /dev/null; then
        if wl-paste --type image/png > "$output_file" 2>/dev/null && [[ -s "$output_file" ]]; then
            return 0
        fi
    fi

    # Fallback to X11
    if command -v xclip &> /dev/null; then
        if xclip -selection clipboard -t image/png -o > "$output_file" 2>/dev/null && [[ -s "$output_file" ]]; then
            return 0
        fi
    fi

    return 1
}

# Function to get image info
get_image_info() {
    local file="$1"
    local size_bytes=$(stat -c%s "$file" 2>/dev/null || stat -f%z "$file" 2>/dev/null || echo "0")
    local size_human=$(du -h "$file" 2>/dev/null | cut -f1 || echo "unknown")
    local dimensions=$(identify -format "%wx%h" "$file" 2>/dev/null || echo "unknown")
    local mime=$(file --mime-type -b "$file" 2>/dev/null || echo "image/png")

    cat <<EOF
{
  "path": "$file",
  "size_bytes": $size_bytes,
  "size_human": "$size_human",
  "dimensions": "$dimensions",
  "mime_type": "$mime",
  "timestamp": "$(date -Iseconds)"
}
EOF
}

# Handle different actions
case "$ACTION" in
    paste)
        if get_clipboard_image "$OUTPUT_PATH"; then
            echo "$OUTPUT_PATH"
        else
            echo "No image in clipboard" >&2
            exit 1
        fi
        ;;

    json)
        if get_clipboard_image "$OUTPUT_PATH"; then
            get_image_info "$OUTPUT_PATH"
            rm -f "$OUTPUT_PATH"  # Cleanup temp file
        else
            echo '{"error": "No image in clipboard"}' >&2
            exit 1
        fi
        ;;

    base64)
        if get_clipboard_image "$OUTPUT_PATH"; then
            base64 "$OUTPUT_PATH"
            rm -f "$OUTPUT_PATH"
        else
            echo "No image in clipboard" >&2
            exit 1
        fi
        ;;

    info)
        if get_clipboard_image "$OUTPUT_PATH"; then
            get_image_info "$OUTPUT_PATH"
            rm -f "$OUTPUT_PATH"
        else
            echo '{"error": "No image in clipboard"}' >&2
            exit 1
        fi
        ;;

    watch)
        echo "Watching clipboard for images... Press Ctrl+C to stop"
        last_hash=""
        while true; do
            if get_clipboard_image "$OUTPUT_PATH"; then
                current_hash=$(md5sum "$OUTPUT_PATH" | cut -d' ' -f1)
                if [[ "$current_hash" != "$last_hash" ]]; then
                    echo "[$(date '+%H:%M:%S')] New image: $OUTPUT_PATH"
                    get_image_info "$OUTPUT_PATH"
                    last_hash="$current_hash"
                fi
                rm -f "$OUTPUT_PATH"
            fi
            sleep 1
        done
        ;;
esac
