package net.minecraft.data.models.model;

import java.util.stream.Collector;
import com.google.common.collect.ImmutableMap;
import java.util.function.Function;
import com.google.common.collect.Streams;
import java.util.stream.Stream;
import java.util.Map;
import com.google.gson.JsonObject;
import com.google.gson.JsonElement;
import java.util.function.Supplier;
import java.util.function.BiConsumer;
import net.minecraft.world.level.block.Block;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
import net.minecraft.resources.ResourceLocation;
import java.util.Optional;

public class ModelTemplate {
    private final Optional<ResourceLocation> model;
    private final Set<TextureSlot> requiredSlots;
    private Optional<String> suffix;
    
    public ModelTemplate(final Optional<ResourceLocation> optional1, final Optional<String> optional2, final TextureSlot... arr) {
        this.model = optional1;
        this.suffix = optional2;
        this.requiredSlots = ImmutableSet.<TextureSlot>copyOf(arr);
    }
    
    public ResourceLocation create(final Block bvr, final TextureMapping iy, final BiConsumer<ResourceLocation, Supplier<JsonElement>> biConsumer) {
        return this.create(ModelLocationUtils.getModelLocation(bvr, this.suffix.orElse("")), iy, biConsumer);
    }
    
    public ResourceLocation createWithSuffix(final Block bvr, final String string, final TextureMapping iy, final BiConsumer<ResourceLocation, Supplier<JsonElement>> biConsumer) {
        return this.create(ModelLocationUtils.getModelLocation(bvr, string + this.suffix.orElse("")), iy, biConsumer);
    }
    
    public ResourceLocation createWithOverride(final Block bvr, final String string, final TextureMapping iy, final BiConsumer<ResourceLocation, Supplier<JsonElement>> biConsumer) {
        return this.create(ModelLocationUtils.getModelLocation(bvr, string), iy, biConsumer);
    }
    
    public ResourceLocation create(final ResourceLocation uh, final TextureMapping iy, final BiConsumer<ResourceLocation, Supplier<JsonElement>> biConsumer) {
        final Map<TextureSlot, ResourceLocation> map5 = this.createMap(iy);
        final JsonObject jsonObject3;
        final Map map6;
        JsonObject jsonObject4;
        biConsumer.accept(uh, () -> {
            jsonObject3 = new JsonObject();
            this.model.ifPresent(uh -> jsonObject3.addProperty("parent", uh.toString()));
            if (!map6.isEmpty()) {
                jsonObject4 = new JsonObject();
                map6.forEach((iz, uh) -> jsonObject4.addProperty(iz.getId(), uh.toString()));
                jsonObject3.add("textures", jsonObject4);
            }
            return jsonObject3;
        });
        return uh;
    }
    
    private Map<TextureSlot, ResourceLocation> createMap(final TextureMapping iy) {
        return Streams.concat(new Stream[] { this.requiredSlots.stream(), iy.getForced() }).collect(ImmutableMap.toImmutableMap(Function.identity(), iy::get));
    }
}
