// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.spawning.assets.spawnsuppression;

import com.hypixel.hytale.assetstore.AssetKeyValidator;
import com.hypixel.hytale.codec.validation.Validator;
import com.hypixel.hytale.codec.validation.Validators;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import com.hypixel.hytale.assetstore.map.IndexedLookupTableAssetMap;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import com.hypixel.hytale.builtin.tagset.config.NPCGroup;
import javax.annotation.Nonnull;
import java.util.Arrays;
import com.hypixel.hytale.assetstore.AssetRegistry;
import com.hypixel.hytale.assetstore.AssetExtraInfo;
import com.hypixel.hytale.assetstore.AssetStore;
import com.hypixel.hytale.codec.validation.ValidatorCache;
import com.hypixel.hytale.assetstore.codec.AssetBuilderCodec;
import com.hypixel.hytale.assetstore.map.IndexedAssetMap;
import com.hypixel.hytale.assetstore.map.JsonAssetWithMap;

public class SpawnSuppression implements JsonAssetWithMap<String, IndexedAssetMap<String, SpawnSuppression>>
{
    public static final AssetBuilderCodec<String, SpawnSuppression> CODEC;
    public static final ValidatorCache<String> VALIDATOR_CACHE;
    private static AssetStore<String, SpawnSuppression, IndexedAssetMap<String, SpawnSuppression>> ASSET_STORE;
    private AssetExtraInfo.Data data;
    protected String id;
    protected double radius;
    protected String[] suppressedGroups;
    protected int[] suppressedGroupIds;
    protected boolean suppressSpawnMarkers;
    
    public static AssetStore<String, SpawnSuppression, IndexedAssetMap<String, SpawnSuppression>> getAssetStore() {
        if (SpawnSuppression.ASSET_STORE == null) {
            SpawnSuppression.ASSET_STORE = AssetRegistry.getAssetStore(SpawnSuppression.class);
        }
        return SpawnSuppression.ASSET_STORE;
    }
    
    public static IndexedAssetMap<String, SpawnSuppression> getAssetMap() {
        return getAssetStore().getAssetMap();
    }
    
    public SpawnSuppression(final String id) {
        this.radius = 10.0;
        this.id = id;
    }
    
    public SpawnSuppression(final String id, final double radius, final String[] suppressedGroups, final int[] suppressedGroupIds, final boolean suppressSpawnMarkers) {
        this.radius = 10.0;
        this.id = id;
        this.radius = radius;
        this.suppressedGroups = suppressedGroups;
        this.suppressedGroupIds = suppressedGroupIds;
        this.suppressSpawnMarkers = suppressSpawnMarkers;
    }
    
    protected SpawnSuppression() {
        this.radius = 10.0;
    }
    
    @Override
    public String getId() {
        return this.id;
    }
    
    public double getRadius() {
        return this.radius;
    }
    
    public int[] getSuppressedGroupIds() {
        return this.suppressedGroupIds;
    }
    
    public boolean isSuppressSpawnMarkers() {
        return this.suppressSpawnMarkers;
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "SpawnSuppression{radius=" + this.radius + ", suppressedGroups=" + Arrays.toString(this.suppressedGroups) + ", suppressSpawnMarkers=" + this.suppressSpawnMarkers;
    }
    
    static {
        CODEC = ((AssetBuilderCodec.Builder)AssetBuilderCodec.builder(SpawnSuppression.class, SpawnSuppression::new, (Codec<String>)Codec.STRING, (t, k) -> t.id = k, t -> t.id, (asset, data) -> asset.data = data, asset -> asset.data).documentation("A configuration designed to prevent certain NPCs spawns within a given region.").appendInherited(new KeyedCodec("SuppressionRadius", Codec.DOUBLE), (suppressor, d) -> suppressor.radius = d, suppressor -> suppressor.radius, (suppressor, parent) -> suppressor.radius = parent.radius).documentation("The radius this spawn suppression should cover. Any chunk which falls even partially within this radius will be affected by the suppression on the x and z axes, but will use exact distance for the y axis. This allows NPCs to continue to spawn in caves below the position or in the skies above, but is slightly more efficient and provides no noticeable differences in world spawns.").addValidator(Validators.greaterThan(0.0)).add().appendInherited(new KeyedCodec("SuppressedGroups", Codec.STRING_ARRAY), (suppressor, s) -> suppressor.suppressedGroups = s, suppressor -> suppressor.suppressedGroups, (suppressor, parent) -> suppressor.suppressedGroups = parent.suppressedGroups).documentation("An array of NPCGroup ids that will be suppressed.").addValidator(NPCGroup.VALIDATOR_CACHE.getArrayValidator()).add().appendInherited(new KeyedCodec("SuppressSpawnMarkers", Codec.BOOLEAN), (suppressor, b) -> suppressor.suppressSpawnMarkers = b, suppressor -> suppressor.suppressSpawnMarkers, (suppressor, parent) -> suppressor.suppressSpawnMarkers = parent.suppressSpawnMarkers).documentation("Whether or not to suppress any spawn markers within the range of this suppression. If set to true, any spawn marker within this range will cease to function while the suppression exists").add().afterDecode(suppressor -> {
            if (suppressor.suppressedGroups != null && suppressor.suppressedGroups.length > 0) {
                final IndexedLookupTableAssetMap<String, NPCGroup> npcGroups = NPCGroup.getAssetMap();
                final IntOpenHashSet set = new IntOpenHashSet();
                for (final String group : suppressor.suppressedGroups) {
                    set.add(npcGroups.getIndex(group));
                }
                suppressor.suppressedGroupIds = set.toIntArray();
            }
            return;
        })).build();
        VALIDATOR_CACHE = new ValidatorCache<String>(new AssetKeyValidator<String>(SpawnSuppression::getAssetStore));
    }
}
