#!/bin/bash

# Usage: ./generate_numbered_images.sh <image_name> <output_path> <max_number> <text_color>
# Example: ./generate_numbered_images.sh espn-plus-light.png /path/to/output 10 white
# This will generate images for numbers 1 through 10 with white text

if [ $# -ne 4 ]; then
    echo "Usage: $0 <image_name> <output_path> <max_number> <text_color>"
    echo "Example: $0 espn-plus-light.png ./output 10 white"
    echo "This will generate images numbered 1 through <max_number> with specified text color"
    echo "Common colors: white, black, red, blue, yellow, etc."
    exit 1
fi

IMAGE_NAME="$1"
OUTPUT_PATH="$2"
MAX_NUMBER="$3"
TEXT_COLOR="$4"

# URL encode the image name for the API - map to correct directory structure and set font size
case "$IMAGE_NAME" in
    espn-plus-light.png)
        ENCODED_IMAGE_NAME="espn%2Fespn-plus-light.png"
        FONT_SIZE="225"
        FONT_PATH="%2Fusr%2Fshare%2Ffonts%2Ftruetype%2Fsf-sports-night%2FSFSportsNight.ttf"
        ;;
    espn-plus-dark.png)
        ENCODED_IMAGE_NAME="espn%2Fespn-plus-dark.png"
        FONT_SIZE="225"
        FONT_PATH="%2Fusr%2Fshare%2Ffonts%2Ftruetype%2Fsf-sports-night%2FSFSportsNight.ttf"
        ;;
    sportsnet-plus-light.png)
        ENCODED_IMAGE_NAME="sportsnet%2Fsportsnet-plus-light.png"
        FONT_SIZE="160"
        FONT_PATH="%2Fusr%2Fshare%2Ffonts%2Ftruetype%2Fgoogle-fonts%2FRoboto-Black.ttf"
        ;;
    sportsnet-plus-dark.png)
        ENCODED_IMAGE_NAME="sportsnet%2Fsportsnet-plus-dark.png"
        FONT_SIZE="160"
        FONT_PATH="%2Fusr%2Fshare%2Ffonts%2Ftruetype%2Fgoogle-fonts%2FRoboto-Black.ttf"
        ;;
    tsn-plus-light.png)
        ENCODED_IMAGE_NAME="tsn%2Ftsn-plus-light.png"
        FONT_SIZE="225"
        FONT_PATH="%2Fusr%2Fshare%2Ffonts%2Ftruetype%2Fsf-sports-night%2FSFSportsNight.ttf"
        ;;
    tsn-plus-dark.png)
        ENCODED_IMAGE_NAME="tsn%2Ftsn-plus-dark.png"
        FONT_SIZE="225"
        FONT_PATH="%2Fusr%2Fshare%2Ffonts%2Ftruetype%2Fsf-sports-night%2FSFSportsNight.ttf"
        ;;
    *)
        echo "Error: Unknown image name: $IMAGE_NAME"
        echo "Supported images: espn-plus-light.png, espn-plus-dark.png, sportsnet-plus-light.png, sportsnet-plus-dark.png, tsn-plus-light.png, tsn-plus-dark.png"
        exit 1
        ;;
esac

# Base URL template
BASE_URL="http://nymeria.swvn.local:5123/api/image?url=https%3A%2F%2Fgit.seth.services%2Fseth-public%2Fscripts%2Fraw%2Fbranch%2Fmain%2F${ENCODED_IMAGE_NAME}&text=NUMBER&position=right&font_size=${FONT_SIZE}&padding=auto&text_color=${TEXT_COLOR}&bg_color=transparent&font_path=${FONT_PATH}"

# Create output directory if it doesn't exist
mkdir -p "$OUTPUT_PATH"

echo "Starting parallel downloads..."

# Start all downloads in parallel
pids=()
for number in $(seq 1 "$MAX_NUMBER"); do
    echo "Starting download for number: $number"
    
    # Replace NUMBER placeholder with actual number
    URL=$(echo "$BASE_URL" | sed "s/NUMBER/$number/g")
    
    # Generate output filename
    OUTPUT_FILE="${OUTPUT_PATH}/${IMAGE_NAME%.png}_${number}.png"
    
    # Download the image in background
    (
        if curl -s "$URL" -o "$OUTPUT_FILE" 2>/dev/null; then
            echo "✓ Saved: $OUTPUT_FILE"
        else
            echo "✗ Failed to generate image for number: $number"
        fi
    ) &
    
    pids+=($!)
done

# Wait for all background processes to complete
echo "Waiting for all downloads to complete..."
for pid in "${pids[@]}"; do
    wait "$pid"
done

echo "Done! Generated images saved to: $OUTPUT_PATH"