use argonauts_core::NoteLevel;
use argonauts_core::input::{ModPlaceChange, ModsAction, ViewAction};
use argonauts_core::settings::SettingScope;
use argonauts_core::settings::ui::{ModName, Place};
use super::settings::set_where_it_came_from;
use super::{Ctx, Effect};
pub(crate) fn tab(ctx: &mut Ctx, tab: Place, action: ViewAction) -> anyhow::Result<Vec<Effect>> {
match action {
ViewAction::Open | ViewAction::Print => Ok(Vec::new()),
ViewAction::Pin { scope } => {
let Some(&the_mod) = ctx.settings().ui.place(tab).first() else {
ctx.notice(
NoteLevel::Info,
format!("The {} tab shows no mods to pin", tab.word()),
);
return Ok(Vec::new());
};
let change = ModPlaceChange::Toggle {
place: Place::AboveInput,
};
command(ctx, ModsAction::Change { the_mod, change }, scope)
}
}
}
pub(crate) fn pin_stats(ctx: &mut Ctx, scope: Option<SettingScope>) -> anyhow::Result<Vec<Effect>> {
let change = ModPlaceChange::Toggle {
place: Place::AboveInput,
};
let the_mod = ModName::Stats;
command(ctx, ModsAction::Change { the_mod, change }, scope)
}
pub(crate) fn command(
ctx: &mut Ctx,
action: ModsAction,
scope: Option<SettingScope>,
) -> anyhow::Result<Vec<Effect>> {
let setting = match action {
ModsAction::List | ModsAction::Page { .. } => return Ok(Vec::new()),
ModsAction::Band { shown } => {
let now = ctx.settings().ui.above_input.shown;
(now != shown).then(|| ("ui.above_input.shown".to_owned(), shown.to_string()))
}
ModsAction::Change { the_mod, change } => {
if let ModPlaceChange::Toggle { place } | ModPlaceChange::Show { place } = change
&& !the_mod.fits(place)
{
ctx.notice(
NoteLevel::Info,
format!("{the_mod} can't show {}", place.phrase()),
);
return Ok(Vec::new());
}
change.setting(the_mod, &ctx.settings().ui)
}
};
match setting {
Some((key, value)) => set_where_it_came_from(ctx, key, value, scope),
None => {
ctx.notice(NoteLevel::Info, "Already so; nothing changed");
Ok(Vec::new())
}
}
}
pub(crate) fn opens_mods(action: &ModsAction) -> bool {
matches!(action, ModsAction::List | ModsAction::Page { .. })
}
pub(crate) fn page_asked(action: &ModsAction) -> Option<String> {
match action {
ModsAction::Page { the_mod } => Some(mod_name(*the_mod).to_owned()),
_ => None,
}
}
pub(crate) fn mod_name(the_mod: ModName) -> &'static str {
match the_mod {
ModName::PlanUsage => "plan-usage",
other => other.name(),
}
}