use boostvoronoi::prelude::*;

type I = i64;

use std::env;
use std::path::Path;

fn main() -> Result<(), BvError> {
    let args: Vec<String> = env::args().collect();

    // Check if exactly one argument was provided (first arg is program name)
    if args.len() != 2 {
        eprintln!("Usage: {} <filename>", args[0]);
        std::process::exit(1);
    }

    let output = {
        println!("Loading file {}", &args[1]);
        let (points, segments) = read_boost_input_file::<I>(Path::new(&args[1]))?;

        println!("Input data:\n{}", points.len());
        for p in points.iter() {
            println!("{} {}", p.x, p.y);
        }
        println!("{}", segments.len());
        for s in segments.iter() {
            println!("{} {} {} {}", s.start.x, s.start.y, s.end.x, s.end.y);
        }
        if true {
            // this will generate some C++ code from the example data
            println!("-------");
            println!("int INPUT_PTS[{}][2] = {{", points.len());
            for p in points.iter() {
                print!("{{{},{}}},", p.x, p.y);
            }
            println!("}};");
            println!("int INPUT_SGS[{}][4] = {{", segments.len());
            for s in segments.iter() {
                print!("{{{},{},{},{}}},", s.start.x, s.start.y, s.end.x, s.end.y);
            }
            println!("}};");
            println!("-------");
        }
        Builder::<I>::default()
            .with_vertices(points.iter())?
            .with_segments(segments.iter())?
            .build()?
    };
    println!("\nResult:");
    for (i, v) in output.vertices().iter().enumerate() {
        println!(
            "vertex #{} contains a point: ({:.12}, {:.12}), incident edge#:{:?}",
            i,
            v.x(),
            v.y(),
            v.get_incident_edge()?
        );
    }

    println!("\ncells:{}", output.cells().len());
    println!("vertices:{}", output.vertices().len());
    println!("edges:{}\n", output.edges().len());

    for (i, e) in output.edges().iter().enumerate() {
        println!("Edge:#{}=>{:?}", i, e);
    }
    Ok(())
}
