#![allow(missing_docs)]

use glam::DVec2;
use road_parceling::{subdivide_all_with_stats, RoadGraph, SubdivisionParams};

fn rect(g: &mut RoadGraph, x0: f64, y0: f64, w: f64, h: f64) {
    let a = g.add_node(DVec2::new(x0, y0));
    let b = g.add_node(DVec2::new(x0 + w, y0));
    let c = g.add_node(DVec2::new(x0 + w, y0 + h));
    let d = g.add_node(DVec2::new(x0, y0 + h));
    g.add_road(a, b).unwrap();
    g.add_road(b, c).unwrap();
    g.add_road(c, d).unwrap();
    g.add_road(d, a).unwrap();
}

fn main() {
    let mut single = RoadGraph::new();
    rect(&mut single, 0.0, 0.0, 200.0, 100.0);
    single.rebuild_topology().unwrap();
    let params = SubdivisionParams::default();
    // warm
    for _ in 0..5 {
        let _ = subdivide_all_with_stats(&single, &params).unwrap();
    }
    let (_, s) = subdivide_all_with_stats(&single, &params).unwrap();
    println!("single rectangle: {} blocks, {} parcels, total {:.1}us, topo {:.1}us, blocks {:.1}us, sub {:.1}us, {:.0} parcels/s",
        s.block_count, s.parcel_count, s.total.as_secs_f64()*1e6, s.topology_rebuild.as_secs_f64()*1e6,
        s.block_extraction.as_secs_f64()*1e6, s.block_subdivide_total.as_secs_f64()*1e6, s.parcels_per_second());
    println!("  → {:.2} us/parcel", s.time_per_parcel_us());

    // 5x5 grid of small blocks — quickly approaches 25 blocks
    let mut grid = RoadGraph::new();
    for i in 0..5 {
        for j in 0..5 {
            rect(
                &mut grid,
                (i as f64) * 220.0,
                (j as f64) * 120.0,
                200.0,
                100.0,
            );
        }
    }
    grid.rebuild_topology().unwrap();
    for _ in 0..3 {
        let _ = subdivide_all_with_stats(&grid, &params).unwrap();
    }
    let (_, s) = subdivide_all_with_stats(&grid, &params).unwrap();
    println!(
        "5x5 grid (25 disjoint blocks): {} blocks, {} parcels, total {:.1}us, {:.0} parcels/s",
        s.block_count,
        s.parcel_count,
        s.total.as_secs_f64() * 1e6,
        s.parcels_per_second()
    );
    println!("  → {:.2} us/parcel", s.time_per_parcel_us());
}
