use tvai_sdk::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("Topaz Video AI SDK - Output Settings Demo");
    println!("========================================");

    let sdk = TvaiSdk::new();

    // Example 1: Social Media Square with Crop to Fit
    println!("\n1. Social Media Square Template (Crop to Fit):");
    let social_template = TemplatePresets::social_media_square()?;
    println!("   Template: {}", social_template.name);
    println!("   Description: {}", social_template.description);
    println!("   Settings:");
    println!("     Resolution: {}x{}", 
        social_template.settings.output.width.unwrap_or(0),
        social_template.settings.output.height.unwrap_or(0)
    );
    println!("     Crop to fit: {}", social_template.settings.output.crop_to_fit);
    println!("     Lock aspect ratio: {}", social_template.settings.output.lock_aspect_ratio.unwrap_or(true));
    println!("     Custom resolution priority: {}", social_template.settings.output.custom_resolution_priority.unwrap_or(0));

    let social_command = sdk.generate_ffmpeg_command(&social_template, "input_16x9.mp4", "output_square.mp4")?;
    println!("   Command:");
    println!("   {}", social_command);

    // Example 2: Letterbox Preserve Aspect Ratio
    println!("\n2. Letterbox Preserve Template (Padding):");
    let letterbox_template = TemplatePresets::letterbox_preserve()?;
    println!("   Template: {}", letterbox_template.name);
    println!("   Settings:");
    println!("     Resolution: {}x{}", 
        letterbox_template.settings.output.width.unwrap_or(0),
        letterbox_template.settings.output.height.unwrap_or(0)
    );
    println!("     Crop to fit: {}", letterbox_template.settings.output.crop_to_fit);
    println!("     Lock aspect ratio: {}", letterbox_template.settings.output.lock_aspect_ratio.unwrap_or(true));

    let letterbox_command = sdk.generate_ffmpeg_command(&letterbox_template, "input_4x3.mp4", "output_letterbox.mp4")?;
    println!("   Command:");
    println!("   {}", letterbox_command);

    // Example 3: Minimal Processing (Output Settings Disabled)
    println!("\n3. Minimal Processing Template (Output Settings Disabled):");
    let minimal_template = TemplatePresets::minimal_processing()?;
    println!("   Template: {}", minimal_template.name);
    println!("   Settings:");
    println!("     Output active: {}", minimal_template.settings.output.active);

    let minimal_command = sdk.generate_ffmpeg_command(&minimal_template, "input.mp4", "output_minimal.mp4")?;
    println!("   Command:");
    println!("   {}", minimal_command);

    // Example 4: Custom Template with Advanced Output Settings
    println!("\n4. Custom Template with Advanced Output Settings:");
    let custom_template = TemplateBuilder::new("Advanced Output Demo")
        .description("Demonstrates all output settings parameters")
        .enable_enhancement("prob-4")
        .enhancement_params(25, 20, 10)
        .resolution(1280, 720) // 720p target
        .video_codec("libx264", Some(24))
        .preset("medium")
        .custom_resolution_priority(1) // High priority for custom resolution
        .lock_aspect_ratio(true) // Preserve aspect ratio
        .crop_to_fit(false) // Use padding
        .pixel_aspect_ratio(2) // 16:9 aspect ratio
        .audio_settings("aac", 128, 2)
        .build()?;

    println!("   Template: {}", custom_template.name);
    println!("   Settings:");
    println!("     Resolution: {}x{}", 
        custom_template.settings.output.width.unwrap_or(0),
        custom_template.settings.output.height.unwrap_or(0)
    );
    println!("     Custom resolution priority: {}", custom_template.settings.output.custom_resolution_priority.unwrap_or(0));
    println!("     Lock aspect ratio: {}", custom_template.settings.output.lock_aspect_ratio.unwrap_or(true));
    println!("     Crop to fit: {}", custom_template.settings.output.crop_to_fit);
    println!("     Pixel aspect ratio: {}", custom_template.settings.output.output_par);

    let custom_command = sdk.generate_ffmpeg_command(&custom_template, "input.mp4", "output_custom.mp4")?;
    println!("   Command:");
    println!("   {}", custom_command);

    // Example 5: Comparison of Different Resolution Priorities
    println!("\n5. Resolution Priority Comparison:");
    
    // Priority 0: Normal (custom resolution used)
    let priority0_template = TemplateBuilder::new("Priority 0")
        .enable_enhancement("prob-4")
        .resolution(1920, 1080)
        .output_settings(6, 0.0) // FHD size method
        .custom_resolution_priority(0) // Normal priority
        .build()?;

    // Priority 2: Size method has priority
    let priority2_template = TemplateBuilder::new("Priority 2")
        .enable_enhancement("prob-4")
        .resolution(1920, 1080) // This will be ignored
        .output_settings(5, 0.0) // QHD size method takes priority
        .custom_resolution_priority(2) // Size method priority
        .build()?;

    let priority0_command = sdk.generate_ffmpeg_command(&priority0_template, "input.mp4", "output_p0.mp4")?;
    let priority2_command = sdk.generate_ffmpeg_command(&priority2_template, "input.mp4", "output_p2.mp4")?;

    println!("   Priority 0 (Custom resolution used):");
    println!("   {}", priority0_command);
    println!("\n   Priority 2 (Size method used - 2560x1440):");
    println!("   {}", priority2_command);

    // Example 6: Aspect Ratio Handling Comparison
    println!("\n6. Aspect Ratio Handling Comparison:");

    // Stretch to fit (ignore aspect ratio)
    let stretch_template = TemplateBuilder::new("Stretch to Fit")
        .enable_enhancement("prob-4")
        .resolution(1920, 1080)
        .lock_aspect_ratio(false) // Allow stretching
        .build()?;

    // Crop to fit (maintain aspect ratio, crop excess)
    let crop_template = TemplateBuilder::new("Crop to Fit")
        .enable_enhancement("prob-4")
        .resolution(1920, 1080)
        .lock_aspect_ratio(true)
        .crop_to_fit(true) // Crop excess
        .build()?;

    // Pad to fit (maintain aspect ratio, add padding)
    let pad_template = TemplateBuilder::new("Pad to Fit")
        .enable_enhancement("prob-4")
        .resolution(1920, 1080)
        .lock_aspect_ratio(true)
        .crop_to_fit(false) // Add padding
        .build()?;

    let stretch_command = sdk.generate_ffmpeg_command(&stretch_template, "input.mp4", "output_stretch.mp4")?;
    let crop_command = sdk.generate_ffmpeg_command(&crop_template, "input.mp4", "output_crop.mp4")?;
    let pad_command = sdk.generate_ffmpeg_command(&pad_template, "input.mp4", "output_pad.mp4")?;

    println!("   Stretch to fit (ignore aspect ratio):");
    println!("   {}", stretch_command);
    println!("\n   Crop to fit (maintain aspect ratio, crop):");
    println!("   {}", crop_command);
    println!("\n   Pad to fit (maintain aspect ratio, pad):");
    println!("   {}", pad_command);

    println!("\n✅ Output settings demo completed successfully!");
    println!("\nKey Features Demonstrated:");
    println!("  • Output settings active/inactive control");
    println!("  • Custom resolution priority handling");
    println!("  • Aspect ratio locking and preservation");
    println!("  • Crop vs pad behavior for aspect ratio mismatches");
    println!("  • Pixel aspect ratio configuration");
    println!("  • Resolution priority vs size method priority");

    Ok(())
}
