use gled_model::asset::midi_controller::{MidiControllerMapping, MidiOutputBindingKind};
use super::build::{Rule, build_rules};
use super::controls::Control;
use super::feedback::{FeedbackRole, feedback_role};
use super::sentence::{Plain, action_sentence, source_sentence, text};
pub(crate) fn name_rule(mapping: &mut MidiControllerMapping, controls: &[Control], rule: &Rule) {
let control = &controls[rule.control];
for (index, suffix) in [(rule.press, ""), (rule.release, " release")] {
let Some(index) = index else {
continue;
};
let sentence = action_sentence(
&mapping.input_bindings[index].action,
&mapping.modes,
&Plain,
);
mapping.input_bindings[index].name =
format!("{}{suffix}: {}", control.name, text(&sentence));
}
for &index in &rule.feedback {
let role = feedback_role(controls, rule, &mapping.output_bindings[index].kind);
let shows = match &mapping.output_bindings[index].kind {
MidiOutputBindingKind::SceneColorValue(_) => "scene color".to_owned(),
kind => kind
.source()
.map(|source| text(&source_sentence(source, &mapping.modes, &Plain)))
.unwrap_or_default(),
};
mapping.output_bindings[index].name =
format!("{} {}: {shows}", control.name, role_word(role));
}
}
fn role_word(role: FeedbackRole) -> &'static str {
match role {
FeedbackRole::Light => "light",
FeedbackRole::MotorFader => "motor fader",
FeedbackRole::Meter => "meter",
FeedbackRole::Ring => "ring",
FeedbackRole::PadColor => "pad color",
FeedbackRole::Value => "value",
}
}
pub(crate) fn name_rules_of(
mapping: &mut MidiControllerMapping,
controls: &[Control],
edited: &[(usize, Option<u8>)],
) {
for rule in build_rules(mapping, controls) {
if edited.contains(&(rule.control, rule.mode)) {
name_rule(mapping, controls, &rule);
}
}
}