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

use crate::common::VertexIndex;
use crate::isotropic_remesh::FlipStrategy;
use crate::isotropic_remesh::collapse_edges::CollapseCandidate;
use std::fmt::Debug;
use vector_traits::num_traits::AsPrimitive;

impl<S> Debug for CollapseCandidate<S>
where
    S: crate::common::sealed::ScalarType,
    f64: AsPrimitive<S>,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(new_pos) = self.new_pos {
            let v: [S; 3] = new_pos.into();
            write!(f, "MovePos c0p:{:?} {v:?}", self.c0p)
        } else {
            write!(f, "KeepPos c0p:{:?}", self.c0p)
        }
    }
}

impl Debug for VertexIndex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.is_valid() {
            write!(f, "V{}", self.0)
        } else {
            write!(f, "V∅")
        }
    }
}

impl From<usize> for VertexIndex {
    #[inline(always)]
    fn from(index: usize) -> Self {
        Self(index as u32)
    }
}

impl From<VertexIndex> for usize {
    #[inline(always)]
    fn from(index: VertexIndex) -> usize {
        index.0 as usize
    }
}

impl<S: crate::common::sealed::ScalarType> Debug for FlipStrategy<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FlipStrategy::Disabled => write!(f, "Disabled"),
            FlipStrategy::Valence => write!(f, "Valence"),
            FlipStrategy::WeightedQuality { quality_threshold } => {
                write!(f, "WeightedQuality({quality_threshold})")
            }
        }
    }
}
