namespace PkmnLib.Plugin.Gen7.Scripts.Pokemon;

[Script(ScriptCategory.Pokemon, "substitute")]
public class SubstituteEffect(uint health) : Script, IScriptBlockIncomingHit
{
    public uint Health { get; private set; } = health;

    /// <inheritdoc />
    public void BlockIncomingHit(IExecutingMove executingMove, IPokemon target, byte hitIndex, ref bool block)
    {
        if (executingMove.UseMove.HasFlag(MoveFlags.IgnoreSubstitute))
            return;

        var args = new CustomTriggers.BypassSubstituteArgs(executingMove, target, hitIndex, false);
        executingMove.User.RunScriptHook<IScriptCustomTrigger>(x =>
            x.CustomTrigger(CustomTriggers.BypassSubstitute, args));
        if (args.Bypass)
            return;

        block = true;
        var damage = executingMove.GetHitData(target, hitIndex).Damage;
        if (damage >= Health)
        {
            executingMove.Battle.EventHook.Invoke(new DialogEvent("substitute_broken"));
            RemoveSelf();
            return;
        }
        Health -= damage;
    }
}