package de.steamwar.misslewars.scripts.utils;

import com.google.gson.JsonObject;

import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;

public class JsonUtils {

    private JsonUtils() {
        throw new IllegalStateException("Utility class");
    }

    public static boolean getBoolean(JsonObject jsonObject, String key, boolean defaultValue) {
        return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsBoolean() : defaultValue;
    }

    public static int getInt(JsonObject jsonObject, String key, int defaultValue) {
        return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsInt() : defaultValue;
    }

    public static float getFloat(JsonObject jsonObject, String key, float defaultValue) {
        return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsFloat() : defaultValue;
    }

    public static String getString(JsonObject jsonObject, String key, String defaultValue) {
        return jsonObject.has(key) ? jsonObject.getAsJsonPrimitive(key).getAsString() : defaultValue;
    }

    public static void getBoolean(JsonObject jsonObject, String key, Consumer<Boolean> booleanConsumer) {
        if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsBoolean());
    }

    public static void getInt(JsonObject jsonObject, String key, IntConsumer booleanConsumer) {
        if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsInt());
    }

    public static void getDouble(JsonObject jsonObject, String key, DoubleConsumer booleanConsumer) {
        if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsDouble());
    }

    public static void getFloat(JsonObject jsonObject, String key, Consumer<Float> booleanConsumer) {
        if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsFloat());
    }

    public static void getString(JsonObject jsonObject, String key, Consumer<String> booleanConsumer) {
        if (jsonObject.has(key)) booleanConsumer.accept(jsonObject.getAsJsonPrimitive(key).getAsString());
    }

}
