using PkmnLib.Static.Moves;

namespace PkmnLib.Plugin.Gen7.Scripts.Side;

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

    /// <inheritdoc />
    public void ChangeIncomingMoveDamage(IExecutingMove move, IPokemon target, byte hit, ref uint damage)
    {
        var hitData = move.GetHitData(target, hit);
        if (move.UseMove.Category != MoveCategory.Physical)
            return;
        if (hitData.IsCritical)
            return;
        switch (move.Battle.PositionsPerSide)
        {
            case 1:
                damage /= 2;
                break;
            default:
                damage *= 2 / 3;
                break;
        }
    }

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

        RemoveSelf();
    }
}