package net.minecraft.client.renderer;

import java.util.Iterator;
import java.util.Objects;
import com.google.common.collect.Sets;
import java.util.Set;
import java.util.Optional;
import com.mojang.blaze3d.vertex.VertexConsumer;
import java.util.Map;
import com.google.common.collect.ImmutableMap;
import com.mojang.blaze3d.vertex.BufferBuilder;

public interface MultiBufferSource {
    default BufferSource immediate(final BufferBuilder dhg) {
        return immediateWithBuffers(ImmutableMap.of(), dhg);
    }
    
    default BufferSource immediateWithBuffers(final Map<RenderType, BufferBuilder> map, final BufferBuilder dhg) {
        return new BufferSource(dhg, map);
    }
    
    VertexConsumer getBuffer(final RenderType ech);
    
    public static class BufferSource implements MultiBufferSource {
        protected final BufferBuilder builder;
        protected final Map<RenderType, BufferBuilder> fixedBuffers;
        protected Optional<RenderType> lastState;
        protected final Set<BufferBuilder> startedBuffers;
        
        protected BufferSource(final BufferBuilder dhg, final Map<RenderType, BufferBuilder> map) {
            this.lastState = Optional.<RenderType>empty();
            this.startedBuffers = Sets.newHashSet();
            this.builder = dhg;
            this.fixedBuffers = map;
        }
        
        @Override
        public VertexConsumer getBuffer(final RenderType ech) {
            final Optional<RenderType> optional3 = ech.asOptional();
            final BufferBuilder dhg4 = this.getBuilderRaw(ech);
            if (!Objects.equals(this.lastState, optional3)) {
                if (this.lastState.isPresent()) {
                    final RenderType ech2 = this.lastState.get();
                    if (!this.fixedBuffers.containsKey(ech2)) {
                        this.endBatch(ech2);
                    }
                }
                if (this.startedBuffers.add(dhg4)) {
                    dhg4.begin(ech.mode(), ech.format());
                }
                this.lastState = optional3;
            }
            return dhg4;
        }
        
        private BufferBuilder getBuilderRaw(final RenderType ech) {
            return this.fixedBuffers.getOrDefault(ech, this.builder);
        }
        
        public void endBatch() {
            final VertexConsumer dhp3;
            this.lastState.ifPresent(ech -> {
                dhp3 = this.getBuffer(ech);
                if (dhp3 == this.builder) {
                    this.endBatch(ech);
                }
                return;
            });
            for (final RenderType ech2 : this.fixedBuffers.keySet()) {
                this.endBatch(ech2);
            }
        }
        
        public void endBatch(final RenderType ech) {
            final BufferBuilder dhg3 = this.getBuilderRaw(ech);
            final boolean boolean4 = Objects.equals(this.lastState, ech.asOptional());
            if (!boolean4 && dhg3 == this.builder) {
                return;
            }
            if (!this.startedBuffers.remove(dhg3)) {
                return;
            }
            ech.end(dhg3, 0, 0, 0);
            if (boolean4) {
                this.lastState = Optional.<RenderType>empty();
            }
        }
    }
}
