use forge_foundation::ZoneType;
use super::EffectContext;
use crate::ability::ability_ir::DefinedRef;
use crate::card::card_util;
use crate::spellability::SpellAbility;
pub fn get_protection_list(sa: &SpellAbility) -> Vec<String> {
let gains = sa.ir.gains.as_deref().unwrap_or("");
if gains != "Choice" && !gains.contains("chosen color") {
return gains
.split(',')
.map(|c| c.trim().to_string())
.filter(|s| !s.is_empty())
.collect();
}
let mut out = Vec::new();
let choices = sa.ir.choices.as_deref().unwrap_or("");
let mut choices_mut = choices.to_string();
if choices_mut.contains("AnyColor") {
out.extend(
["White", "Blue", "Black", "Red", "Green"]
.iter()
.map(|s| s.to_string()),
);
choices_mut = choices_mut.replace("AnyColor,", "").replace("AnyColor", "");
}
let trimmed = choices_mut.trim().trim_end_matches(',');
if !trimmed.is_empty() {
out.extend(
trimmed
.split(',')
.map(|c| c.trim().to_string())
.filter(|s| !s.is_empty()),
);
}
if out.is_empty() {
out = ["White", "Blue", "Black", "Red", "Green"]
.iter()
.map(|s| s.to_string())
.collect();
}
out
}
pub fn run(game: &mut crate::game::GameState, card_id: crate::ids::CardId, keyword: &str) {
if game.card(card_id).zone == ZoneType::Battlefield {
game.card_mut(card_id).pump_keywords.remove(keyword);
}
}
#[manabrew_engine_macros::spell_effect(ProtectEffect)]
fn resolve(ctx: &mut EffectContext, sa: &crate::spellability::SpellAbility) {
let controller = sa.activating_player;
let target = sa
.target_chosen
.target_card
.or_else(|| match sa.defined_ref() {
Some(DefinedRef::SelfCard) => sa.source,
Some(DefinedRef::ParentTarget) => ctx.parent_target_card,
_ => sa.source,
});
let gains = sa.ir.gains.clone().unwrap_or_default();
let is_choice = gains.contains("Choice") || gains.contains("chosen color");
let mut targets = target.into_iter().collect::<Vec<_>>();
targets.extend(card_util::get_radiance(ctx.game, sa).iter().copied());
targets.retain(|&id| ctx.game.card(id).zone == ZoneType::Battlefield);
targets.sort_unstable_by_key(|cid| cid.0);
targets.dedup();
if targets.is_empty() {
return;
}
if is_choice {
let choices = get_protection_list(sa);
let chosen = ctx.agents[controller.index()].choose_color(controller, &choices);
if let Some(color) = chosen {
let prot_kw = format!("Protection from {}", color.to_lowercase());
for card_id in targets {
ctx.game.card_mut(card_id).add_pump_keyword(&prot_kw);
}
}
} else {
for card_id in targets {
ctx.game.card_mut(card_id).add_pump_keyword(&gains);
}
}
}