package com.mojang.blaze3d.vertex;

import java.util.List;
import com.mojang.blaze3d.systems.RenderSystem;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.function.Function;
import com.google.common.collect.UnmodifiableIterator;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import com.google.common.collect.ImmutableList;

public class VertexFormat {
    private final ImmutableList<VertexFormatElement> elements;
    private final IntList offsets;
    private final int vertexSize;
    
    public VertexFormat(final ImmutableList<VertexFormatElement> immutableList) {
        this.offsets = (IntList)new IntArrayList();
        this.elements = immutableList;
        int integer3 = 0;
        for (final VertexFormatElement dhr5 : immutableList) {
            this.offsets.add(integer3);
            integer3 += dhr5.getByteSize();
        }
        this.vertexSize = integer3;
    }
    
    @Override
    public String toString() {
        return "format: " + this.elements.size() + " elements: " + this.elements.stream().map(Object::toString).collect(Collectors.joining(" "));
    }
    
    public int getIntegerSize() {
        return this.getVertexSize() / 4;
    }
    
    public int getVertexSize() {
        return this.vertexSize;
    }
    
    public ImmutableList<VertexFormatElement> getElements() {
        return this.elements;
    }
    
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (object == null || this.getClass() != object.getClass()) {
            return false;
        }
        final VertexFormat dhq3 = (VertexFormat)object;
        return this.vertexSize == dhq3.vertexSize && this.elements.equals(dhq3.elements);
    }
    
    @Override
    public int hashCode() {
        return this.elements.hashCode();
    }
    
    public void setupBufferState(final long long1) {
        if (!RenderSystem.isOnRenderThread()) {
            RenderSystem.recordRenderCall(() -> this.setupBufferState(long1));
            return;
        }
        final int integer4 = this.getVertexSize();
        final List<VertexFormatElement> list5 = this.getElements();
        for (int integer5 = 0; integer5 < list5.size(); ++integer5) {
            list5.get(integer5).setupBufferState(long1 + this.offsets.getInt(integer5), integer4);
        }
    }
    
    public void clearBufferState() {
        if (!RenderSystem.isOnRenderThread()) {
            RenderSystem.recordRenderCall(this::clearBufferState);
            return;
        }
        for (final VertexFormatElement dhr3 : this.getElements()) {
            dhr3.clearBufferState();
        }
    }
}
