using PkmnLib.Plugin.Gen7.Scripts.Utils;

namespace PkmnLib.Plugin.Gen7.Scripts.Moves;

[Script(ScriptCategory.Move, "camouflage")]
public class Camouflage : Script, IScriptOnSecondaryEffect
{
    /// <inheritdoc />
    public void OnSecondaryEffect(IExecutingMove move, IPokemon target, byte hit)
    {
        var type = GetTypeIdentifier(move.Battle);
        if (type == null)
            return;
        move.User.SetTypes([type.Value]);
    }

    private static TypeIdentifier? GetTypeIdentifier(IBattle battle)
    {
        var typesLibrary = battle.Library.StaticLibrary.Types;
        var environmentCategory = battle.GetEnvironmentCategory();
        return environmentCategory switch
        {
            EnvironmentHelper.EnvironmentCategory.Electric when typesLibrary.TryGetTypeIdentifier("electric",
                out var electricType) => electricType,
            EnvironmentHelper.EnvironmentCategory.Fairy when typesLibrary.TryGetTypeIdentifier("fairy",
                out var fairyType) => fairyType,
            EnvironmentHelper.EnvironmentCategory.Grass when typesLibrary.TryGetTypeIdentifier("grass",
                out var grassType) => grassType,
            EnvironmentHelper.EnvironmentCategory.Psychic when typesLibrary.TryGetTypeIdentifier("psychic",
                out var psychicType) => psychicType,
            EnvironmentHelper.EnvironmentCategory.Rock when typesLibrary.TryGetTypeIdentifier("rock", out var rockType)
                => rockType,
            EnvironmentHelper.EnvironmentCategory.Ground when typesLibrary.TryGetTypeIdentifier("ground",
                out var groundType) => groundType,
            EnvironmentHelper.EnvironmentCategory.Ice when typesLibrary.TryGetTypeIdentifier("ice", out var iceType) =>
                iceType,
            EnvironmentHelper.EnvironmentCategory.Water when typesLibrary.TryGetTypeIdentifier("water",
                out var waterType) => waterType,
            EnvironmentHelper.EnvironmentCategory.Normal when typesLibrary.TryGetTypeIdentifier("normal",
                out var normalType) => normalType,
            _ => null,
        };
    }
}