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 cyg) {
        return immediateWithBuffers(ImmutableMap.of(), cyg);
    }
    
    default BufferSource immediateWithBuffers(final Map<RenderType, BufferBuilder> map, final BufferBuilder cyg) {
        return new BufferSource(cyg, map);
    }
    
    VertexConsumer getBuffer(final RenderType drb);
    
    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 cyg, final Map<RenderType, BufferBuilder> map) {
            this.lastState = Optional.<RenderType>empty();
            this.startedBuffers = Sets.newHashSet();
            this.builder = cyg;
            this.fixedBuffers = map;
        }
        
        @Override
        public VertexConsumer getBuffer(final RenderType drb) {
            final Optional<RenderType> optional3 = drb.asOptional();
            final BufferBuilder cyg4 = this.getBuilderRaw(drb);
            if (!Objects.equals(this.lastState, optional3)) {
                if (this.lastState.isPresent()) {
                    final RenderType drb2 = this.lastState.get();
                    if (!this.fixedBuffers.containsKey(drb2)) {
                        this.endBatch(drb2);
                    }
                }
                if (this.startedBuffers.add(cyg4)) {
                    cyg4.begin(drb.mode(), drb.format());
                }
                this.lastState = optional3;
            }
            return cyg4;
        }
        
        private BufferBuilder getBuilderRaw(final RenderType drb) {
            return this.fixedBuffers.getOrDefault(drb, this.builder);
        }
        
        public void endBatch() {
            final VertexConsumer cyo3;
            this.lastState.ifPresent(drb -> {
                cyo3 = this.getBuffer(drb);
                if (cyo3 == this.builder) {
                    this.endBatch(drb);
                }
                return;
            });
            for (final RenderType drb2 : this.fixedBuffers.keySet()) {
                this.endBatch(drb2);
            }
        }
        
        public void endBatch(final RenderType drb) {
            final BufferBuilder cyg3 = this.getBuilderRaw(drb);
            final boolean boolean4 = Objects.equals(this.lastState, drb.asOptional());
            if (!boolean4 && cyg3 == this.builder) {
                return;
            }
            if (!this.startedBuffers.remove(cyg3)) {
                return;
            }
            drb.end(cyg3, 0, 0, 0);
            if (boolean4) {
                this.lastState = Optional.<RenderType>empty();
            }
        }
    }
}
