use pdfmake_rust::{
    Alignment, Document, DocumentNode, Margins, PageSize, PdfMake, Style, TableCell, TableNode,
    TableStyle, TableWidth, TextNode,
};
use std::fs;

fn main() {
    let doc = Document::builder()
        .page_size(PageSize::a4())
        .page_margins(Margins::all(40.0))
        .content(DocumentNode::Text(
            TextNode::new("Table Examples").inline_style(Style::new().font_size(20.0).bold(true)),
        ))
        .content(DocumentNode::text("Simple Table:"))
        .content(create_simple_table())
        .content(DocumentNode::text("Styled Table:"))
        .content(create_styled_table())
        .content(DocumentNode::text("Table with Column Widths:"))
        .content(create_widths_table())
        .content(DocumentNode::text("Table with Col/Row Span:"))
        .content(create_span_table())
        .build();

    let pdf = PdfMake::new();
    let bytes = pdf.render(&doc).expect("Failed to render PDF");

    fs::write("examples/pdfs/table.pdf", &bytes).expect("Failed to write PDF");
    println!("Generated examples/pdfs/table.pdf ({} bytes)", bytes.len());
}

fn create_simple_table() -> DocumentNode {
    let body = vec![
        vec![
            TableCell::text("Name"),
            TableCell::text("Age"),
            TableCell::text("City"),
        ],
        vec![
            TableCell::text("Alice"),
            TableCell::text("30"),
            TableCell::text("New York"),
        ],
        vec![
            TableCell::text("Bob"),
            TableCell::text("25"),
            TableCell::text("San Francisco"),
        ],
        vec![
            TableCell::text("Charlie"),
            TableCell::text("35"),
            TableCell::text("Chicago"),
        ],
    ];

    DocumentNode::table(TableNode::new(body))
}

fn create_styled_table() -> DocumentNode {
    let header_style = Style::new().bold(true).fill_color("#eeeeee");
    let body = vec![
        vec![
            TableCell::text("Header 1").style(header_style.clone()),
            TableCell::text("Header 2").style(header_style.clone()),
            TableCell::text("Header 3").style(header_style),
        ],
        vec![
            TableCell::text("Cell 1"),
            TableCell::text("Cell 2"),
            TableCell::text("Cell 3"),
        ],
        vec![
            TableCell::text("Cell 4").fill_color("#e6f3ff"),
            TableCell::text("Cell 5").fill_color("#e6f3ff"),
            TableCell::text("Cell 6").fill_color("#e6f3ff"),
        ],
    ];

    DocumentNode::table(
        TableNode::new(body)
            .table_style(TableStyle::CrossLines)
            .header_rows(1),
    )
}

fn create_widths_table() -> DocumentNode {
    let body = vec![
        vec![
            TableCell::text("ID").style(Style::new().bold(true)),
            TableCell::text("Product").style(Style::new().bold(true)),
            TableCell::text("Price").style(Style::new().bold(true)),
        ],
        vec![
            TableCell::text("1"),
            TableCell::text("Widget A"),
            TableCell::text("$10.00"),
        ],
        vec![
            TableCell::text("2"),
            TableCell::text("Widget B"),
            TableCell::text("$25.50"),
        ],
    ];

    DocumentNode::table(
        TableNode::new(body).widths(vec![
            TableWidth::Fixed(40.0),
            TableWidth::Star,
            TableWidth::Fixed(80.0),
        ]),
    )
}

fn create_span_table() -> DocumentNode {
    let body = vec![
        vec![
            TableCell::text("Merged Header")
                .col_span(2)
                .style(Style::new().bold(true).alignment(Alignment::Center)),
            TableCell::text("Value").style(Style::new().bold(true)),
        ],
        vec![
            TableCell::text("Tall Cell").row_span(2).fill_color("#fff0f0"),
            TableCell::text("A"),
            TableCell::text("100"),
        ],
        vec![TableCell::text("B"), TableCell::text("200")],
    ];

    DocumentNode::table(TableNode::new(body).table_style(TableStyle::CrossLines))
}
