using PkmnLib.Plugin.Gen7.Scripts.Utils;

namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "nature_power")]
public class NaturePower : Script, IScriptChangeMove
{
    /// <inheritdoc />
    public void ChangeMove(IMoveChoice choice, ref StringKey moveName)
    {
        var battleData = choice.User.BattleData;
        if (battleData is null)
            return;
        var newMoveName = GetMoveName(battleData.Battle);
        if (newMoveName is null)
        {
            choice.Fail();
            return;
        }
        moveName = newMoveName.Value;
    }

    private static StringKey? GetMoveName(IBattle battle)
    {
        var movesLibrary = battle.Library.StaticLibrary.Moves;
        var environmentCategory = battle.GetEnvironmentCategory();
        var moveName = environmentCategory switch
        {
            EnvironmentHelper.EnvironmentCategory.Electric when movesLibrary.TryGet("thunderbolt",
                out var thunderboltMove) => (StringKey?)thunderboltMove.Name,
            EnvironmentHelper.EnvironmentCategory.Fairy when movesLibrary.TryGet("moonblast", out var moonblastMove) =>
                moonblastMove.Name,
            EnvironmentHelper.EnvironmentCategory.Grass when movesLibrary.TryGet("energy_ball", out var energyballMove)
                => energyballMove.Name,
            EnvironmentHelper.EnvironmentCategory.Psychic when movesLibrary.TryGet("psychic", out var psychicMove) =>
                psychicMove.Name,
            EnvironmentHelper.EnvironmentCategory.Rock when movesLibrary.TryGet("power_gem", out var rockMove) =>
                rockMove.Name,
            EnvironmentHelper.EnvironmentCategory.Ground when movesLibrary.TryGet("earth_power", out var groundMove) =>
                groundMove.Name,
            EnvironmentHelper.EnvironmentCategory.Ice when movesLibrary.TryGet("ice_beam", out var iceMove) => iceMove
                .Name,
            EnvironmentHelper.EnvironmentCategory.Water when movesLibrary.TryGet("hydro_pump", out var waterMove) =>
                waterMove.Name,
            EnvironmentHelper.EnvironmentCategory.Normal when movesLibrary.TryGet("tri_attack", out var normalMove) =>
                normalMove.Name,
            _ => null,
        };
        return moveName;
    }
}