use tvai_sdk::{TvaiSdk, TemplateBuilder};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 创建 SDK 实例
    let sdk = TvaiSdk::new();

    println!("=== Topaz Video AI FFmpeg 内置配置演示 ===\n");

    // 显示编码器报告
    println!("{}", sdk.ffmpeg_generator.generate_encoder_report());

    // 创建一个示例模板
    let template = TemplateBuilder::new("内置配置演示")
        .description("使用内置配置的 FFmpeg 命令生成演示")
        .enable_enhancement("prob-4")
        .enhancement_params(25, 30, 20)
        .resolution(1920, 1080)
        .video_codec("H264", Some(23))
        .audio_settings("AAC", 192, 2)
        .build()?;

    // 生成基本 FFmpeg 命令
    println!("=== 基本 FFmpeg 命令 ===");
    let basic_command = sdk.generate_ffmpeg_command(&template, "input.mp4", "output.mp4")?;
    println!("{}\n", basic_command);

    // 演示自动编码器选择
    println!("=== 自动编码器选择 ===");
    
    // Windows + NVIDIA GPU
    match sdk.ffmpeg_generator.generate_with_auto_encoder(&template, "input.mp4", "output_nvidia.mp4", "windows", Some("nvidia")) {
        Ok(command) => println!("Windows + NVIDIA: {}", command),
        Err(e) => println!("Windows + NVIDIA 错误: {}", e),
    }

    // macOS + Apple Silicon
    match sdk.ffmpeg_generator.generate_with_auto_encoder(&template, "input.mp4", "output_apple.mov", "osx", Some("appleSilicon")) {
        Ok(command) => println!("macOS + Apple Silicon: {}", command),
        Err(e) => println!("macOS + Apple Silicon 错误: {}", e),
    }

    // Linux + AMD GPU
    match sdk.ffmpeg_generator.generate_with_auto_encoder(&template, "input.mp4", "output_amd.mp4", "linux", Some("amd")) {
        Ok(command) => println!("Linux + AMD: {}", command),
        Err(e) => println!("Linux + AMD 错误: {}", e),
    }

    println!();

    // 演示模型推荐
    println!("=== 模型推荐 ===");
    let resolutions = [480, 720, 1080, 2160];
    for height in resolutions {
        let recommended = sdk.ffmpeg_generator.recommend_model_for_resolution(height);
        println!("{}p 推荐模型: {:?}", height, recommended);
    }

    println!();

    // 演示内置音频编解码器配置
    println!("=== 内置音频编解码器配置 ===");
    for codec in sdk.ffmpeg_generator.get_available_audio_codecs() {
        println!("- {}: {}", codec.name, codec.ffmpeg_opts);
        if let Some(bitrate_config) = &codec.bitrate {
            println!("  比特率范围: {}-{} kbps (推荐: {} kbps)",
                     bitrate_config.min, bitrate_config.max, bitrate_config.default);
            println!("  建议值: {:?}", bitrate_config.suggested);
        }
    }

    println!();

    // 演示兼容性筛选
    println!("=== 兼容性筛选 ===");
    
    println!("Windows 兼容编码器:");
    let windows_encoders = sdk.ffmpeg_generator.get_compatible_video_encoders("windows", None);
    for encoder in windows_encoders.iter().take(5) { // 只显示前5个
        println!("  - {} ({})", encoder.id, encoder.encoder);
    }

    println!("\nmacOS 兼容编码器:");
    let macos_encoders = sdk.ffmpeg_generator.get_compatible_video_encoders("osx", None);
    for encoder in macos_encoders.iter().take(5) {
        println!("  - {} ({})", encoder.id, encoder.encoder);
    }

    println!("\nMP4 格式兼容编码器:");
    let mp4_encoders = sdk.ffmpeg_generator.get_encoders_for_extension("mp4");
    for encoder in mp4_encoders.iter().take(5) {
        println!("  - {} ({})", encoder.id, encoder.encoder);
    }

    println!();

    // 演示不同质量设置
    println!("=== 不同质量设置演示 ===");
    
    // 高质量设置
    let hq_template = TemplateBuilder::new("高质量")
        .enable_enhancement("prob-4")
        .video_codec("hevc_nvenc", Some(18))
        .audio_settings("AAC", 320, 2)
        .build()?;
    
    let hq_command = sdk.generate_ffmpeg_command(&hq_template, "input.mp4", "output_hq.mp4")?;
    println!("高质量: {}", hq_command);

    // 快速编码设置
    let fast_template = TemplateBuilder::new("快速编码")
        .enable_enhancement("prob-4")
        .video_codec("h264_nvenc", Some(25))
        .audio_settings("AAC", 128, 2)
        .preset("fast")
        .build()?;
    
    let fast_command = sdk.generate_ffmpeg_command(&fast_template, "input.mp4", "output_fast.mp4")?;
    println!("快速编码: {}", fast_command);

    println!("\n=== 演示完成 ===");

    Ok(())
}
