namespace PkmnLib.Plugin.Gen7.Scripts.Pokemon;

[Script(ScriptCategory.Pokemon, "bind")]
public class BindEffect : Script, IScriptOnEndTurn, IScriptPreventSelfSwitch, IScriptPreventSelfRunAway,
    IAIInfoScriptExpectedEndOfTurnDamage
{
    private readonly IPokemon? _owner;
    private int _turns;
    private readonly float _percentOfMaxHealth;

    public BindEffect(IPokemon owner, int turns, float percentOfMaxHealth)
    {
        _owner = owner;
        _turns = turns;
        _percentOfMaxHealth = percentOfMaxHealth;
    }

    /// <inheritdoc />
    public void OnEndTurn(IScriptSource owner, IBattle battle)
    {
        if (_owner == null)
            return;
        if (_turns > 0)
        {
            _turns--;
            _owner.Damage((uint)(_owner.MaxHealth * _percentOfMaxHealth), DamageSource.Misc);
        }
        if (_turns <= 0)
            RemoveSelf();
    }

    /// <inheritdoc />
    public void PreventSelfSwitch(ISwitchChoice choice, ref bool prevent) => prevent = _turns > 0;

    /// <inheritdoc />
    public void PreventSelfRunAway(IFleeChoice choice, ref bool prevent) => prevent = _turns > 0;

    /// <inheritdoc />
    public void ExpectedEndOfTurnDamage(IPokemon pokemon, ref int damage)
    {
        damage += (int)(_owner?.MaxHealth * _percentOfMaxHealth ?? 0);
    }
}