namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "freeze_dry")]
public class FreezeDry : Script, IScriptChangeEffectiveness, IScriptOnSecondaryEffect
{
    /// <inheritdoc />
    public void ChangeEffectiveness(IExecutingMove move, IPokemon target, byte hit, ref float effectiveness)
    {
        var battleData = target.BattleData;
        if (battleData == null)
            return;

        var typeLibrary = battleData.Battle.Library.StaticLibrary.Types;
        if (!typeLibrary.TryGetTypeIdentifier("water", out var waterType))
            return;

        if (target.Types.Contains(waterType))
        {
            var effectivenessWithoutWater = target.Types.Where(x => x != waterType)
                .Select(x => typeLibrary.GetEffectiveness(x, target.Types)).Aggregate(1f, (a, b) => a * b);
            effectiveness = effectivenessWithoutWater * 2;
        }
    }

    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        var battleData = target.BattleData;
        if (battleData == null)
            return;

        if (battleData.Battle.Random.EffectChance(10, move, target, hit))
        {
            target.SetStatus("frozen", move.User);
        }
    }
}