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

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

import com.hypixel.hytale.codec.validation.Validator;
import com.hypixel.hytale.codec.validation.Validators;
import com.hypixel.hytale.assetstore.AssetExtraInfo;
import com.hypixel.hytale.assetstore.codec.AssetBuilderCodec;
import com.hypixel.hytale.assetstore.map.DefaultAssetMap;
import com.hypixel.hytale.assetstore.map.JsonAssetWithMap;
import com.hypixel.hytale.builtin.hytalegenerator.assets.Cleanable;
import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.codec.codecs.array.ArrayCodec;
import com.hypixel.hytale.builtin.hytalegenerator.props.WeightedProp;
import com.hypixel.hytale.builtin.hytalegenerator.datastructures.WeightedMap;
import com.hypixel.hytale.builtin.hytalegenerator.props.Prop;
import javax.annotation.Nonnull;
import com.hypixel.hytale.codec.builder.BuilderCodec;

public class WeightedPropAsset extends PropAsset
{
    public static final BuilderCodec<WeightedPropAsset> CODEC;
    private EntryAsset[] entryAssets;
    private String seed;
    
    public WeightedPropAsset() {
        this.entryAssets = new EntryAsset[0];
        this.seed = "";
    }
    
    @Nonnull
    @Override
    public Prop build(@Nonnull final Argument argument) {
        if (super.skip() || this.entryAssets.length == 0) {
            return Prop.noProp();
        }
        final WeightedMap<Prop> weightedProps = new WeightedMap<Prop>(this.entryAssets.length);
        final Argument childArgument = new Argument(argument);
        childArgument.parentSeed = argument.parentSeed.child(this.seed);
        for (final EntryAsset entryAsset : this.entryAssets) {
            weightedProps.add(entryAsset.propAsset.build(childArgument), entryAsset.weight);
        }
        return new WeightedProp(weightedProps, childArgument.parentSeed.createSupplier().get());
    }
    
    @Override
    public void cleanUp() {
        for (final EntryAsset asset : this.entryAssets) {
            asset.cleanUp();
        }
    }
    
    static {
        CODEC = BuilderCodec.builder(WeightedPropAsset.class, WeightedPropAsset::new, PropAsset.ABSTRACT_CODEC).append(new KeyedCodec<EntryAsset[]>("Entries", new ArrayCodec<EntryAsset>((Codec<EntryAsset>)EntryAsset.CODEC, EntryAsset[]::new), true), (asset, value) -> asset.entryAssets = value, asset -> asset.entryAssets).add().append(new KeyedCodec("Seed", Codec.STRING, true), (asset, value) -> asset.seed = value, asset -> asset.seed).add().build();
    }
    
    public static class EntryAsset implements Cleanable, JsonAssetWithMap<String, DefaultAssetMap<String, EntryAsset>>
    {
        public static final AssetBuilderCodec<String, EntryAsset> CODEC;
        private String id;
        private AssetExtraInfo.Data data;
        private double weight;
        private PropAsset propAsset;
        
        public EntryAsset() {
            this.weight = 1.0;
            this.propAsset = new NoPropAsset();
        }
        
        @Override
        public String getId() {
            return this.id;
        }
        
        @Override
        public void cleanUp() {
            this.propAsset.cleanUp();
        }
        
        static {
            CODEC = ((AssetBuilderCodec.Builder)AssetBuilderCodec.builder(EntryAsset.class, EntryAsset::new, (Codec<String>)Codec.STRING, (asset, id) -> asset.id = id, config -> config.id, (config, data) -> config.data = data, config -> config.data).append(new KeyedCodec<Double>("Weight", Codec.DOUBLE, true), (asset, value) -> asset.weight = value, asset -> asset.weight).addValidator((Validator<? super Double>)Validators.greaterThan(0.0)).add().append(new KeyedCodec("Prop", PropAsset.CODEC, true), (asset, value) -> asset.propAsset = value, asset -> asset.propAsset).add()).build();
        }
    }
}
