namespace PkmnLib.Plugin.Gen7.Scripts.Status;

[Script(ScriptCategory.Status, "frozen")]
public class Frozen : Script, IScriptPreventMove, IScriptOnEndTurn
{
    private IPokemon? _pokemon;

    /// <inheritdoc />
    public override void OnAddedToParent(IScriptSource source)
    {
        _pokemon = source as IPokemon;
        if (_pokemon == null)
        {
            throw new InvalidOperationException("Frozen script can only be added to a Pokemon.");
        }
    }

    /// <inheritdoc />
    public void PreventMove(IExecutingMove move, ref bool prevent)
    {
        if (move.UseMove.MoveType.Name == "fire" || move.UseMove.HasFlag(MoveFlags.Defrost))
        {
            _pokemon?.ClearStatus();
            return;
        }

        prevent = true;
    }

    /// <inheritdoc />
    public void OnEndTurn(IScriptSource owner, IBattle battle)
    {
        if (_pokemon == null)
            return;
        if (battle.Random.GetInt(0, 100) >= 20)
            return;

        _pokemon.ClearStatus();
    }

    /// <inheritdoc />
    public override void OnRemove()
    {
        _pokemon?.BattleData?.Battle.EventHook.Invoke(new DialogEvent("pokemon_thawed_out",
            new Dictionary<string, object>
            {
                { "pokemon", _pokemon },
            }));
    }
}