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

package com.hypixel.hytale.builtin.hytalegenerator.assets.blockmask;

import com.hypixel.hytale.codec.codecs.array.ArrayCodec;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import java.util.HashMap;
import com.hypixel.hytale.builtin.hytalegenerator.LoggerUtil;
import com.hypixel.hytale.builtin.hytalegenerator.MaterialSet;
import com.hypixel.hytale.builtin.hytalegenerator.BlockMask;
import javax.annotation.Nonnull;
import com.hypixel.hytale.builtin.hytalegenerator.material.MaterialCache;
import com.hypixel.hytale.builtin.hytalegenerator.assets.blockset.MaterialSetAsset;
import com.hypixel.hytale.assetstore.AssetExtraInfo;
import com.hypixel.hytale.assetstore.codec.AssetBuilderCodec;
import java.util.Map;
import com.hypixel.hytale.builtin.hytalegenerator.assets.Cleanable;
import com.hypixel.hytale.assetstore.map.DefaultAssetMap;
import com.hypixel.hytale.assetstore.map.JsonAssetWithMap;

public class BlockMaskAsset implements JsonAssetWithMap<String, DefaultAssetMap<String, BlockMaskAsset>>, Cleanable
{
    private static final Map<String, Exported> exportedNodes;
    public static final AssetBuilderCodec<String, BlockMaskAsset> CODEC;
    private String id;
    private AssetExtraInfo.Data data;
    protected String exportName;
    protected String importName;
    private MaterialSetAsset dontPlaceMaterialSetAsset;
    private MaterialSetAsset dontReplaceMaterialSetAsset;
    private BlockMaskEntryAsset[] blockMaskEntries;
    
    public BlockMaskAsset() {
        this.exportName = "";
        this.importName = "";
        this.dontPlaceMaterialSetAsset = new MaterialSetAsset();
        this.dontReplaceMaterialSetAsset = new MaterialSetAsset();
        this.blockMaskEntries = new BlockMaskEntryAsset[0];
    }
    
    public BlockMask build(@Nonnull final MaterialCache materialCache) {
        if (this.importName == null || this.importName.isEmpty()) {
            final MaterialSet dontPlaceBlockSet = (this.dontPlaceMaterialSetAsset == null) ? new MaterialSet() : this.dontPlaceMaterialSetAsset.build(materialCache);
            final MaterialSet dontReplaceBlockSet = (this.dontReplaceMaterialSetAsset == null) ? new MaterialSet() : this.dontReplaceMaterialSetAsset.build(materialCache);
            final BlockMask blockMask = new BlockMask();
            blockMask.setSkippedBlocks(dontPlaceBlockSet);
            blockMask.setDefaultMask(dontReplaceBlockSet);
            for (final BlockMaskEntryAsset entry : this.blockMaskEntries) {
                blockMask.putBlockMaskEntry(entry.getPropBlockSet(materialCache), entry.getReplacesBlockSet(materialCache));
            }
            return blockMask;
        }
        final Exported importedAssetEntry = BlockMaskAsset.exportedNodes.get(this.importName);
        if (importedAssetEntry == null || importedAssetEntry.asset == null) {
            LoggerUtil.getLogger().warning("Imported BlockMask asset with name '" + this.importName + "' not found");
            return new BlockMask();
        }
        return importedAssetEntry.asset.build(materialCache);
    }
    
    @Override
    public String getId() {
        return this.id;
    }
    
    @Override
    public void cleanUp() {
        this.dontPlaceMaterialSetAsset.cleanUp();
        this.dontReplaceMaterialSetAsset.cleanUp();
        for (final BlockMaskEntryAsset blockMaskEntryAsset : this.blockMaskEntries) {
            blockMaskEntryAsset.cleanUp();
        }
    }
    
    static {
        exportedNodes = new HashMap<String, Exported>();
        CODEC = ((AssetBuilderCodec.Builder)AssetBuilderCodec.builder(BlockMaskAsset.class, BlockMaskAsset::new, (Codec<String>)Codec.STRING, (asset, id) -> asset.id = id, config -> config.id, (config, data) -> config.data = data, config -> config.data).append(new KeyedCodec<MaterialSetAsset>("DontPlace", (Codec<MaterialSetAsset>)MaterialSetAsset.CODEC, false), (t, k) -> t.dontPlaceMaterialSetAsset = k, t -> t.dontPlaceMaterialSetAsset).add().append(new KeyedCodec("DontReplace", MaterialSetAsset.CODEC, false), (t, k) -> t.dontReplaceMaterialSetAsset = k, t -> t.dontReplaceMaterialSetAsset).add().append(new KeyedCodec("Advanced", new ArrayCodec(BlockMaskEntryAsset.CODEC, BlockMaskEntryAsset[]::new), false), (t, k) -> t.blockMaskEntries = k, t -> t.blockMaskEntries).add().append(new KeyedCodec("ExportAs", Codec.STRING, false), (t, k) -> t.exportName = k, t -> t.exportName).add().append(new KeyedCodec("Import", Codec.STRING, false), (t, k) -> t.importName = k, t -> t.importName).add().afterDecode(asset -> {
            if (asset.exportName != null && !asset.exportName.isEmpty()) {
                if (BlockMaskAsset.exportedNodes.containsKey(asset.exportName)) {
                    LoggerUtil.getLogger().warning("Duplicate export name for asset: " + asset.exportName);
                }
                final Exported exported = new Exported();
                exported.asset = asset;
                BlockMaskAsset.exportedNodes.put(asset.exportName, exported);
                LoggerUtil.getLogger().fine("Registered imported node asset with name '" + asset.exportName + "' with asset id '" + asset.id);
            }
        })).build();
    }
    
    public static class Exported
    {
        public BlockMaskAsset asset;
    }
}
