//! Example of using typedialog as a library
//!
//! Run with: cargo run --example library_example

use typedialog_core::form_parser;
use typedialog_core::prelude::*;
use typedialog_core::prompts;

fn main() -> Result<()> {
    println!("\n=== typedialog Library Examples ===\n");

    // Example 1: Simple text input
    println!("--- Text Input Example ---");
    let name = prompts::text("What is your name?", Some("Alice"), Some("e.g., John Doe"))?;
    println!("You entered: {}\n", name);

    // Example 2: Confirmation
    println!("--- Confirmation Example ---");
    let confirmed = prompts::confirm("Do you want to continue?", Some(true), None)?;
    println!("Confirmed: {}\n", confirmed);

    // Example 3: Selection from list
    println!("--- Selection Example ---");
    let options = vec!["Rust".to_string(), "Python".to_string(), "JavaScript".to_string()];
    let language = prompts::select("Choose your favorite language", options, Some(3), false)?;
    println!("You selected: {}\n", language);

    // Example 4: Multiple selections
    println!("--- Multi-Select Example ---");
    let frameworks = vec![
        "Actix".to_string(),
        "Tokio".to_string(),
        "Rocket".to_string(),
        "Axum".to_string(),
    ];
    let selected = prompts::multi_select(
        "Which frameworks have you used?",
        frameworks,
        Some(2),
        false,
    )?;
    println!("You selected: {:?}\n", selected);

    // Example 5: Password input
    println!("--- Password Example ---");
    let password = prompts::password("Enter a password", true)?;
    println!("Password length: {}\n", password.len());

    // Example 6: Date selection
    println!("--- Date Example ---");
    let date = prompts::date(
        "Select your birth date",
        Some("2000-01-01"),
        Some("1900-01-01"),
        Some("2024-12-31"),
        "Mon",
    )?;
    println!("Selected date: {}\n", date);

    // Example 7: Custom type
    println!("--- Custom Type Example ---");
    let port = prompts::custom("Enter port number", "u16", Some("8080"))?;
    println!("Port: {}\n", port);

    // Example 8: Load and execute form from TOML
    println!("--- Form Example ---");
    match form_parser::load_from_file("examples/registration_form.toml") {
        Ok(form) => {
            println!("Executing form: {}\n", form.name);
            match form_parser::execute(form) {
                Ok(results) => {
                    println!("Form results:");
                    for (key, value) in results {
                        println!("  {}: {}", key, value);
                    }
                }
                Err(e) => eprintln!("Error executing form: {}", e),
            }
        }
        Err(e) => eprintln!("Error loading form: {}", e),
    }

    println!("\n=== Done ===\n");
    Ok(())
}
