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

package com.hypixel.hytale.server.core.modules.blockset;

import it.unimi.dsi.fastutil.objects.ObjectCollection;
import java.util.function.Consumer;
import java.util.Objects;
import com.hypixel.hytale.assetstore.map.BlockTypeAssetMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
import it.unimi.dsi.fastutil.ints.IntSets;
import com.hypixel.hytale.server.core.asset.type.item.config.Item;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;
import java.util.Map;
import javax.annotation.Nonnull;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;

public class BlockSetLookupTable
{
    @Nonnull
    private final Object2ObjectMap<String, IntSet> blockNameIdMap;
    @Nonnull
    private final Object2ObjectMap<String, IntSet> groupNameIdMap;
    @Nonnull
    private final Object2ObjectMap<String, IntSet> hitboxNameIdMap;
    @Nonnull
    private final Object2ObjectMap<String, IntSet> categoryIdMap;
    
    public BlockSetLookupTable(@Nonnull final Map<String, BlockType> blockTypeMap) {
        final Object2ObjectMap<String, IntSet> blockNameIdMap = new Object2ObjectOpenHashMap<String, IntSet>();
        final Object2ObjectMap<String, IntSet> groupNameIdMap = new Object2ObjectOpenHashMap<String, IntSet>();
        final Object2ObjectMap<String, IntSet> hitboxNameIdMap = new Object2ObjectOpenHashMap<String, IntSet>();
        final Object2ObjectMap<String, IntSet> categoryIdMap = new Object2ObjectOpenHashMap<String, IntSet>();
        final BlockTypeAssetMap<String, BlockType> assetMap = BlockType.getAssetMap();
        blockTypeMap.keySet().forEach(blockName -> {
            final int index = assetMap.getIndex(blockName);
            if (index == Integer.MIN_VALUE) {
                throw new IllegalArgumentException("Unknown key! " + blockName);
            }
            else {
                blockNameIdMap.computeIfAbsent(blockName, s -> new IntOpenHashSet()).add(index);
                return;
            }
        });
        blockTypeMap.forEach((blockTypeKey, blockType) -> {
            final String group = blockType.getGroup();
            if (group != null && !group.isEmpty()) {
                final int index2 = assetMap.getIndex(blockTypeKey);
                if (index2 == Integer.MIN_VALUE) {
                    throw new IllegalArgumentException("Unknown key! " + blockTypeKey);
                }
                else {
                    groupNameIdMap.computeIfAbsent(group, s -> new IntOpenHashSet()).add(index2);
                }
            }
            String hitboxType = blockType.getHitboxType();
            if (hitboxType != null && !hitboxType.isEmpty()) {
                final int index3 = hitboxType.indexOf(124);
                if (index3 != 0) {
                    if (index3 > 0) {
                        hitboxType = hitboxType.substring(0, index3);
                    }
                    final int index4 = assetMap.getIndex(blockTypeKey);
                    if (index4 == Integer.MIN_VALUE) {
                        throw new IllegalArgumentException("Unknown key! " + blockTypeKey);
                    }
                    else {
                        hitboxNameIdMap.computeIfAbsent(hitboxType, s -> new IntOpenHashSet()).add(index4);
                    }
                }
            }
            final String name = blockType.getId();
            final Item item = Item.getAssetMap().getAsset(name);
            if (item != null) {
                final String[] categories = item.getCategories();
                if (categories != null) {
                    final String[] arr$ = categories;
                    final int len$ = arr$.length;
                    int i$ = 0;
                    while (i$ < len$) {
                        final String category = arr$[i$];
                        final int index5 = assetMap.getIndex(blockTypeKey);
                        if (index5 == Integer.MIN_VALUE) {
                            throw new IllegalArgumentException("Unknown key! " + blockTypeKey);
                        }
                        else {
                            categoryIdMap.computeIfAbsent(category, s -> new IntOpenHashSet()).add(index5);
                            ++i$;
                        }
                    }
                }
            }
            return;
        });
        blockNameIdMap.replaceAll((s, tIntSet) -> {
            ((IntOpenHashSet)tIntSet).trim();
            return IntSets.unmodifiable(tIntSet);
        });
        groupNameIdMap.replaceAll((s, tIntSet) -> {
            ((IntOpenHashSet)tIntSet).trim();
            return IntSets.unmodifiable(tIntSet);
        });
        hitboxNameIdMap.replaceAll((s, tIntSet) -> {
            ((IntOpenHashSet)tIntSet).trim();
            return IntSets.unmodifiable(tIntSet);
        });
        categoryIdMap.replaceAll((s, tIntSet) -> {
            ((IntOpenHashSet)tIntSet).trim();
            return IntSets.unmodifiable(tIntSet);
        });
        this.blockNameIdMap = Object2ObjectMaps.unmodifiable((Object2ObjectMap<? extends String, ? extends IntSet>)blockNameIdMap);
        this.groupNameIdMap = Object2ObjectMaps.unmodifiable((Object2ObjectMap<? extends String, ? extends IntSet>)groupNameIdMap);
        this.hitboxNameIdMap = Object2ObjectMaps.unmodifiable((Object2ObjectMap<? extends String, ? extends IntSet>)hitboxNameIdMap);
        this.categoryIdMap = Object2ObjectMaps.unmodifiable((Object2ObjectMap<? extends String, ? extends IntSet>)categoryIdMap);
    }
    
    public void addAll(@Nonnull final IntSet result) {
        final ObjectCollection<IntSet> values = this.blockNameIdMap.values();
        Objects.requireNonNull(result);
        values.forEach(result::addAll);
    }
    
    @Nonnull
    public Object2ObjectMap<String, IntSet> getBlockNameIdMap() {
        return this.blockNameIdMap;
    }
    
    @Nonnull
    public Object2ObjectMap<String, IntSet> getGroupNameIdMap() {
        return this.groupNameIdMap;
    }
    
    @Nonnull
    public Object2ObjectMap<String, IntSet> getHitboxNameIdMap() {
        return this.hitboxNameIdMap;
    }
    
    @Nonnull
    public Object2ObjectMap<String, IntSet> getCategoryIdMap() {
        return this.categoryIdMap;
    }
    
    public boolean isEmpty() {
        return this.blockNameIdMap.isEmpty() && this.groupNameIdMap.isEmpty() && this.hitboxNameIdMap.isEmpty() && this.categoryIdMap.isEmpty();
    }
}
