use mr_pdf::{Pdf, TableBorderStyle};
use std::fs::File;
use std::io::BufWriter;

fn main() -> std::io::Result<()> {
    let file = File::create("preview/padding_demo.pdf")?;
    let mut pdf = Pdf::new(BufWriter::new(file))?;
    pdf.set_title("Padding and Filler Demo");

    // Demonstration of TextBlock filling (auto-expand)
    pdf.text("Example: Auto-fill with Alignment")
        .size(14.0)
        .bold()
        .margin_bottom(5.0);
    
    // Default Align::Left: Text.......
    pdf.text("Align Left (Default)")
        .fill('.')
        .size(12.0)
        .margin_bottom(10.0);

    // Align::Right: .......Text
    pdf.text("Align Right")
        .align_right()
        .fill('.')
        .size(12.0)
        .margin_bottom(10.0);

    // Align::Center: ...Text...
    pdf.text("Align Center")
        .align_center()
        .fill('.')
        .size(12.0)
        .margin_bottom(30.0);

    // Demonstration in TableCells
    pdf.text("In Table Cells (Smart Measurement & Alignment)")
        .size(16.0)
        .bold()
        .margin_bottom(10.0);

    pdf.table(|t| {
        t.widths(vec![200.0, 200.0]);
        t.header(vec!["Key (Auto filled)", "Value"]);
        t.border(TableBorderStyle::Full);

        t.row_builder(|r| {
            // Fill dots until end of cell
            r.cells.push(mr_pdf::TableCell::text("ID").fill('.'));
            r.cell("99081");
        });
        
        t.row_builder(|r| {
            // Right aligned fill (dots on left)
            r.cells.push(mr_pdf::TableCell::text("NAME").fill('.').align(mr_pdf::Align::Right));
            r.cell("John Wick");
        });

        t.row_builder(|r| {
            // Center aligned fill (dots on both sides)
            r.cells.push(mr_pdf::TableCell::text("TOTAL AMOUNT").fill('_').align(mr_pdf::Align::Center));
            r.cell("$10,000.00");
        });
    })?;

    pdf.advance_cursor(20.0);

    // Demonstration in RichText
    pdf.text("In Rich Text (Flexible Filler Span)")
        .size(16.0)
        .bold()
        .margin_bottom(10.0);

    pdf.rich_text(|r| {
        r.span("Item Name ").bold();
        r.span("").fill('.'); // This span will expand to fill the gap
        r.span(" $125.00");
    }).size(14.0);

    pdf.rich_text(|r| {
        r.span("Right Aligned Filler ").fill('.');
    }).align_right();

    pdf.rich_text(|r| {
        r.span("Centered Filler ").fill('_');
    }).align_center();

    pdf.advance_cursor(10.0);
    pdf.text("Granular Span Alignment (Left Block but Center Span)")
        .size(12.0)
        .bold()
        .margin_bottom(5.0);

    pdf.rich_text(|r| {
        r.span("NAME: ").bold();
        r.span("John Wick").fill('.').align(mr_pdf::Align::Center);
        r.span(" [ADMIN]").color(mr_pdf::Color::Rgb(200, 0, 0));
    });

    pdf.advance_cursor(20.0);
    pdf.text("In Layout (Row & Columns)")
        .size(16.0)
        .bold()
        .margin_bottom(10.0);

    pdf.row(|r| {
        r.col(200.0, |pdf| {
            pdf.rich_text(|rt| {
                rt.span("Left Col ").bold();
                rt.span("").fill('.');
            });
            Ok(())
        });
        r.col(200.0, |pdf| {
            pdf.rich_text(|rt| {
                rt.span("Right Col ").fill('.');
                rt.span(" $50.00").bold();
            }).align_right();
            Ok(())
        });
    })?;

    pdf.finish()?;
    println!("Successfully generated padding_demo.pdf");
    Ok(())
}
