using PkmnLib.Static.Moves;

namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "present")]
public class Present : Script, IScriptOnSecondaryEffect, IScriptChangeBasePower, IScriptChangeCategory,
    IScriptOnBeforeHit
{
    private bool _isHealing;
    private byte _basePower;

    /// <inheritdoc />
    public void OnBeforeHit(IExecutingMove move, IPokemon target, byte hitIndex)
    {
        var battleRandom = move.User.BattleData?.Battle.Random;
        if (battleRandom == null)
            return;
        var percentRoll = battleRandom.GetFloat() * 100.0;
        if (percentRoll < 20.0)
        {
            _isHealing = true;
            _basePower = 0;
        }
        else
        {
            _isHealing = false;
            _basePower = percentRoll switch
            {
                < 30 => 120,
                < 60 => 80,
                _ => 40,
            };
        }
    }

    /// <inheritdoc />
    public void ChangeCategory(IExecutingMove move, IPokemon target, byte hitIndex, ref MoveCategory category)
    {
        if (_isHealing)
            category = MoveCategory.Status;
    }

    /// <inheritdoc />
    public void ChangeBasePower(IExecutingMove move, IPokemon target, byte hit, ref ushort basePower)
    {
        basePower = _basePower;
    }

    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        if (!_isHealing)
            return;

        var healAmount = (ushort)(target.MaxHealth / 4);
        target.Heal(healAmount);
    }
}