/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

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());
    }

}
