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

package com.hypixel.hytale.protocol;

import java.util.Arrays;
import com.hypixel.hytale.protocol.io.ValidationResult;
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 OffsetNoise
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 13;
    public static final int MAX_SIZE = 282624028;
    @Nullable
    public NoiseConfig[] x;
    @Nullable
    public NoiseConfig[] y;
    @Nullable
    public NoiseConfig[] z;
    
    public OffsetNoise() {
    }
    
    public OffsetNoise(@Nullable final NoiseConfig[] x, @Nullable final NoiseConfig[] y, @Nullable final NoiseConfig[] z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    
    public OffsetNoise(@Nonnull final OffsetNoise other) {
        this.x = other.x;
        this.y = other.y;
        this.z = other.z;
    }
    
    @Nonnull
    public static OffsetNoise deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final OffsetNoise obj = new OffsetNoise();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 13 + buf.getIntLE(offset + 1);
            final int xCount = VarInt.peek(buf, varPos0);
            if (xCount < 0) {
                throw ProtocolException.negativeLength("X", xCount);
            }
            if (xCount > 4096000) {
                throw ProtocolException.arrayTooLong("X", xCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + xCount * 23L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("X", varPos0 + varIntLen + xCount * 23, buf.readableBytes());
            }
            obj.x = new NoiseConfig[xCount];
            int elemPos = varPos0 + varIntLen;
            for (int i = 0; i < xCount; ++i) {
                obj.x[i] = NoiseConfig.deserialize(buf, elemPos);
                elemPos += NoiseConfig.computeBytesConsumed(buf, elemPos);
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 13 + buf.getIntLE(offset + 5);
            final int yCount = VarInt.peek(buf, varPos2);
            if (yCount < 0) {
                throw ProtocolException.negativeLength("Y", yCount);
            }
            if (yCount > 4096000) {
                throw ProtocolException.arrayTooLong("Y", yCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + yCount * 23L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Y", varPos2 + varIntLen + yCount * 23, buf.readableBytes());
            }
            obj.y = new NoiseConfig[yCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < yCount; ++i) {
                obj.y[i] = NoiseConfig.deserialize(buf, elemPos);
                elemPos += NoiseConfig.computeBytesConsumed(buf, elemPos);
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 13 + buf.getIntLE(offset + 9);
            final int zCount = VarInt.peek(buf, varPos3);
            if (zCount < 0) {
                throw ProtocolException.negativeLength("Z", zCount);
            }
            if (zCount > 4096000) {
                throw ProtocolException.arrayTooLong("Z", zCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            if (varPos3 + varIntLen + zCount * 23L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Z", varPos3 + varIntLen + zCount * 23, buf.readableBytes());
            }
            obj.z = new NoiseConfig[zCount];
            int elemPos = varPos3 + varIntLen;
            for (int i = 0; i < zCount; ++i) {
                obj.z[i] = NoiseConfig.deserialize(buf, elemPos);
                elemPos += NoiseConfig.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 13;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 13 + fieldOffset0;
            final int arrLen = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0);
            for (int i = 0; i < arrLen; ++i) {
                pos0 += NoiseConfig.computeBytesConsumed(buf, pos0);
            }
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 5);
            int pos2 = offset + 13 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                pos2 += NoiseConfig.computeBytesConsumed(buf, pos2);
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 9);
            int pos3 = offset + 13 + fieldOffset3;
            final int arrLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3);
            for (int i = 0; i < arrLen; ++i) {
                pos3 += NoiseConfig.computeBytesConsumed(buf, pos3);
            }
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.x != null) {
            nullBits |= 0x1;
        }
        if (this.y != null) {
            nullBits |= 0x2;
        }
        if (this.z != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        final int xOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int yOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int zOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.x != null) {
            buf.setIntLE(xOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.x.length > 4096000) {
                throw ProtocolException.arrayTooLong("X", this.x.length, 4096000);
            }
            VarInt.write(buf, this.x.length);
            for (final NoiseConfig item : this.x) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(xOffsetSlot, -1);
        }
        if (this.y != null) {
            buf.setIntLE(yOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.y.length > 4096000) {
                throw ProtocolException.arrayTooLong("Y", this.y.length, 4096000);
            }
            VarInt.write(buf, this.y.length);
            for (final NoiseConfig item : this.y) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(yOffsetSlot, -1);
        }
        if (this.z != null) {
            buf.setIntLE(zOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.z.length > 4096000) {
                throw ProtocolException.arrayTooLong("Z", this.z.length, 4096000);
            }
            VarInt.write(buf, this.z.length);
            for (final NoiseConfig item : this.z) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(zOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 13;
        if (this.x != null) {
            size += VarInt.size(this.x.length) + this.x.length * 23;
        }
        if (this.y != null) {
            size += VarInt.size(this.y.length) + this.y.length * 23;
        }
        if (this.z != null) {
            size += VarInt.size(this.z.length) + this.z.length * 23;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 13) {
            return ValidationResult.error("Buffer too small: expected at least 13 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int xOffset = buffer.getIntLE(offset + 1);
            if (xOffset < 0) {
                return ValidationResult.error("Invalid offset for X");
            }
            int pos = offset + 13 + xOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for X");
            }
            final int xCount = VarInt.peek(buffer, pos);
            if (xCount < 0) {
                return ValidationResult.error("Invalid array count for X");
            }
            if (xCount > 4096000) {
                return ValidationResult.error("X exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += xCount * 23;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading X");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int yOffset = buffer.getIntLE(offset + 5);
            if (yOffset < 0) {
                return ValidationResult.error("Invalid offset for Y");
            }
            int pos = offset + 13 + yOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Y");
            }
            final int yCount = VarInt.peek(buffer, pos);
            if (yCount < 0) {
                return ValidationResult.error("Invalid array count for Y");
            }
            if (yCount > 4096000) {
                return ValidationResult.error("Y exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += yCount * 23;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Y");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int zOffset = buffer.getIntLE(offset + 9);
            if (zOffset < 0) {
                return ValidationResult.error("Invalid offset for Z");
            }
            int pos = offset + 13 + zOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Z");
            }
            final int zCount = VarInt.peek(buffer, pos);
            if (zCount < 0) {
                return ValidationResult.error("Invalid array count for Z");
            }
            if (zCount > 4096000) {
                return ValidationResult.error("Z exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += zCount * 23;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Z");
            }
        }
        return ValidationResult.OK;
    }
    
    public OffsetNoise clone() {
        final OffsetNoise copy = new OffsetNoise();
        copy.x = (NoiseConfig[])((this.x != null) ? ((NoiseConfig[])Arrays.stream(this.x).map(e -> e.clone()).toArray(NoiseConfig[]::new)) : null);
        copy.y = (NoiseConfig[])((this.y != null) ? ((NoiseConfig[])Arrays.stream(this.y).map(e -> e.clone()).toArray(NoiseConfig[]::new)) : null);
        copy.z = (NoiseConfig[])((this.z != null) ? ((NoiseConfig[])Arrays.stream(this.z).map(e -> e.clone()).toArray(NoiseConfig[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final OffsetNoise other) {
            return Arrays.equals(this.x, other.x) && Arrays.equals(this.y, other.y) && Arrays.equals(this.z, other.z);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Arrays.hashCode(this.x);
        result = 31 * result + Arrays.hashCode(this.y);
        result = 31 * result + Arrays.hashCode(this.z);
        return result;
    }
}
