//! FFmpeg Command Generation Demo
//! 
//! This example demonstrates how to generate FFmpeg commands
//! from Topaz Video AI templates for various processing scenarios.

use tvai::*;

fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    println!("=== FFmpeg Command Generation Demo ===\n");

    // Get template manager
    let manager = global_topaz_templates().lock().unwrap();

    // Demo 1: Basic command generation
    println!("1. Basic Command Generation:");
    match manager.quick_ffmpeg_command(
        "upscale_to_4k",
        "input.mp4",
        "output_4k.mp4"
    ) {
        Ok(cmd) => {
            println!("Template: upscale_to_4k");
            println!("Command: {}", cmd);
        }
        Err(e) => println!("Error: {}", e),
    }
    println!();

    // Demo 2: GPU-specific commands
    println!("2. GPU-Specific Commands:");
    
    // NVIDIA GPU
    match manager.gpu_ffmpeg_command(
        "convert_to_60fps",
        "input.mp4",
        "output_60fps.mp4",
        "nvidia"
    ) {
        Ok(cmd) => {
            println!("NVIDIA GPU Command:");
            println!("{}", cmd);
        }
        Err(e) => println!("NVIDIA Error: {}", e),
    }
    println!();

    // AMD GPU
    match manager.gpu_ffmpeg_command(
        "remove_noise",
        "noisy_input.mp4",
        "clean_output.mp4",
        "amd"
    ) {
        Ok(cmd) => {
            println!("AMD GPU Command:");
            println!("{}", cmd);
        }
        Err(e) => println!("AMD Error: {}", e),
    }
    println!();

    // Demo 3: CPU processing
    println!("3. CPU Processing Command:");
    match manager.cpu_ffmpeg_command(
        "4x_slow_motion",
        "action.mp4",
        "slow_motion.mp4"
    ) {
        Ok(cmd) => {
            println!("CPU Command:");
            println!("{}", cmd);
        }
        Err(e) => println!("CPU Error: {}", e),
    }
    println!();

    // Demo 4: Custom quality settings
    println!("4. Custom Quality Commands:");
    
    // High quality (CRF 15, slow preset)
    match manager.quality_ffmpeg_command(
        "film_stock_4k_strong",
        "film_input.mp4",
        "film_output_hq.mp4",
        15,
        "slow"
    ) {
        Ok(cmd) => {
            println!("High Quality Command (CRF 15, slow preset):");
            println!("{}", cmd);
        }
        Err(e) => println!("High Quality Error: {}", e),
    }
    println!();

    // Fast encoding (CRF 23, fast preset)
    match manager.quality_ffmpeg_command(
        "upscale_to_4k",
        "input.mp4",
        "output_fast.mp4",
        23,
        "fast"
    ) {
        Ok(cmd) => {
            println!("Fast Encoding Command (CRF 23, fast preset):");
            println!("{}", cmd);
        }
        Err(e) => println!("Fast Encoding Error: {}", e),
    }
    println!();

    // Demo 5: Custom options
    println!("5. Custom Options:");
    let custom_options = FfmpegCommandOptions::default()
        .with_crf(20)
        .with_preset("medium")
        .with_video_codec("libx264")
        .with_audio_codec("aac")
        .with_custom_args(vec!["-movflags".to_string(), "+faststart".to_string()]);

    match manager.generate_ffmpeg_command(
        "auto_crop_stabilization",
        "shaky_input.mp4",
        "stable_output.mp4",
        Some(&custom_options)
    ) {
        Ok(cmd) => {
            println!("Custom Options Command:");
            println!("{}", cmd);
        }
        Err(e) => println!("Custom Options Error: {}", e),
    }
    println!();

    // Demo 6: Batch processing commands
    println!("6. Batch Processing Commands:");
    let input_output_pairs = vec![
        ("video1.mp4".to_string(), "enhanced1.mp4".to_string()),
        ("video2.mp4".to_string(), "enhanced2.mp4".to_string()),
        ("video3.mp4".to_string(), "enhanced3.mp4".to_string()),
    ];

    match manager.batch_ffmpeg_commands(
        "upscale_to_4k",
        &input_output_pairs,
        None
    ) {
        Ok(commands) => {
            println!("Batch Commands for upscale_to_4k:");
            for (i, cmd) in commands.iter().enumerate() {
                println!("  {}. {}", i + 1, cmd);
            }
        }
        Err(e) => println!("Batch Error: {}", e),
    }
    println!();

    // Demo 7: Different templates showcase
    println!("7. Different Templates Showcase:");
    let templates_to_demo = [
        ("upscale_to_4k", "input.mp4", "output_4k.mp4"),
        ("convert_to_60fps", "input.mp4", "output_60fps.mp4"),
        ("remove_noise", "noisy.mp4", "clean.mp4"),
        ("4x_slow_motion", "action.mp4", "slow.mp4"),
        ("auto_crop_stabilization", "shaky.mp4", "stable.mp4"),
    ];

    for (template, input, output) in &templates_to_demo {
        match manager.quick_ffmpeg_command(template, input, output) {
            Ok(cmd) => {
                // Get template info directly from manager to avoid deadlock
                if let Some(template_obj) = manager.get_template(template) {
                    println!("Template: {} ({})", template_obj.name, template);
                    println!("Description: {}", template_obj.description);
                    println!("Command: {}", cmd);
                    println!();
                }
            }
            Err(e) => println!("Error with template {}: {}", template, e),
        }
    }

    drop(manager); // Release lock

    // Demo 8: Command options comparison
    println!("8. Command Options Comparison:");
    println!("CPU vs GPU vs Custom options for the same template:\n");

    let manager = global_topaz_templates().lock().unwrap();
    
    // CPU
    if let Ok(cpu_cmd) = manager.cpu_ffmpeg_command("upscale_to_4k", "input.mp4", "output_cpu.mp4") {
        println!("CPU Command:");
        println!("{}\n", cpu_cmd);
    }
    
    // NVIDIA GPU
    if let Ok(gpu_cmd) = manager.gpu_ffmpeg_command("upscale_to_4k", "input.mp4", "output_gpu.mp4", "nvidia") {
        println!("NVIDIA GPU Command:");
        println!("{}\n", gpu_cmd);
    }
    
    // Custom high-quality
    let hq_options = FfmpegCommandOptions::nvidia().with_crf(12).with_preset("slow");
    if let Ok(hq_cmd) = manager.generate_ffmpeg_command("upscale_to_4k", "input.mp4", "output_hq.mp4", Some(&hq_options)) {
        println!("High Quality NVIDIA Command:");
        println!("{}\n", hq_cmd);
    }

    drop(manager);

    println!("=== Usage Tips ===");
    println!("• Use quick_ffmpeg_command() for simple cases");
    println!("• Use gpu_ffmpeg_command() to specify GPU type");
    println!("• Use cpu_ffmpeg_command() for CPU-only processing");
    println!("• Use quality_ffmpeg_command() for custom quality settings");
    println!("• Use generate_ffmpeg_command() for full customization");
    println!("• Use batch_ffmpeg_commands() for multiple files");
    println!();

    println!("=== Command Generation Complete ===");
    println!("You can now copy and run these FFmpeg commands directly!");
    println!("Note: Actual Topaz Video AI filters may require the Topaz FFmpeg plugin.");

    Ok(())
}
