package com.r3944realms.leashedplayer.content.gamerules;

import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.flag.FeatureFlagSet;
import net.minecraft.world.level.Level;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

public enum GameruleRegistry {
    INSTANCE;
    public static final Map<String, net.minecraft.world.level.GameRules.Key<?>> gamerules = new HashMap<>();;
    public static final Map<String, RuleDataType> gameruleDataTypes = new HashMap<>();
    public enum RuleDataType {
        BOOLEAN,
        INTEGER,
    }
    @SuppressWarnings("unchecked")

    public static boolean getGameruleBoolValue(Level level, String gameruleName) {
        if (level.isClientSide && GameRules.gamerulesBooleanValuesClient.containsKey(gameruleName)) {
            return GameRules.gamerulesBooleanValuesClient.get(gameruleName);
        }
        if (gameruleDataTypes.get(gameruleName) != RuleDataType.BOOLEAN) {
            return false;
        }
        return ((ServerLevel)level).getGameRules().getBoolean((net.minecraft.world.level.GameRules.Key<net.minecraft.world.level.GameRules.BooleanValue>) gamerules.get(gameruleName));
    }
    @SuppressWarnings("unchecked")
    public static Integer getGameruleIntValue(Level level, String gameruleName) {
        if (level.isClientSide && GameRules.gameruleIntegerValuesClient.containsKey(gameruleName)) {
            return GameRules.gameruleIntegerValuesClient.get(gameruleName);
        }
        if (gameruleDataTypes.get(gameruleName) != RuleDataType.INTEGER) {
            return 0;
        }
        return ((ServerLevel)level).getGameRules().getInt((net.minecraft.world.level.GameRules.Key<net.minecraft.world.level.GameRules.IntegerValue>)gamerules.get(gameruleName));
    }

    public void registerGamerule(String gameruleName, net.minecraft.world.level.GameRules.Category category, boolean pDefault) {
        registerGamerule(gameruleName, category, pDefault, (s,i)->{});//最后一个仅占位无用
    }
    public void registerGamerule(String gameruleName, net.minecraft.world.level.GameRules.Category category, boolean pDefault, BiConsumer<MinecraftServer, net.minecraft.world.level.GameRules.BooleanValue> pChangeListener) {
        gamerules.put(gameruleName, net.minecraft.world.level.GameRules.register(gameruleName, category, net.minecraft.world.level.GameRules.BooleanValue.create(pDefault, pChangeListener)));
        gameruleDataTypes.put(gameruleName, RuleDataType.BOOLEAN);
    }
    public void registerGamerule(String gameruleName, net.minecraft.world.level.GameRules.Category category, int pDefault) {
        registerGamerule(gameruleName, category, pDefault, (BiConsumer<MinecraftServer, net.minecraft.world.level.GameRules.IntegerValue>) (s, i)->{});//最后一个仅占位无用
    }
    public void registerGamerule(String gameruleName, net.minecraft.world.level.GameRules.Category category, int pDefault, BiConsumer<MinecraftServer, net.minecraft.world.level.GameRules.IntegerValue> pChangeListener) {
        gamerules.put(gameruleName, net.minecraft.world.level.GameRules.register(gameruleName, category, net.minecraft.world.level.GameRules.IntegerValue.create(pDefault, pChangeListener)));
        gameruleDataTypes.put(gameruleName, RuleDataType.INTEGER);
    }
    public void registerGamerule(String gameruleName, net.minecraft.world.level.GameRules.Category category, int pDefault, int pMin, int pMax, BiConsumer<MinecraftServer, net.minecraft.world.level.GameRules.IntegerValue> pChangeListener) {
        gamerules.put(gameruleName, net.minecraft.world.level.GameRules.register(gameruleName, category, net.minecraft.world.level.GameRules.IntegerValue.create(pDefault, pMin, pMax, FeatureFlagSet.of(), pChangeListener)));
        gameruleDataTypes.put(gameruleName, RuleDataType.INTEGER);
    }
    public void registerGamerule(String gameruleName, net.minecraft.world.level.GameRules.Category category, float value) {
        registerGamerule(gameruleName, category, value, (s,i)->{});
    }
    public void registerGamerule(String gameruleName, net.minecraft.world.level.GameRules.Category category, float pDefault, BiConsumer<MinecraftServer, GameRules.FloatValue> pChangeListener) {
        gamerules.put(gameruleName, net.minecraft.world.level.GameRules.register(gameruleName, category, GameRules.FloatValue.create(pDefault, pChangeListener)));
        gameruleDataTypes.put(gameruleName, RuleDataType.INTEGER);
    }
    public void registerGamerule(String gameruleName, net.minecraft.world.level.GameRules.Category category, float pDefault, float pMin, float pMax, BiConsumer<MinecraftServer, GameRules.FloatValue> pChangeListener) {
        gamerules.put(gameruleName, net.minecraft.world.level.GameRules.register(gameruleName, category, GameRules.FloatValue.create(pDefault, pMin, pMax,pChangeListener)));
        gameruleDataTypes.put(gameruleName, RuleDataType.INTEGER);
    }
}
