// 
// 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 RotationNoise
{
    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[] pitch;
    @Nullable
    public NoiseConfig[] yaw;
    @Nullable
    public NoiseConfig[] roll;
    
    public RotationNoise() {
    }
    
    public RotationNoise(@Nullable final NoiseConfig[] pitch, @Nullable final NoiseConfig[] yaw, @Nullable final NoiseConfig[] roll) {
        this.pitch = pitch;
        this.yaw = yaw;
        this.roll = roll;
    }
    
    public RotationNoise(@Nonnull final RotationNoise other) {
        this.pitch = other.pitch;
        this.yaw = other.yaw;
        this.roll = other.roll;
    }
    
    @Nonnull
    public static RotationNoise deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final RotationNoise obj = new RotationNoise();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 13 + buf.getIntLE(offset + 1);
            final int pitchCount = VarInt.peek(buf, varPos0);
            if (pitchCount < 0) {
                throw ProtocolException.negativeLength("Pitch", pitchCount);
            }
            if (pitchCount > 4096000) {
                throw ProtocolException.arrayTooLong("Pitch", pitchCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + pitchCount * 23L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Pitch", varPos0 + varIntLen + pitchCount * 23, buf.readableBytes());
            }
            obj.pitch = new NoiseConfig[pitchCount];
            int elemPos = varPos0 + varIntLen;
            for (int i = 0; i < pitchCount; ++i) {
                obj.pitch[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 yawCount = VarInt.peek(buf, varPos2);
            if (yawCount < 0) {
                throw ProtocolException.negativeLength("Yaw", yawCount);
            }
            if (yawCount > 4096000) {
                throw ProtocolException.arrayTooLong("Yaw", yawCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + yawCount * 23L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Yaw", varPos2 + varIntLen + yawCount * 23, buf.readableBytes());
            }
            obj.yaw = new NoiseConfig[yawCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < yawCount; ++i) {
                obj.yaw[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 rollCount = VarInt.peek(buf, varPos3);
            if (rollCount < 0) {
                throw ProtocolException.negativeLength("Roll", rollCount);
            }
            if (rollCount > 4096000) {
                throw ProtocolException.arrayTooLong("Roll", rollCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            if (varPos3 + varIntLen + rollCount * 23L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Roll", varPos3 + varIntLen + rollCount * 23, buf.readableBytes());
            }
            obj.roll = new NoiseConfig[rollCount];
            int elemPos = varPos3 + varIntLen;
            for (int i = 0; i < rollCount; ++i) {
                obj.roll[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.pitch != null) {
            nullBits |= 0x1;
        }
        if (this.yaw != null) {
            nullBits |= 0x2;
        }
        if (this.roll != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        final int pitchOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int yawOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int rollOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.pitch != null) {
            buf.setIntLE(pitchOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.pitch.length > 4096000) {
                throw ProtocolException.arrayTooLong("Pitch", this.pitch.length, 4096000);
            }
            VarInt.write(buf, this.pitch.length);
            for (final NoiseConfig item : this.pitch) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(pitchOffsetSlot, -1);
        }
        if (this.yaw != null) {
            buf.setIntLE(yawOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.yaw.length > 4096000) {
                throw ProtocolException.arrayTooLong("Yaw", this.yaw.length, 4096000);
            }
            VarInt.write(buf, this.yaw.length);
            for (final NoiseConfig item : this.yaw) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(yawOffsetSlot, -1);
        }
        if (this.roll != null) {
            buf.setIntLE(rollOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.roll.length > 4096000) {
                throw ProtocolException.arrayTooLong("Roll", this.roll.length, 4096000);
            }
            VarInt.write(buf, this.roll.length);
            for (final NoiseConfig item : this.roll) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(rollOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 13;
        if (this.pitch != null) {
            size += VarInt.size(this.pitch.length) + this.pitch.length * 23;
        }
        if (this.yaw != null) {
            size += VarInt.size(this.yaw.length) + this.yaw.length * 23;
        }
        if (this.roll != null) {
            size += VarInt.size(this.roll.length) + this.roll.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 pitchOffset = buffer.getIntLE(offset + 1);
            if (pitchOffset < 0) {
                return ValidationResult.error("Invalid offset for Pitch");
            }
            int pos = offset + 13 + pitchOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Pitch");
            }
            final int pitchCount = VarInt.peek(buffer, pos);
            if (pitchCount < 0) {
                return ValidationResult.error("Invalid array count for Pitch");
            }
            if (pitchCount > 4096000) {
                return ValidationResult.error("Pitch exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += pitchCount * 23;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Pitch");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int yawOffset = buffer.getIntLE(offset + 5);
            if (yawOffset < 0) {
                return ValidationResult.error("Invalid offset for Yaw");
            }
            int pos = offset + 13 + yawOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Yaw");
            }
            final int yawCount = VarInt.peek(buffer, pos);
            if (yawCount < 0) {
                return ValidationResult.error("Invalid array count for Yaw");
            }
            if (yawCount > 4096000) {
                return ValidationResult.error("Yaw exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += yawCount * 23;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Yaw");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int rollOffset = buffer.getIntLE(offset + 9);
            if (rollOffset < 0) {
                return ValidationResult.error("Invalid offset for Roll");
            }
            int pos = offset + 13 + rollOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Roll");
            }
            final int rollCount = VarInt.peek(buffer, pos);
            if (rollCount < 0) {
                return ValidationResult.error("Invalid array count for Roll");
            }
            if (rollCount > 4096000) {
                return ValidationResult.error("Roll exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += rollCount * 23;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Roll");
            }
        }
        return ValidationResult.OK;
    }
    
    public RotationNoise clone() {
        final RotationNoise copy = new RotationNoise();
        copy.pitch = (NoiseConfig[])((this.pitch != null) ? ((NoiseConfig[])Arrays.stream(this.pitch).map(e -> e.clone()).toArray(NoiseConfig[]::new)) : null);
        copy.yaw = (NoiseConfig[])((this.yaw != null) ? ((NoiseConfig[])Arrays.stream(this.yaw).map(e -> e.clone()).toArray(NoiseConfig[]::new)) : null);
        copy.roll = (NoiseConfig[])((this.roll != null) ? ((NoiseConfig[])Arrays.stream(this.roll).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 RotationNoise other) {
            return Arrays.equals(this.pitch, other.pitch) && Arrays.equals(this.yaw, other.yaw) && Arrays.equals(this.roll, other.roll);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Arrays.hashCode(this.pitch);
        result = 31 * result + Arrays.hashCode(this.yaw);
        result = 31 * result + Arrays.hashCode(this.roll);
        return result;
    }
}
