// 
// 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 InteractionCameraSettings
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 237568019;
    @Nullable
    public InteractionCamera[] firstPerson;
    @Nullable
    public InteractionCamera[] thirdPerson;
    
    public InteractionCameraSettings() {
    }
    
    public InteractionCameraSettings(@Nullable final InteractionCamera[] firstPerson, @Nullable final InteractionCamera[] thirdPerson) {
        this.firstPerson = firstPerson;
        this.thirdPerson = thirdPerson;
    }
    
    public InteractionCameraSettings(@Nonnull final InteractionCameraSettings other) {
        this.firstPerson = other.firstPerson;
        this.thirdPerson = other.thirdPerson;
    }
    
    @Nonnull
    public static InteractionCameraSettings deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final InteractionCameraSettings obj = new InteractionCameraSettings();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 9 + buf.getIntLE(offset + 1);
            final int firstPersonCount = VarInt.peek(buf, varPos0);
            if (firstPersonCount < 0) {
                throw ProtocolException.negativeLength("FirstPerson", firstPersonCount);
            }
            if (firstPersonCount > 4096000) {
                throw ProtocolException.arrayTooLong("FirstPerson", firstPersonCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + firstPersonCount * 29L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("FirstPerson", varPos0 + varIntLen + firstPersonCount * 29, buf.readableBytes());
            }
            obj.firstPerson = new InteractionCamera[firstPersonCount];
            int elemPos = varPos0 + varIntLen;
            for (int i = 0; i < firstPersonCount; ++i) {
                obj.firstPerson[i] = InteractionCamera.deserialize(buf, elemPos);
                elemPos += InteractionCamera.computeBytesConsumed(buf, elemPos);
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 9 + buf.getIntLE(offset + 5);
            final int thirdPersonCount = VarInt.peek(buf, varPos2);
            if (thirdPersonCount < 0) {
                throw ProtocolException.negativeLength("ThirdPerson", thirdPersonCount);
            }
            if (thirdPersonCount > 4096000) {
                throw ProtocolException.arrayTooLong("ThirdPerson", thirdPersonCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + thirdPersonCount * 29L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("ThirdPerson", varPos2 + varIntLen + thirdPersonCount * 29, buf.readableBytes());
            }
            obj.thirdPerson = new InteractionCamera[thirdPersonCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < thirdPersonCount; ++i) {
                obj.thirdPerson[i] = InteractionCamera.deserialize(buf, elemPos);
                elemPos += InteractionCamera.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 9;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 9 + fieldOffset0;
            final int arrLen = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0);
            for (int i = 0; i < arrLen; ++i) {
                pos0 += InteractionCamera.computeBytesConsumed(buf, pos0);
            }
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 5);
            int pos2 = offset + 9 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                pos2 += InteractionCamera.computeBytesConsumed(buf, pos2);
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.firstPerson != null) {
            nullBits |= 0x1;
        }
        if (this.thirdPerson != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        final int firstPersonOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int thirdPersonOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.firstPerson != null) {
            buf.setIntLE(firstPersonOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.firstPerson.length > 4096000) {
                throw ProtocolException.arrayTooLong("FirstPerson", this.firstPerson.length, 4096000);
            }
            VarInt.write(buf, this.firstPerson.length);
            for (final InteractionCamera item : this.firstPerson) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(firstPersonOffsetSlot, -1);
        }
        if (this.thirdPerson != null) {
            buf.setIntLE(thirdPersonOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.thirdPerson.length > 4096000) {
                throw ProtocolException.arrayTooLong("ThirdPerson", this.thirdPerson.length, 4096000);
            }
            VarInt.write(buf, this.thirdPerson.length);
            for (final InteractionCamera item : this.thirdPerson) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(thirdPersonOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 9;
        if (this.firstPerson != null) {
            size += VarInt.size(this.firstPerson.length) + this.firstPerson.length * 29;
        }
        if (this.thirdPerson != null) {
            size += VarInt.size(this.thirdPerson.length) + this.thirdPerson.length * 29;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 9) {
            return ValidationResult.error("Buffer too small: expected at least 9 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int firstPersonOffset = buffer.getIntLE(offset + 1);
            if (firstPersonOffset < 0) {
                return ValidationResult.error("Invalid offset for FirstPerson");
            }
            int pos = offset + 9 + firstPersonOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for FirstPerson");
            }
            final int firstPersonCount = VarInt.peek(buffer, pos);
            if (firstPersonCount < 0) {
                return ValidationResult.error("Invalid array count for FirstPerson");
            }
            if (firstPersonCount > 4096000) {
                return ValidationResult.error("FirstPerson exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += firstPersonCount * 29;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading FirstPerson");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int thirdPersonOffset = buffer.getIntLE(offset + 5);
            if (thirdPersonOffset < 0) {
                return ValidationResult.error("Invalid offset for ThirdPerson");
            }
            int pos = offset + 9 + thirdPersonOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ThirdPerson");
            }
            final int thirdPersonCount = VarInt.peek(buffer, pos);
            if (thirdPersonCount < 0) {
                return ValidationResult.error("Invalid array count for ThirdPerson");
            }
            if (thirdPersonCount > 4096000) {
                return ValidationResult.error("ThirdPerson exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += thirdPersonCount * 29;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading ThirdPerson");
            }
        }
        return ValidationResult.OK;
    }
    
    public InteractionCameraSettings clone() {
        final InteractionCameraSettings copy = new InteractionCameraSettings();
        copy.firstPerson = (InteractionCamera[])((this.firstPerson != null) ? ((InteractionCamera[])Arrays.stream(this.firstPerson).map(e -> e.clone()).toArray(InteractionCamera[]::new)) : null);
        copy.thirdPerson = (InteractionCamera[])((this.thirdPerson != null) ? ((InteractionCamera[])Arrays.stream(this.thirdPerson).map(e -> e.clone()).toArray(InteractionCamera[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final InteractionCameraSettings other) {
            return Arrays.equals(this.firstPerson, other.firstPerson) && Arrays.equals(this.thirdPerson, other.thirdPerson);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Arrays.hashCode(this.firstPerson);
        result = 31 * result + Arrays.hashCode(this.thirdPerson);
        return result;
    }
}
