using PkmnLib.Static.Moves;

namespace PkmnLib.Plugin.Gen7.Scripts.Side;

[Script(ScriptCategory.Side, "light_screen")]
public class LightScreenEffect(int turns) : Script, IScriptChangeIncomingMoveDamage, IScriptOnEndTurn
{
    private int _turns = turns;

    /// <inheritdoc />
    public void ChangeIncomingMoveDamage(IExecutingMove move, IPokemon target, byte hit, ref uint damage)
    {
        if (move.UseMove.Category != MoveCategory.Special ||
            // Critical hits are not affected by the barrier
            move.GetHitData(target, hit).IsCritical)
            return;
        var battleData = target.BattleData;
        // If not a 1v1 battle, damage reduction is only 1/3rd.
        if (battleData?.Battle.PositionsPerSide > 1)
            damage = (uint)(damage * (2.0f / 3.0f));
        else
            damage = (uint)(damage * 0.5f);
    }

    /// <inheritdoc />
    public void OnEndTurn(IScriptSource owner, IBattle battle)
    {
        _turns -= 1;
        if (_turns <= 0)
            RemoveSelf();
    }
}