using PkmnLib.Plugin.Gen7.Scripts.Pokemon;

namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "bide")]
public class Bide : Script, IScriptOnSecondaryEffect
{
    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        var bideEffect = move.User.Volatile.Get<BideEffect>();
        if (bideEffect == null)
        {
            bideEffect = new BideEffect(move.User);
            move.User.Volatile.Add(bideEffect);
        }

        bideEffect.Turns += 1;
        if (bideEffect.Turns < 3)
            return;
        if (bideEffect.DamageTaken == 0)
        {
            move.GetHitData(target, hit).Fail();
        }
        else
        {
            var lastPokemon = bideEffect.HitBy.LastOrDefault(x => x.BattleData?.IsOnBattlefield == true);
            lastPokemon?.Damage(bideEffect.DamageTaken * 2, DamageSource.MoveDamage);
        }

        move.User.Volatile.Remove<BideEffect>();
    }
}