//! Demo of integrated template API
//! 
//! This example demonstrates how to use the integrated template API
//! for convenient video processing with predefined templates.

use tvai::*;

#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
    println!("=== Integrated Template API Demo ===\n");

    // List all available templates
    println!("Available templates:");
    let templates = list_available_templates();
    for (i, template_name) in templates.iter().enumerate() {
        if let Some((name, description)) = get_template_info(template_name) {
            println!("  {}. {} - {}", i + 1, name, description);
        }
    }
    println!("Total: {} templates\n", templates.len());

    // Demonstrate template information retrieval
    println!("=== Template Information ===");
    let sample_templates = ["upscale_to_4k", "convert_to_60fps", "remove_noise", "4x_slow_motion"];
    
    for template_name in &sample_templates {
        if let Some((name, description)) = get_template_info(template_name) {
            println!("Template: {}", name);
            println!("  ID: {}", template_name);
            println!("  Description: {}", description);
            println!();
        }
    }

    // Demonstrate template application (without actual processing)
    println!("=== Template Application Demo ===");
    
    // Get template manager to show applied parameters
    let manager = global_topaz_templates().lock().unwrap();
    
    for template_name in &sample_templates {
        match manager.apply_template(template_name) {
            Ok(applied) => {
                println!("✓ Template '{}' applied successfully:", applied.name);
                println!("  Description: {}", applied.description);
                
                if let Some(upscale) = &applied.enhance_params.upscale {
                    println!("  Upscale parameters:");
                    println!("    Scale factor: {}x", upscale.scale_factor);
                    println!("    Model: {:?}", upscale.model);
                    println!("    Compression: {}", upscale.compression);
                    println!("    Quality preset: {:?}", upscale.quality_preset);
                    
                    if upscale.noise > 0.0 {
                        println!("    Noise reduction: {}", upscale.noise);
                    }
                    if upscale.details > 0.0 {
                        println!("    Detail enhancement: {}", upscale.details);
                    }
                    if upscale.halo > 0.0 {
                        println!("    Halo reduction: {}", upscale.halo);
                    }
                    if upscale.blur > 0.0 {
                        println!("    Blur reduction: {}", upscale.blur);
                    }
                }
                
                if let Some(interpolation) = &applied.enhance_params.interpolation {
                    println!("  Interpolation parameters:");
                    println!("    Multiplier: {}x", interpolation.multiplier);
                    println!("    Model: {:?}", interpolation.model);
                    println!("    Input FPS: {}", interpolation.input_fps);
                    if let Some(target_fps) = interpolation.target_fps {
                        println!("    Target FPS: {}", target_fps);
                    }
                }
                
                if applied.stabilization_enabled {
                    println!("  ✓ Stabilization enabled");
                }
                
                if applied.grain_enabled {
                    println!("  ✓ Grain effect enabled");
                }
                
                if let Some(fps) = applied.output_fps {
                    println!("  Output FPS: {}", fps);
                }
                
                println!();
            }
            Err(e) => {
                println!("✗ Failed to apply template '{}': {}", template_name, e);
            }
        }
    }

    drop(manager); // Release lock

    // Show how to use the convenient API functions
    println!("=== Convenient API Functions ===");
    println!("The following functions are available for easy video processing:");
    println!();
    
    println!("// General template processing");
    println!("process_with_template(input, output, \"upscale_to_4k\").await?;");
    println!();
    
    println!("// Specific template shortcuts");
    println!("upscale_to_4k(input, output).await?;");
    println!("convert_to_60fps(input, output).await?;");
    println!("remove_noise(input, output).await?;");
    println!("slow_motion_4x(input, output).await?;");
    println!("auto_crop_stabilization(input, output).await?;");
    println!();

    // Example usage patterns
    println!("=== Example Usage Patterns ===");
    println!();
    
    println!("1. Simple 4K upscaling:");
    println!("   let result = upscale_to_4k(Path::new(\"input.mp4\"), Path::new(\"output_4k.mp4\")).await?;");
    println!();
    
    println!("2. Convert to 60fps:");
    println!("   let result = convert_to_60fps(Path::new(\"input.mp4\"), Path::new(\"output_60fps.mp4\")).await?;");
    println!();
    
    println!("3. Remove noise:");
    println!("   let result = remove_noise(Path::new(\"noisy_input.mp4\"), Path::new(\"clean_output.mp4\")).await?;");
    println!();
    
    println!("4. Create slow motion:");
    println!("   let result = slow_motion_4x(Path::new(\"input.mp4\"), Path::new(\"slow_motion.mp4\")).await?;");
    println!();
    
    println!("5. Stabilize shaky video:");
    println!("   let result = auto_crop_stabilization(Path::new(\"shaky.mp4\"), Path::new(\"stable.mp4\")).await?;");
    println!();

    println!("=== Custom Template Usage ===");
    println!();
    println!("You can also load custom templates:");
    println!("let mut manager = TopazTemplateManager::new();");
    println!("manager.load_from_file(\"my_template\".to_string(), \"path/to/template.json\")?;");
    println!("let result = process_with_template(input, output, \"my_template\").await?;");
    println!();

    println!("Or load all templates from a directory:");
    println!("let count = manager.load_examples_templates(\"templates/\")?;");
    println!("println!(\"Loaded {{}} templates\", count);");
    println!();

    println!("=== Demo Complete ===");
    println!("The template system is now fully integrated into the tvai library!");
    println!("You can use any of the convenient functions shown above for video processing.");
    
    Ok(())
}
