//! Demo application for herolib-web.

use herolib_web::markdown;

fn main() {
    println!("╔══════════════════════════════════════════════════════════════╗");
    println!("║          herolib-web: Markdown to HTML with Bootstrap       ║");
    println!("╚══════════════════════════════════════════════════════════════╝\n");

    // Example 1: Simple Markdown to HTML Page
    demo_simple_markdown();
    
    // Example 2: Complex Markdown Document
    demo_complex_markdown();
    
    // Example 3: Markdown with Code Blocks
    demo_markdown_with_code();
    
    // Example 4: Table of Contents
    demo_markdown_toc();
    
    // Example 5: Markdown Container
    demo_markdown_container();
    
    // Example 6: Blog Post
    demo_blog_post();
}

fn demo_simple_markdown() {
    println!("Example 1: Simple Markdown to HTML Page");
    println!("═══════════════════════════════════════════════════════════════\n");
    
    let markdown = r#"# Welcome to herolib-web

This is a simple **markdown** document that will be converted to a beautiful HTML page with Bootstrap styling.

## Key Features

- Clean HTML output
- Bootstrap 5.3.8 styling
- Mobile responsive
- Professional appearance

Click [here](https://example.com) to learn more!"#;

    let html = markdown::markdown_to_html_page(markdown, "Welcome Page");
    
    println!("Generated HTML page with {} characters", html.len());
    println!("✓ Contains Bootstrap CSS: {}", html.contains("bootstrap.min.css"));
    println!("✓ Contains Bootstrap JS: {}", html.contains("bootstrap.bundle.min.js"));
    println!("✓ Mobile viewport: {}", html.contains("viewport"));
    println!("✓ Proper structure: {}", html.contains("<!DOCTYPE html>"));
    println!("✓ Semantic HTML: {}", html.contains("<h1>"));
    println!("\n");
}

fn demo_complex_markdown() {
    println!("Example 2: Complex Markdown Document");
    println!("═══════════════════════════════════════════════════════════════\n");
    
    let markdown = r#"# Programming Guide

## Introduction

Welcome to our comprehensive guide on web development.

### What You'll Learn

1. HTML basics
2. CSS styling
3. JavaScript interactions
4. Bootstrap framework

## Getting Started

Before you begin, make sure you have:

- A text editor (VS Code, Sublime, etc.)
- A web browser
- Basic computer literacy

### Installation

Follow these steps:

1. Download the tools
2. Install them
3. Verify installation
4. Start coding!

## Best Practices

> "Code is read much more often than it is written." - Guido van Rossum

Always remember:
- Write clean code
- Comment your work
- Test thoroughly
- Ask for help

---

## Conclusion

You now have everything you need to get started!"#;

    let html = markdown::markdown_to_html_page(markdown, "Programming Guide");
    
    // Extract and show some stats
    let headings = markdown::extract_headings(markdown);
    let word_count = markdown::count_words(markdown);
    
    println!("Document Statistics:");
    println!("  Headings found: {}", headings.len());
    println!("  Word count: {}", word_count);
    println!("  HTML size: {} bytes", html.len());
    println!("\nHeadings:");
    for (level, text) in headings {
        println!("  {} - {}", "  ".repeat(level), text);
    }
    println!("\n");
}

fn demo_markdown_with_code() {
    println!("Example 3: Markdown with Code Blocks");
    println!("═══════════════════════════════════════════════════════════════\n");
    
    let markdown = r#"# Rust Tutorial

## Hello World

Here's your first Rust program:

```rust
fn main() {
    println!("Hello, world!");
}
```

## More Examples

### Variables

```rust
let x = 5;
let y: i32 = 10;
println!("{} + {} = {}", x, y, x + y);
```

### Functions

```rust
fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn main() {
    let result = add(5, 3);
    println!("5 + 3 = {}", result);
}
```

## Inline Code

You can also use `inline code` like `let x = 5;` in your text.

---

That's it! You now know the basics of Rust."#;

    let html = markdown::markdown_to_html_page(markdown, "Rust Tutorial");
    
    println!("Code blocks highlighted with Bootstrap styling");
    println!("✓ Contains <pre> tags: {}", html.contains("<pre>"));
    println!("✓ Contains <code> tags: {}", html.contains("<code>"));
    println!("✓ HTML size: {} bytes\n", html.len());
}

fn demo_markdown_toc() {
    println!("Example 4: Table of Contents");
    println!("═══════════════════════════════════════════════════════════════\n");
    
    let markdown = r#"# Full Documentation

## Chapter 1: Getting Started

### 1.1 Installation
### 1.2 Configuration

## Chapter 2: Core Concepts

### 2.1 Architecture
### 2.2 Data Flow
### 2.3 Best Practices

## Chapter 3: Advanced Topics

### 3.1 Performance Optimization
### 3.2 Security Considerations

## Chapter 4: Troubleshooting

### 4.1 Common Issues
### 4.2 FAQ"#;

    let toc = markdown::markdown_to_toc_bootstrap(markdown);
    let headings = markdown::extract_headings(markdown);
    
    println!("Generated Table of Contents:");
    println!("Number of sections: {}", headings.len());
    println!("✓ Bootstrap navbar: {}", toc.contains("navbar"));
    println!("✓ Navigation links: {}", toc.contains("nav-link"));
    println!("✓ Contains h1: Getting Started - {}", headings.iter().any(|(_, t)| t.contains("Getting")));
    println!("\nHeadings found:");
    for (level, text) in &headings {
        println!("  {}└─ {}", "  ".repeat(*level), text);
    }
    println!("\n");
}

fn demo_markdown_container() {
    println!("Example 5: Markdown Container (for embedding)");
    println!("═══════════════════════════════════════════════════════════════\n");
    
    let markdown = r#"# Product Features

## Feature 1: Easy to Use
Simple and intuitive interface designed for everyone.

## Feature 2: Powerful
Built with performance in mind for demanding workloads.

## Feature 3: Reliable
Battle-tested in production environments worldwide.

## Feature 4: Secure
Enterprise-grade security built in."#;

    let html = markdown::markdown_to_html_bootstrap(markdown);
    
    println!("Generated container HTML:");
    println!("✓ Bootstrap container: {}", html.contains("container"));
    println!("✓ Margin classes: {}", html.contains("mt-5"));
    println!("✓ No DOCTYPE: {}", !html.contains("<!DOCTYPE"));
    println!("✓ Ready to embed: {}", html.contains("<div class"));
    println!("✓ Size: {} bytes\n", html.len());
}

fn demo_blog_post() {
    println!("Example 6: Blog Post");
    println!("═══════════════════════════════════════════════════════════════\n");
    
    let blog_markdown = r#"# Building Web Applications with Rust

Published: December 30, 2024

## Introduction

Rust is becoming increasingly popular for web development. In this post, we'll explore why and how to get started.

## Why Rust for Web?

Rust provides several advantages:

1. **Performance** - Compiled to native code, no garbage collection
2. **Safety** - Memory safety without runtime overhead
3. **Concurrency** - Built-in support for async/await
4. **Reliability** - Strong type system catches bugs at compile time

## Getting Started

### Prerequisites

```bash
rustup install stable
cargo --version
```

### Your First Web Server

```rust
use std::net::TcpListener;
use std::io::{Read, Write};

fn main() {
    let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
    
    for stream in listener.incoming() {
        let mut stream = stream.unwrap();
        let mut buffer = [0; 512];
        stream.read(&mut buffer).unwrap();
        
        let response = "HTTP/1.1 200 OK\r\n\r\nHello from Rust!";
        stream.write_all(response.as_bytes()).unwrap();
    }
}
```

## Best Practices

> "Write code as if the next person who reads it is a violent psychopath who knows where you live." - John Woods

Key practices:
- Keep functions focused and small
- Use meaningful variable names
- Document public APIs
- Write tests alongside code
- Handle errors properly

## Conclusion

Rust is an excellent choice for web development. Start learning today!

### Resources

- [The Rust Book](https://doc.rust-lang.org/book/)
- [Rust by Example](https://doc.rust-lang.org/rust-by-example/)
- [Official Crates](https://crates.io/)"#;

    let html = markdown::markdown_to_html_page(
        blog_markdown,
        "Building Web Applications with Rust"
    );
    
    println!("Blog post generated successfully!");
    println!("✓ Title: {}", html.contains("Building Web Applications with Rust"));
    println!("✓ Headings: {}", markdown::extract_headings(blog_markdown).len());
    println!("✓ Word count: {}", markdown::count_words(blog_markdown));
    println!("✓ Code blocks: {}", blog_markdown.matches("```").count() / 2);
    println!("✓ Bootstrap styled: {}", html.contains("bootstrap@5.3.8"));
    println!("✓ HTML size: {} bytes", html.len());
    
    // Save example to file
    std::fs::write("/tmp/blog_post.html", &html).ok();
    println!("\n✓ Example saved to: /tmp/blog_post.html\n");
}
