using System.Linq;
using PkmnLib.Static;
using PkmnLib.Static.Libraries;

namespace PkmnLib.Plugin.Gen7.Scripts.Pokemon;

[Script(ScriptCategory.Pokemon, "foresight")]
public class ForesightEffect : Script
{
    private readonly IReadOnlyTypeLibrary _typeLibrary;
    private readonly TypeIdentifier _normalType;
    private readonly TypeIdentifier _fightingType;
    private readonly TypeIdentifier _ghostType;

    public ForesightEffect(IReadOnlyTypeLibrary typeLibrary)
    {
        _typeLibrary = typeLibrary;
        typeLibrary.TryGetTypeIdentifier("normal", out _normalType);
        typeLibrary.TryGetTypeIdentifier("fighting", out _fightingType);
        typeLibrary.TryGetTypeIdentifier("ghost", out _ghostType);
    }

    /// <inheritdoc />
    public override void PreventStatBoostChange(IPokemon target, Statistic stat, sbyte amount, bool selfInflicted,
        ref bool prevent)
    {
        if (stat == Statistic.Evasion)
            prevent = true;
    }

    /// <inheritdoc />
    public override void ChangeEffectiveness(IExecutingMove move, IPokemon target, byte hit, ref float effectiveness)
    {
        var hitData = move.GetHitData(target, hit);
        if (hitData.Type == _normalType && target.Types.Contains(_fightingType))
            effectiveness = _typeLibrary.GetEffectiveness(_normalType, target.Types.Where(x => x != _ghostType));
        else if (hitData.Type == _fightingType && target.Types.Contains(_ghostType))
            effectiveness = _typeLibrary.GetEffectiveness(_fightingType, target.Types.Where(x => x != _ghostType));
    }
}