package net.minecraft.client.renderer.block.model;

import javax.annotation.Nullable;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonDeserializer;
import net.minecraft.client.renderer.block.model.multipart.Selector;
import java.lang.reflect.Type;
import com.google.gson.GsonBuilder;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.StateDefinition;
import com.google.gson.Gson;
import java.util.Iterator;
import java.util.List;
import com.google.common.collect.Maps;
import net.minecraft.util.GsonHelper;
import java.io.Reader;
import net.minecraft.client.renderer.block.model.multipart.MultiPart;
import java.util.Map;

public class BlockModelDefinition {
    private final Map<String, MultiVariant> variants;
    private MultiPart multiPart;
    
    public static BlockModelDefinition fromStream(final Context a, final Reader reader) {
        return GsonHelper.<BlockModelDefinition>fromJson(a.gson, reader, BlockModelDefinition.class);
    }
    
    public BlockModelDefinition(final Map<String, MultiVariant> map, final MultiPart edl) {
        this.variants = Maps.newLinkedHashMap();
        this.multiPart = edl;
        this.variants.putAll(map);
    }
    
    public BlockModelDefinition(final List<BlockModelDefinition> list) {
        this.variants = Maps.newLinkedHashMap();
        BlockModelDefinition ecz3 = null;
        for (final BlockModelDefinition ecz4 : list) {
            if (ecz4.isMultiPart()) {
                this.variants.clear();
                ecz3 = ecz4;
            }
            this.variants.putAll(ecz4.variants);
        }
        if (ecz3 != null) {
            this.multiPart = ecz3.multiPart;
        }
    }
    
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (object instanceof BlockModelDefinition) {
            final BlockModelDefinition ecz3 = (BlockModelDefinition)object;
            if (this.variants.equals(ecz3.variants)) {
                return this.isMultiPart() ? this.multiPart.equals(ecz3.multiPart) : (!ecz3.isMultiPart());
            }
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return 31 * this.variants.hashCode() + (this.isMultiPart() ? this.multiPart.hashCode() : 0);
    }
    
    public Map<String, MultiVariant> getVariants() {
        return this.variants;
    }
    
    public boolean isMultiPart() {
        return this.multiPart != null;
    }
    
    public MultiPart getMultiPart() {
        return this.multiPart;
    }
    
    public static final class Context {
        protected final Gson gson;
        private StateDefinition<Block, BlockState> definition;
        
        public Context() {
            this.gson = new GsonBuilder().registerTypeAdapter(BlockModelDefinition.class, new Deserializer()).registerTypeAdapter(Variant.class, new Variant.Deserializer()).registerTypeAdapter(MultiVariant.class, new MultiVariant.Deserializer()).registerTypeAdapter(MultiPart.class, new MultiPart.Deserializer(this)).registerTypeAdapter(Selector.class, new Selector.Deserializer()).create();
        }
        
        public StateDefinition<Block, BlockState> getDefinition() {
            return this.definition;
        }
        
        public void setDefinition(final StateDefinition<Block, BlockState> cfk) {
            this.definition = cfk;
        }
    }
    
    public static class Deserializer implements JsonDeserializer<BlockModelDefinition> {
        @Override
        public BlockModelDefinition deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
            final JsonObject jsonObject5 = jsonElement.getAsJsonObject();
            final Map<String, MultiVariant> map6 = this.getVariants(jsonDeserializationContext, jsonObject5);
            final MultiPart edl7 = this.getMultiPart(jsonDeserializationContext, jsonObject5);
            if (map6.isEmpty() && (edl7 == null || edl7.getMultiVariants().isEmpty())) {
                throw new JsonParseException("Neither 'variants' nor 'multipart' found");
            }
            return new BlockModelDefinition(map6, edl7);
        }
        
        protected Map<String, MultiVariant> getVariants(final JsonDeserializationContext jsonDeserializationContext, final JsonObject jsonObject) {
            final Map<String, MultiVariant> map4 = Maps.newHashMap();
            if (jsonObject.has("variants")) {
                final JsonObject jsonObject2 = GsonHelper.getAsJsonObject(jsonObject, "variants");
                for (final Map.Entry<String, JsonElement> entry7 : jsonObject2.entrySet()) {
                    map4.put(entry7.getKey(), jsonDeserializationContext.<MultiVariant>deserialize(entry7.getValue(), MultiVariant.class));
                }
            }
            return map4;
        }
        
        @Nullable
        protected MultiPart getMultiPart(final JsonDeserializationContext jsonDeserializationContext, final JsonObject jsonObject) {
            if (!jsonObject.has("multipart")) {
                return null;
            }
            final JsonArray jsonArray4 = GsonHelper.getAsJsonArray(jsonObject, "multipart");
            return jsonDeserializationContext.<MultiPart>deserialize(jsonArray4, MultiPart.class);
        }
    }
}
