#!/bin/bash

# Define scenes, sigmas, and schedulers
scenes=("bicycle" "bonsai" "counter" "kitchen" "garden" "room" "stump")
mip_nerf_sigma=(30 100)
mip_nerf_noise_scheduler=("sqrt" "linear" "cosine")

# Function to echo with timestamp
log_message() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}

# Function to run the render script for a specific scene, sigma, and scheduler
run_render() {
    local scene=$1
    local sigma=$2
    local scheduler=$3
    
    # Base paths for current scene
    BASE_PATH="/project/hentci/ours_data/mip-nerf-360/poison_${scene}"
    POINTS_DIR="${BASE_PATH}/sparse/0"
    ORIGINAL_POINTS="${POINTS_DIR}/points3D.ply"
    KDE_POINTS="${POINTS_DIR}/points3D_KDE7.5.ply"  # Always use KDE 7.5
    
    log_message "Starting rendering for scene ${scene} with sigma ${sigma} and scheduler ${scheduler}"
    
    # Save the current points3D.ply if it exists
    if [ -f "$ORIGINAL_POINTS" ]; then
        log_message "Backing up existing points3D.ply"
        mv "$ORIGINAL_POINTS" "${ORIGINAL_POINTS}.backup"
        log_message "Backup created at: ${ORIGINAL_POINTS}.backup"
    fi
    
    # Copy the fixed KDE points file to points3D.ply
    log_message "Copying points3D_KDE7.5.ply to points3D.ply"
    cp "$KDE_POINTS" "$ORIGINAL_POINTS"
    
    if [ $? -eq 0 ]; then
        log_message "File copied successfully"
    else
        log_message "ERROR: Failed to copy file"
        return 1
    fi
    
    # Create output directory if it doesn't exist
    OUTPUT_DIR="/project2/hentci/Metrics/ablation_study/scheduling_noise/${scene}/sigma${sigma}/${scheduler}"
    mkdir -p "$OUTPUT_DIR"
    
    log_message "Starting render script with these parameters:"
    log_message "- Scene: $scene"
    log_message "- Sigma: $sigma"
    log_message "- Scheduler: $scheduler"
    log_message "- Source path: $BASE_PATH"
    log_message "- Image directory: images"
    log_message "- Output path: $OUTPUT_DIR"
    
    # Run the render script
    python3 ../render.py \
        -s "$BASE_PATH" \
        -i images \
        -m "$OUTPUT_DIR" \
        --eval
    
    # Check if render script executed successfully
    if [ $? -eq 0 ]; then
        log_message "Render script completed successfully"
    else
        log_message "ERROR: Render script failed"
    fi
    
    # Restore the original points3D.ply
    log_message "Cleaning up: Removing temporary points3D.ply"
    rm "$ORIGINAL_POINTS"
    
    if [ -f "${ORIGINAL_POINTS}.backup" ]; then
        log_message "Restoring original points3D.ply from backup"
        mv "${ORIGINAL_POINTS}.backup" "$ORIGINAL_POINTS"
        log_message "Backup restored successfully"
    fi
    
    log_message "Rendering for scene ${scene} with sigma ${sigma} and scheduler ${scheduler} completed"
    log_message "----------------------------------------"
}

# Main execution
log_message "Starting execution of all render experiments"
log_message "Will run renders for ${#scenes[@]} scenes, ${#mip_nerf_sigma[@]} sigma values, and ${#mip_nerf_noise_scheduler[@]} schedulers"
log_message "Scenes: ${scenes[*]}"
log_message "Sigma values: ${mip_nerf_sigma[*]}"
log_message "Schedulers: ${mip_nerf_noise_scheduler[*]}"
log_message "Note: Using fixed points3D_KDE7.5.ply for all experiments"
log_message "----------------------------------------"

# Loop through each scene, sigma, and scheduler
for scene in "${scenes[@]}"; do
    for sigma in "${mip_nerf_sigma[@]}"; do
        for scheduler in "${mip_nerf_noise_scheduler[@]}"; do
            run_render "$scene" "$sigma" "$scheduler"
        done
    done
done

log_message "All render experiments completed"