use super::{ParsedText, TextProblem, list, same, same_all};
use indicatrix_cut_core::Design;
use indicatrix_formats::asc::AscTier;
fn first_free(taken: &[bool], matches: impl Fn(usize) -> bool) -> Option<usize> {
(0..taken.len()).find(|&index| !taken[index] && matches(index))
}
pub(super) fn pair_tiers(base: &[AscTier], edited: &[AscTier]) -> Vec<Option<usize>> {
let mut taken = vec![false; base.len()];
let mut pairs: Vec<Option<usize>> = vec![None; edited.len()];
for (pair, tier) in pairs.iter_mut().zip(edited) {
*pair = first_free(&taken, |i| base[i].name == tier.name);
if let Some(index) = *pair {
taken[index] = true;
}
}
for (pair, tier) in pairs.iter_mut().zip(edited) {
if pair.is_some() {
continue;
}
*pair = first_free(&taken, |i| {
same(base[i].angle_deg, tier.angle_deg) && same_all(&base[i].indices, &tier.indices)
});
if let Some(index) = *pair {
taken[index] = true;
}
}
if base.len() == edited.len() {
for (position, pair) in pairs.iter_mut().enumerate() {
if pair.is_none() && !taken[position] {
taken[position] = true;
*pair = Some(position);
}
}
}
pairs
}
fn concave_tiers_phrase(design: &Design) -> String {
let names: Vec<String> = design
.concave_tiers
.iter()
.enumerate()
.map(|(position, concave)| {
if concave.name.is_empty() {
format!("number {}", position + 1)
} else {
concave.name.clone()
}
})
.collect();
let noun = if names.len() == 1 { "tier" } else { "tiers" };
format!("the concave {noun} {}", list(&names))
}
fn check_concave_gear(
design: &Design,
base: &ParsedText,
edited: &ParsedText,
) -> Result<(), TextProblem> {
let gear = edited.schedule.gear_teeth_abs();
let was = base.schedule.gear_teeth_abs();
if gear == was || design.concave_tiers.is_empty() {
return Ok(());
}
Err(TextProblem::at(
edited.gear_line,
format!(
"The gear cannot change in the text while the design has {}. Concave tooth numbers \
are not in the text, so they cannot follow a new gear: on the {gear}-tooth gear \
they would keep the same numbers and sit at a different angle. Change the gear in \
the design settings instead (that moves them too), or keep {was} teeth.",
concave_tiers_phrase(design)
),
))
}
pub(super) fn check_edited(
design: &Design,
base: &ParsedText,
edited: &ParsedText,
pairs: &[Option<usize>],
) -> Result<(), TextProblem> {
check_concave_gear(design, base, edited)?;
let gear = edited.schedule.gear_teeth_abs();
let gear_changed = gear != base.schedule.gear_teeth_abs();
for (position, (tier, pair)) in edited.schedule.tiers.iter().zip(pairs).enumerate() {
let line = edited.tier_line(position);
let before = pair.map(|index| &base.schedule.tiers[index]);
let angle_is_new = before.is_none_or(|b| !same(b.angle_deg, tier.angle_deg));
if angle_is_new && !(-90.0..=90.0).contains(&tier.angle_deg) {
return Err(TextProblem::at(
line,
format!(
"The angle {} is outside the range -90 to 90 degrees.",
tier.angle_deg
),
));
}
let indices_are_new =
gear_changed || before.is_none_or(|b| !same_all(&b.indices, &tier.indices));
if indices_are_new
&& let Some(bad) = tier
.indices
.iter()
.find(|index| !(0.0..=f64::from(gear)).contains(*index))
{
return Err(TextProblem::at(
line,
format!(
"The index {bad} is not on the {gear}-tooth gear. Indices run from 0 to {gear}."
),
));
}
if let Some(clash) = design.concave_tiers.iter().find(|concave| {
!concave.name.is_empty() && tier.names().contains(&concave.name.as_str())
}) {
return Err(TextProblem::at(
line,
format!(
"The name {} belongs to a concave tier. Pick another name.",
clash.name
),
));
}
}
Ok(())
}