// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.protocol;

import java.util.Objects;
import java.util.Arrays;
import com.hypixel.hytale.protocol.io.ValidationResult;
import com.hypixel.hytale.protocol.io.PacketIO;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class Fluid
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 22;
    public static final int VARIABLE_FIELD_COUNT = 5;
    public static final int VARIABLE_BLOCK_START = 42;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String id;
    public int maxFluidLevel;
    @Nullable
    public BlockTextures[] cubeTextures;
    public boolean requiresAlphaBlending;
    @Nonnull
    public Opacity opacity;
    @Nullable
    public ShaderType[] shaderEffect;
    @Nullable
    public ColorLight light;
    public int fluidFXIndex;
    public int blockSoundSetIndex;
    @Nullable
    public String blockParticleSetId;
    @Nullable
    public Color particleColor;
    @Nullable
    public int[] tagIndexes;
    
    public Fluid() {
        this.opacity = Opacity.Solid;
    }
    
    public Fluid(@Nullable final String id, final int maxFluidLevel, @Nullable final BlockTextures[] cubeTextures, final boolean requiresAlphaBlending, @Nonnull final Opacity opacity, @Nullable final ShaderType[] shaderEffect, @Nullable final ColorLight light, final int fluidFXIndex, final int blockSoundSetIndex, @Nullable final String blockParticleSetId, @Nullable final Color particleColor, @Nullable final int[] tagIndexes) {
        this.opacity = Opacity.Solid;
        this.id = id;
        this.maxFluidLevel = maxFluidLevel;
        this.cubeTextures = cubeTextures;
        this.requiresAlphaBlending = requiresAlphaBlending;
        this.opacity = opacity;
        this.shaderEffect = shaderEffect;
        this.light = light;
        this.fluidFXIndex = fluidFXIndex;
        this.blockSoundSetIndex = blockSoundSetIndex;
        this.blockParticleSetId = blockParticleSetId;
        this.particleColor = particleColor;
        this.tagIndexes = tagIndexes;
    }
    
    public Fluid(@Nonnull final Fluid other) {
        this.opacity = Opacity.Solid;
        this.id = other.id;
        this.maxFluidLevel = other.maxFluidLevel;
        this.cubeTextures = other.cubeTextures;
        this.requiresAlphaBlending = other.requiresAlphaBlending;
        this.opacity = other.opacity;
        this.shaderEffect = other.shaderEffect;
        this.light = other.light;
        this.fluidFXIndex = other.fluidFXIndex;
        this.blockSoundSetIndex = other.blockSoundSetIndex;
        this.blockParticleSetId = other.blockParticleSetId;
        this.particleColor = other.particleColor;
        this.tagIndexes = other.tagIndexes;
    }
    
    @Nonnull
    public static Fluid deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final Fluid obj = new Fluid();
        final byte nullBits = buf.getByte(offset);
        obj.maxFluidLevel = buf.getIntLE(offset + 1);
        obj.requiresAlphaBlending = (buf.getByte(offset + 5) != 0);
        obj.opacity = Opacity.fromValue(buf.getByte(offset + 6));
        if ((nullBits & 0x1) != 0x0) {
            obj.light = ColorLight.deserialize(buf, offset + 7);
        }
        obj.fluidFXIndex = buf.getIntLE(offset + 11);
        obj.blockSoundSetIndex = buf.getIntLE(offset + 15);
        if ((nullBits & 0x2) != 0x0) {
            obj.particleColor = Color.deserialize(buf, offset + 19);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos0 = offset + 42 + buf.getIntLE(offset + 22);
            final int idLen = VarInt.peek(buf, varPos0);
            if (idLen < 0) {
                throw ProtocolException.negativeLength("Id", idLen);
            }
            if (idLen > 4096000) {
                throw ProtocolException.stringTooLong("Id", idLen, 4096000);
            }
            obj.id = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos2 = offset + 42 + buf.getIntLE(offset + 26);
            final int cubeTexturesCount = VarInt.peek(buf, varPos2);
            if (cubeTexturesCount < 0) {
                throw ProtocolException.negativeLength("CubeTextures", cubeTexturesCount);
            }
            if (cubeTexturesCount > 4096000) {
                throw ProtocolException.arrayTooLong("CubeTextures", cubeTexturesCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + cubeTexturesCount * 5L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("CubeTextures", varPos2 + varIntLen + cubeTexturesCount * 5, buf.readableBytes());
            }
            obj.cubeTextures = new BlockTextures[cubeTexturesCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < cubeTexturesCount; ++i) {
                obj.cubeTextures[i] = BlockTextures.deserialize(buf, elemPos);
                elemPos += BlockTextures.computeBytesConsumed(buf, elemPos);
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos3 = offset + 42 + buf.getIntLE(offset + 30);
            final int shaderEffectCount = VarInt.peek(buf, varPos3);
            if (shaderEffectCount < 0) {
                throw ProtocolException.negativeLength("ShaderEffect", shaderEffectCount);
            }
            if (shaderEffectCount > 4096000) {
                throw ProtocolException.arrayTooLong("ShaderEffect", shaderEffectCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            if (varPos3 + varIntLen + shaderEffectCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("ShaderEffect", varPos3 + varIntLen + shaderEffectCount * 1, buf.readableBytes());
            }
            obj.shaderEffect = new ShaderType[shaderEffectCount];
            int elemPos = varPos3 + varIntLen;
            for (int i = 0; i < shaderEffectCount; ++i) {
                obj.shaderEffect[i] = ShaderType.fromValue(buf.getByte(elemPos));
                ++elemPos;
            }
        }
        if ((nullBits & 0x20) != 0x0) {
            final int varPos4 = offset + 42 + buf.getIntLE(offset + 34);
            final int blockParticleSetIdLen = VarInt.peek(buf, varPos4);
            if (blockParticleSetIdLen < 0) {
                throw ProtocolException.negativeLength("BlockParticleSetId", blockParticleSetIdLen);
            }
            if (blockParticleSetIdLen > 4096000) {
                throw ProtocolException.stringTooLong("BlockParticleSetId", blockParticleSetIdLen, 4096000);
            }
            obj.blockParticleSetId = PacketIO.readVarString(buf, varPos4, PacketIO.UTF8);
        }
        if ((nullBits & 0x40) != 0x0) {
            final int varPos5 = offset + 42 + buf.getIntLE(offset + 38);
            final int tagIndexesCount = VarInt.peek(buf, varPos5);
            if (tagIndexesCount < 0) {
                throw ProtocolException.negativeLength("TagIndexes", tagIndexesCount);
            }
            if (tagIndexesCount > 4096000) {
                throw ProtocolException.arrayTooLong("TagIndexes", tagIndexesCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos5);
            if (varPos5 + varIntLen + tagIndexesCount * 4L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("TagIndexes", varPos5 + varIntLen + tagIndexesCount * 4, buf.readableBytes());
            }
            obj.tagIndexes = new int[tagIndexesCount];
            for (int j = 0; j < tagIndexesCount; ++j) {
                obj.tagIndexes[j] = buf.getIntLE(varPos5 + varIntLen + j * 4);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 42;
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 22);
            int pos0 = offset + 42 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 26);
            int pos2 = offset + 42 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                pos2 += BlockTextures.computeBytesConsumed(buf, pos2);
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 30);
            int pos3 = offset + 42 + fieldOffset3;
            final int arrLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + arrLen * 1;
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits & 0x20) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 34);
            int pos4 = offset + 42 + fieldOffset4;
            final int sl = VarInt.peek(buf, pos4);
            pos4 += VarInt.length(buf, pos4) + sl;
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        if ((nullBits & 0x40) != 0x0) {
            final int fieldOffset5 = buf.getIntLE(offset + 38);
            int pos5 = offset + 42 + fieldOffset5;
            final int arrLen = VarInt.peek(buf, pos5);
            pos5 += VarInt.length(buf, pos5) + arrLen * 4;
            if (pos5 - offset > maxEnd) {
                maxEnd = pos5 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.light != null) {
            nullBits |= 0x1;
        }
        if (this.particleColor != null) {
            nullBits |= 0x2;
        }
        if (this.id != null) {
            nullBits |= 0x4;
        }
        if (this.cubeTextures != null) {
            nullBits |= 0x8;
        }
        if (this.shaderEffect != null) {
            nullBits |= 0x10;
        }
        if (this.blockParticleSetId != null) {
            nullBits |= 0x20;
        }
        if (this.tagIndexes != null) {
            nullBits |= 0x40;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.maxFluidLevel);
        buf.writeByte(this.requiresAlphaBlending ? 1 : 0);
        buf.writeByte(this.opacity.getValue());
        if (this.light != null) {
            this.light.serialize(buf);
        }
        else {
            buf.writeZero(4);
        }
        buf.writeIntLE(this.fluidFXIndex);
        buf.writeIntLE(this.blockSoundSetIndex);
        if (this.particleColor != null) {
            this.particleColor.serialize(buf);
        }
        else {
            buf.writeZero(3);
        }
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int cubeTexturesOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int shaderEffectOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int blockParticleSetIdOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int tagIndexesOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.id != null) {
            buf.setIntLE(idOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.id, 4096000);
        }
        else {
            buf.setIntLE(idOffsetSlot, -1);
        }
        if (this.cubeTextures != null) {
            buf.setIntLE(cubeTexturesOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.cubeTextures.length > 4096000) {
                throw ProtocolException.arrayTooLong("CubeTextures", this.cubeTextures.length, 4096000);
            }
            VarInt.write(buf, this.cubeTextures.length);
            for (final BlockTextures item : this.cubeTextures) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(cubeTexturesOffsetSlot, -1);
        }
        if (this.shaderEffect != null) {
            buf.setIntLE(shaderEffectOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.shaderEffect.length > 4096000) {
                throw ProtocolException.arrayTooLong("ShaderEffect", this.shaderEffect.length, 4096000);
            }
            VarInt.write(buf, this.shaderEffect.length);
            for (final ShaderType item2 : this.shaderEffect) {
                buf.writeByte(item2.getValue());
            }
        }
        else {
            buf.setIntLE(shaderEffectOffsetSlot, -1);
        }
        if (this.blockParticleSetId != null) {
            buf.setIntLE(blockParticleSetIdOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.blockParticleSetId, 4096000);
        }
        else {
            buf.setIntLE(blockParticleSetIdOffsetSlot, -1);
        }
        if (this.tagIndexes != null) {
            buf.setIntLE(tagIndexesOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.tagIndexes.length > 4096000) {
                throw ProtocolException.arrayTooLong("TagIndexes", this.tagIndexes.length, 4096000);
            }
            VarInt.write(buf, this.tagIndexes.length);
            for (final int item3 : this.tagIndexes) {
                buf.writeIntLE(item3);
            }
        }
        else {
            buf.setIntLE(tagIndexesOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 42;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.cubeTextures != null) {
            int cubeTexturesSize = 0;
            for (final BlockTextures elem : this.cubeTextures) {
                cubeTexturesSize += elem.computeSize();
            }
            size += VarInt.size(this.cubeTextures.length) + cubeTexturesSize;
        }
        if (this.shaderEffect != null) {
            size += VarInt.size(this.shaderEffect.length) + this.shaderEffect.length * 1;
        }
        if (this.blockParticleSetId != null) {
            size += PacketIO.stringSize(this.blockParticleSetId);
        }
        if (this.tagIndexes != null) {
            size += VarInt.size(this.tagIndexes.length) + this.tagIndexes.length * 4;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 42) {
            return ValidationResult.error("Buffer too small: expected at least 42 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x4) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 22);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 42 + idOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Id");
            }
            final int idLen = VarInt.peek(buffer, pos);
            if (idLen < 0) {
                return ValidationResult.error("Invalid string length for Id");
            }
            if (idLen > 4096000) {
                return ValidationResult.error("Id exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += idLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Id");
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int cubeTexturesOffset = buffer.getIntLE(offset + 26);
            if (cubeTexturesOffset < 0) {
                return ValidationResult.error("Invalid offset for CubeTextures");
            }
            int pos = offset + 42 + cubeTexturesOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for CubeTextures");
            }
            final int cubeTexturesCount = VarInt.peek(buffer, pos);
            if (cubeTexturesCount < 0) {
                return ValidationResult.error("Invalid array count for CubeTextures");
            }
            if (cubeTexturesCount > 4096000) {
                return ValidationResult.error("CubeTextures exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < cubeTexturesCount; ++i) {
                final ValidationResult structResult = BlockTextures.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid BlockTextures in CubeTextures[" + i + "]: " + structResult.error());
                }
                pos += BlockTextures.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int shaderEffectOffset = buffer.getIntLE(offset + 30);
            if (shaderEffectOffset < 0) {
                return ValidationResult.error("Invalid offset for ShaderEffect");
            }
            int pos = offset + 42 + shaderEffectOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ShaderEffect");
            }
            final int shaderEffectCount = VarInt.peek(buffer, pos);
            if (shaderEffectCount < 0) {
                return ValidationResult.error("Invalid array count for ShaderEffect");
            }
            if (shaderEffectCount > 4096000) {
                return ValidationResult.error("ShaderEffect exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += shaderEffectCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading ShaderEffect");
            }
        }
        if ((nullBits & 0x20) != 0x0) {
            final int blockParticleSetIdOffset = buffer.getIntLE(offset + 34);
            if (blockParticleSetIdOffset < 0) {
                return ValidationResult.error("Invalid offset for BlockParticleSetId");
            }
            int pos = offset + 42 + blockParticleSetIdOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for BlockParticleSetId");
            }
            final int blockParticleSetIdLen = VarInt.peek(buffer, pos);
            if (blockParticleSetIdLen < 0) {
                return ValidationResult.error("Invalid string length for BlockParticleSetId");
            }
            if (blockParticleSetIdLen > 4096000) {
                return ValidationResult.error("BlockParticleSetId exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += blockParticleSetIdLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading BlockParticleSetId");
            }
        }
        if ((nullBits & 0x40) != 0x0) {
            final int tagIndexesOffset = buffer.getIntLE(offset + 38);
            if (tagIndexesOffset < 0) {
                return ValidationResult.error("Invalid offset for TagIndexes");
            }
            int pos = offset + 42 + tagIndexesOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for TagIndexes");
            }
            final int tagIndexesCount = VarInt.peek(buffer, pos);
            if (tagIndexesCount < 0) {
                return ValidationResult.error("Invalid array count for TagIndexes");
            }
            if (tagIndexesCount > 4096000) {
                return ValidationResult.error("TagIndexes exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += tagIndexesCount * 4;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading TagIndexes");
            }
        }
        return ValidationResult.OK;
    }
    
    public Fluid clone() {
        final Fluid copy = new Fluid();
        copy.id = this.id;
        copy.maxFluidLevel = this.maxFluidLevel;
        copy.cubeTextures = (BlockTextures[])((this.cubeTextures != null) ? ((BlockTextures[])Arrays.stream(this.cubeTextures).map(e -> e.clone()).toArray(BlockTextures[]::new)) : null);
        copy.requiresAlphaBlending = this.requiresAlphaBlending;
        copy.opacity = this.opacity;
        copy.shaderEffect = (ShaderType[])((this.shaderEffect != null) ? ((ShaderType[])Arrays.copyOf(this.shaderEffect, this.shaderEffect.length)) : null);
        copy.light = ((this.light != null) ? this.light.clone() : null);
        copy.fluidFXIndex = this.fluidFXIndex;
        copy.blockSoundSetIndex = this.blockSoundSetIndex;
        copy.blockParticleSetId = this.blockParticleSetId;
        copy.particleColor = ((this.particleColor != null) ? this.particleColor.clone() : null);
        copy.tagIndexes = (int[])((this.tagIndexes != null) ? Arrays.copyOf(this.tagIndexes, this.tagIndexes.length) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final Fluid other) {
            return Objects.equals(this.id, other.id) && this.maxFluidLevel == other.maxFluidLevel && Arrays.equals(this.cubeTextures, other.cubeTextures) && this.requiresAlphaBlending == other.requiresAlphaBlending && Objects.equals(this.opacity, other.opacity) && Arrays.equals(this.shaderEffect, other.shaderEffect) && Objects.equals(this.light, other.light) && this.fluidFXIndex == other.fluidFXIndex && this.blockSoundSetIndex == other.blockSoundSetIndex && Objects.equals(this.blockParticleSetId, other.blockParticleSetId) && Objects.equals(this.particleColor, other.particleColor) && Arrays.equals(this.tagIndexes, other.tagIndexes);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.id);
        result = 31 * result + Integer.hashCode(this.maxFluidLevel);
        result = 31 * result + Arrays.hashCode(this.cubeTextures);
        result = 31 * result + Boolean.hashCode(this.requiresAlphaBlending);
        result = 31 * result + Objects.hashCode(this.opacity);
        result = 31 * result + Arrays.hashCode(this.shaderEffect);
        result = 31 * result + Objects.hashCode(this.light);
        result = 31 * result + Integer.hashCode(this.fluidFXIndex);
        result = 31 * result + Integer.hashCode(this.blockSoundSetIndex);
        result = 31 * result + Objects.hashCode(this.blockParticleSetId);
        result = 31 * result + Objects.hashCode(this.particleColor);
        result = 31 * result + Arrays.hashCode(this.tagIndexes);
        return result;
    }
}
