//! Simple FFmpeg Command Generation Examples
//! 
//! This example shows the simplest ways to generate FFmpeg commands
//! from Topaz Video AI templates.

use tvai::*;

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

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

    // Example 1: Quick command generation
    println!("1. Quick Command Generation:");
    if let Ok(cmd) = manager.quick_ffmpeg_command("upscale_to_4k", "input.mp4", "output_4k.mp4") {
        println!("Upscale to 4K:");
        println!("{}\n", cmd);
    }

    // Example 2: GPU-specific commands
    println!("2. GPU-Specific Commands:");
    
    // NVIDIA
    if let Ok(cmd) = manager.gpu_ffmpeg_command("convert_to_60fps", "input.mp4", "output_60fps.mp4", "nvidia") {
        println!("NVIDIA GPU (60fps conversion):");
        println!("{}\n", cmd);
    }
    
    // AMD
    if let Ok(cmd) = manager.gpu_ffmpeg_command("remove_noise", "noisy.mp4", "clean.mp4", "amd") {
        println!("AMD GPU (noise removal):");
        println!("{}\n", cmd);
    }

    // Example 3: CPU processing
    println!("3. CPU Processing:");
    if let Ok(cmd) = manager.cpu_ffmpeg_command("4x_slow_motion", "action.mp4", "slow.mp4") {
        println!("CPU (4x slow motion):");
        println!("{}\n", cmd);
    }

    // Example 4: Custom quality
    println!("4. Custom Quality Settings:");
    
    // High quality
    if let Ok(cmd) = manager.quality_ffmpeg_command("film_stock_4k_strong", "film.mp4", "enhanced_hq.mp4", 15, "slow") {
        println!("High Quality (CRF 15):");
        println!("{}\n", cmd);
    }
    
    // Fast encoding
    if let Ok(cmd) = manager.quality_ffmpeg_command("upscale_to_4k", "input.mp4", "output_fast.mp4", 23, "fast") {
        println!("Fast Encoding (CRF 23):");
        println!("{}\n", cmd);
    }

    // Example 5: Batch processing
    println!("5. Batch Processing:");
    let files = 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()),
    ];
    
    if let Ok(commands) = manager.batch_ffmpeg_commands("upscale_to_4k", &files, None) {
        println!("Batch commands for 4K upscaling:");
        for (i, cmd) in commands.iter().enumerate() {
            println!("  File {}: {}", i + 1, cmd);
        }
        println!();
    }

    // Example 6: Custom options
    println!("6. Custom Options:");
    let custom_opts = FfmpegCommandOptions::nvidia()
        .with_crf(18)
        .with_preset("medium")
        .with_video_codec("hevc_nvenc")
        .with_audio_codec("aac");
    
    if let Ok(cmd) = manager.generate_ffmpeg_command(
        "auto_crop_stabilization", 
        "shaky.mp4", 
        "stable.mp4", 
        Some(&custom_opts)
    ) {
        println!("Custom stabilization command:");
        println!("{}\n", cmd);
    }

    drop(manager); // Release lock

    // Example 7: Available templates
    println!("7. Available Templates:");
    let templates = list_available_templates();
    println!("You can use any of these {} templates:", templates.len());
    for (i, template) in templates.iter().take(8).enumerate() {
        println!("  {}. {}", i + 1, template);
    }
    if templates.len() > 8 {
        println!("  ... and {} more", templates.len() - 8);
    }
    println!();

    // Example 8: Command structure explanation
    println!("8. Command Structure:");
    println!("Generated commands follow this pattern:");
    println!("  ffmpeg [input_options] -i \"input.mp4\" [filters] [output_options] \"output.mp4\"");
    println!();
    println!("Where:");
    println!("  • input_options: Hardware acceleration, etc.");
    println!("  • filters: Topaz Video AI processing filters");
    println!("  • output_options: Codec, quality, format settings");
    println!();

    println!("=== Usage in Scripts ===");
    println!("You can use these commands in shell scripts:");
    println!();
    println!("Bash/PowerShell example:");
    println!("```");
    
    let manager = global_topaz_templates().lock().unwrap();
    if let Ok(cmd) = manager.quick_ffmpeg_command("upscale_to_4k", "$input", "$output") {
        println!("{}", cmd);
    }
    drop(manager);
    
    println!("```");
    println!();

    println!("=== Tips ===");
    println!("• Copy the generated commands and run them in your terminal");
    println!("• Modify input/output paths as needed");
    println!("• Adjust quality settings (CRF) based on your needs:");
    println!("  - CRF 12-15: Very high quality, large files");
    println!("  - CRF 18-23: Good quality, reasonable file size");
    println!("  - CRF 24-28: Lower quality, smaller files");
    println!("• Use GPU encoding for faster processing");
    println!("• Use CPU encoding for maximum compatibility");

    Ok(())
}
