use forge_foundation::ZoneType;
use super::EffectContext;
use crate::ability::ability_ir::DefinedRef;
#[manabrew_engine_macros::spell_effect(PreventDamageEffect)]
fn resolve(ctx: &mut EffectContext, sa: &crate::spellability::SpellAbility) {
let amount = sa
.ir
.amount
.as_deref()
.map(|raw| super::resolve_numeric_value(ctx.game, sa, raw, 1))
.unwrap_or(1);
if amount <= 0 {
return;
}
if let Some(target) = sa.target_chosen.target_card {
if ctx.game.card(target).zone == ZoneType::Battlefield {
ctx.game.card_mut(target).damage_prevention += amount;
}
return;
}
if let Some(pid) = sa.target_chosen.target_player {
ctx.game.player_add_damage_prevention(pid, amount);
return;
}
match sa
.ir
.defined
.as_ref()
.and_then(|defined| defined.refs.first())
{
Some(DefinedRef::SelfCard) => {
if let Some(source) = sa.source {
if ctx.game.card(source).zone == ZoneType::Battlefield {
ctx.game.card_mut(source).damage_prevention += amount;
}
}
}
Some(DefinedRef::ParentTarget) => {
if let Some(parent_target) = ctx.parent_target_card {
if ctx.game.card(parent_target).zone == ZoneType::Battlefield {
ctx.game.card_mut(parent_target).damage_prevention += amount;
}
}
}
Some(DefinedRef::You) => {
let controller = sa.activating_player;
ctx.game.player_add_damage_prevention(controller, amount);
}
Some(DefinedRef::Opponent) => {
let opp = ctx.game.opponent_of(sa.activating_player);
ctx.game.player_add_damage_prevention(opp, amount);
}
_ => {
if let Some(source) = sa.source {
if ctx.game.card(source).zone == ZoneType::Battlefield {
ctx.game.card_mut(source).damage_prevention += amount;
}
}
}
}
}