namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "fling")]
public class Fling : Script, IScriptOnSecondaryEffect, IScriptChangeBasePower
{
    /// <inheritdoc />
    public void ChangeBasePower(IExecutingMove move, IPokemon target, byte hit, ref ushort basePower)
    {
        var item = move.User.HeldItem;
        if (item == null)
        {
            move.GetHitData(target, hit).Fail();
            return;
        }

        if (item.Category is ItemCategory.FormChanger or ItemCategory.Pokeball or ItemCategory.Mail
            or ItemCategory.KeyItem or ItemCategory.TmHm)
        {
            move.GetHitData(target, hit).Fail();
            return;
        }
        if (!item.TryGetAdditionalData<byte>("fling_power", out var flingPower))
        {
            move.GetHitData(target, hit).Fail();
            return;
        }

        basePower = flingPower;
    }

    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit) =>
        move.User.RemoveHeldItemForBattle();
}