use super::{CrownShift, RetargetError, RetargetMode, RetargetProposal, RetargetRow};
use crate::{
material::{
design_material_index_from_name, design_material_options, parse_design_material_form,
},
material_lookup::{EditorMaterialLookup, resolved_gem_material},
};
use indicatrix::{geometry::meet_solver::Block, optics::materials::GemMaterial};
use indicatrix_cut_core::{Design, MaterialSelection, ResolvedMaterial, Risk};
#[derive(Debug, Clone)]
pub struct RetargetRowView {
pub tier_index: usize,
pub block: &'static str,
pub name: String,
pub old_angle: String,
pub new_angle: String,
pub margin: String,
pub risk_label: &'static str,
pub risk_rgb: (u8, u8, u8),
}
#[derive(Debug, Clone, Default)]
pub struct RetargetView {
pub rows: Vec<RetargetRowView>,
pub notes: Vec<String>,
pub anchored_errors: Vec<String>,
pub solve_error: String,
}
#[must_use]
pub const fn risk_label_and_rgb(risk: Risk) -> (&'static str, (u8, u8, u8)) {
match risk {
Risk::Safe => ("Safe", (0x10, 0xb9, 0x81)),
Risk::Marginal => ("Marginal", (0xf5, 0x9e, 0x0b)),
Risk::Windows => ("Windows", (0xf4, 0x3f, 0x5e)),
}
}
#[must_use]
pub fn row_view(row: &RetargetRow) -> RetargetRowView {
let block = match row.block {
Block::Crown => "Crown",
Block::Pavilion => "Pavilion",
Block::Girdle => "Girdle",
};
let (risk_label, risk_rgb) = risk_label_and_rgb(row.risk);
RetargetRowView {
tier_index: row.tier_index,
block,
name: row.name.clone(),
old_angle: format!("{:.2}\u{b0}", row.old_angle),
new_angle: format!("{:.2}\u{b0}", row.new_angle),
margin: format!("{:+.2}\u{b0}", row.margin_deg),
risk_label,
risk_rgb,
}
}
#[must_use]
pub fn retarget_view(
design: &Design,
target: &ResolvedMaterial,
crown: CrownShift,
mode: RetargetMode,
custom_materials: &[GemMaterial],
) -> (RetargetView, Option<RetargetProposal>) {
match super::build_proposal(design, target, crown, mode, custom_materials) {
Ok(proposal) => {
let rows = proposal.rows.iter().map(row_view).collect();
let view = RetargetView {
rows,
notes: proposal.notes.clone(),
anchored_errors: Vec::new(),
solve_error: String::new(),
};
(view, Some(proposal))
}
Err(RetargetError::AnchoredTiers(tiers)) => {
let anchored_errors = tiers
.iter()
.map(|(index, name)| format!("#{index} \"{name}\""))
.collect();
let view = RetargetView {
rows: Vec::new(),
notes: Vec::new(),
anchored_errors,
solve_error: String::new(),
};
(view, None)
}
Err(RetargetError::Solve(err)) => {
let view = RetargetView {
rows: Vec::new(),
notes: Vec::new(),
anchored_errors: Vec::new(),
solve_error: err.to_string(),
};
(view, None)
}
}
}
pub fn resolve_target_selection(
design: &Design,
custom: &[GemMaterial],
combo_index: i32,
ri_override_text: &str,
) -> Result<MaterialSelection, String> {
let options = design_material_options(custom);
parse_design_material_form(combo_index, ri_override_text, &options, &design.material)
}
#[must_use]
pub fn target_material_selection(
design: &Design,
custom: &[GemMaterial],
combo_index: i32,
ri_override_text: &str,
) -> MaterialSelection {
resolve_target_selection(design, custom, combo_index, ri_override_text)
.unwrap_or_else(|_| design.material.clone())
}
#[must_use]
pub fn resolved_material_from_selection(
selection: &MaterialSelection,
custom: &[GemMaterial],
) -> ResolvedMaterial {
let lookup = EditorMaterialLookup::new(custom);
let resolved = selection.resolve(&lookup);
let gem = resolved_gem_material(selection, &lookup);
ResolvedMaterial { gem, ..resolved }
}
#[must_use]
pub fn target_display_name(selection: &MaterialSelection) -> String {
selection.name.clone().unwrap_or_else(|| {
if selection.refractive_index_override.is_some() {
"Custom RI".to_string()
} else {
"(none)".to_string()
}
})
}
#[must_use]
pub fn initial_target_index(material: &MaterialSelection, options: &[String]) -> i32 {
if material.name.is_none() && material.refractive_index_override.is_some() {
return i32::try_from(options.len()).unwrap_or(i32::MAX) - 1;
}
design_material_index_from_name(material.name.as_deref(), options)
}