namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "guard_split")]
public class GuardSplit : Script, IScriptOnSecondaryEffect
{
    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        var user = move.User;
        var userStats = user.FlatStats;
        var targetStats = target.FlatStats;

        var userDefense = userStats.GetStatistic(Statistic.Defense);
        var targetDefense = targetStats.GetStatistic(Statistic.Defense);
        var userSpecialDefense = userStats.GetStatistic(Statistic.SpecialDefense);
        var targetSpecialDefense = targetStats.GetStatistic(Statistic.SpecialDefense);

        var newDefense = (userDefense + targetDefense) / 2;
        var newSpecialDefense = (userSpecialDefense + targetSpecialDefense) / 2;

        userStats.SetStatistic(Statistic.Defense, newDefense);
        userStats.SetStatistic(Statistic.SpecialDefense, newSpecialDefense);
        targetStats.SetStatistic(Statistic.Defense, newDefense);
        targetStats.SetStatistic(Statistic.SpecialDefense, newSpecialDefense);
        user.RecalculateBoostedStats();
        target.RecalculateBoostedStats();
    }
}