// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2025 lacklustr@protonmail.com https://github.com/eadf

use remesh::prelude::*;

#[test]
fn test_f64_flip_edges_1() -> Result<(), RemeshError> {
    let vertices = vec![
        [0.0, 0.0, 0.0],
        [0.0, 0.0, 1.0],
        [0.0, 0.5, 0.0],
        [1.0, 0.0, 0.0],
        [0.5, 0.0, 0.5],
        [0.0, 0.25, 0.5],
        [0.0, 0.125, 0.75],
        [0.0, 0.0, 0.5],
    ];

    let indices = vec![
        0, 7, 6, 2, 3, 0, 5, 4, 2, 1, 7, 4, 5, 2, 0, 5, 6, 4, 2, 4, 3, 6, 5, 0, 6, 1, 4, 0, 3, 4,
        7, 1, 6, 7, 0, 4,
    ];

    let (result_vertices, result_indices) = IsotropicRemesh::<f64, _>::new(&vertices, &indices)?
        .with_target_edge_length(0.3)?
        .with_split_edges(SplitStrategy::Disabled)?
        .with_collapse_edges(CollapseStrategy::Disabled)?
        .without_smooth_weight()?
        .with_flip_edges(FlipStrategy::default_valence())?
        .run(1)?;

    println!(
        "remesh result: {} vertices, {} indices",
        result_vertices.len(),
        result_indices.len()
    );
    println!(
        "result vertices:{result_vertices:?}:{}",
        result_vertices.len()
    );
    println!("result indices:{result_indices:?}:{}", result_indices.len());
    Ok(())
}

#[test]
fn test_flip_edges_2() -> Result<(), RemeshError> {
    let vertices = vec![
        [-1.0, -1.0, -1.0],
        [-1.0, -1.0, 1.0],
        [-1.0, 1.0, -1.0],
        [-1.0, 1.0, 1.0],
        [1.0, -1.0, -1.0],
        [1.0, -1.0, 1.0],
        [1.0, 1.0, -1.0],
        [1.0, 1.0, 1.0],
        [0.3, 0.3, 1.0],
        [-0.3, -0.3, 1.0],
    ];
    let indices = vec![
        1, 5, 9, 0, 1, 2, 2, 3, 6, 6, 7, 4, 4, 5, 0, 2, 6, 0, 2, 1, 3, 7, 6, 3, 5, 4, 7, 1, 0, 5,
        4, 0, 6, 7, 3, 8, 3, 5, 8, 5, 7, 8, 5, 3, 9, 3, 1, 9,
    ];

    let (result_vertices, result_indices) = IsotropicRemesh::<f64, _>::new(&vertices, &indices)?
        .with_target_edge_length(0.5)?
        .with_split_edges(SplitStrategy::Disabled)?
        .with_collapse_edges(CollapseStrategy::Disabled)?
        .without_smooth_weight()?
        .with_flip_edges(FlipStrategy::quality(1.1))?
        .with_crease_angle_threshold(170.0)?
        .run(1)?;

    println!(
        "remesh result: {} vertices, {} indices",
        result_vertices.len(),
        result_indices.len()
    );
    println!(
        "result vertices:{result_vertices:?}:{}",
        result_vertices.len()
    );
    println!("result indices:{result_indices:?}:{}", result_indices.len());
    assert_eq!(result_vertices.len(), 10, "result_vertices.len() mismatch");
    assert_eq!(result_indices.len(), 48, "result_indices.len() mismatch");
    Ok(())
}

#[test]
fn test_flip_edges_3() -> Result<(), RemeshError> {
    let vertices = vec![
        [-1.0, -1.0, -1.0],
        [-1.0, -1.0, 1.0],
        [-1.0, 1.0, -1.0],
        [-1.0, 1.0, 1.0],
        [1.0, -1.0, -1.0],
        [1.0, -1.0, 1.0],
        [1.0, 1.0, -1.0],
        [1.0, 1.0, 1.0],
        [0.3, 0.3, 1.0],
        [-0.3, -0.3, 1.0],
    ];
    let indices = vec![
        1, 5, 9, 0, 1, 2, 2, 3, 6, 6, 7, 4, 4, 5, 0, 2, 6, 0, 2, 1, 3, 7, 6, 3, 5, 4, 7, 1, 0, 5,
        4, 0, 6, 7, 3, 8, 3, 5, 8, 5, 7, 8, 5, 3, 9, 3, 1, 9,
    ];

    let (result_vertices, result_indices) = IsotropicRemesh::<f64, _>::new(&vertices, &indices)?
        .with_target_edge_length(1.2)?
        .with_split_edges(SplitStrategy::Disabled)?
        .with_default_collapse_multiplier()?
        .without_smooth_weight()?
        .with_flip_edges(FlipStrategy::Valence)?
        .with_default_crease_threshold()?
        .with_default_coplanar_threshold()?
        .run(2)?;

    println!(
        "remesh result: {} vertices, {} indices",
        result_vertices.len(),
        result_indices.len()
    );
    println!(
        "result vertices:{result_vertices:?}:{}",
        result_vertices.len()
    );
    println!("result indices:{result_indices:?}:{}", result_indices.len());
    assert_eq!(result_vertices.len(), 10, "result_vertices.len() mismatch");
    assert_eq!(result_indices.len(), 48, "result_indices.len() mismatch");
    Ok(())
}
