// 
// 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.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class CameraAxis
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 9;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 4096014;
    @Nullable
    public Rangef angleRange;
    @Nullable
    public CameraNode[] targetNodes;
    
    public CameraAxis() {
    }
    
    public CameraAxis(@Nullable final Rangef angleRange, @Nullable final CameraNode[] targetNodes) {
        this.angleRange = angleRange;
        this.targetNodes = targetNodes;
    }
    
    public CameraAxis(@Nonnull final CameraAxis other) {
        this.angleRange = other.angleRange;
        this.targetNodes = other.targetNodes;
    }
    
    @Nonnull
    public static CameraAxis deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final CameraAxis obj = new CameraAxis();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.angleRange = Rangef.deserialize(buf, offset + 1);
        }
        int pos = offset + 9;
        if ((nullBits & 0x2) != 0x0) {
            final int targetNodesCount = VarInt.peek(buf, pos);
            if (targetNodesCount < 0) {
                throw ProtocolException.negativeLength("TargetNodes", targetNodesCount);
            }
            if (targetNodesCount > 4096000) {
                throw ProtocolException.arrayTooLong("TargetNodes", targetNodesCount, 4096000);
            }
            final int targetNodesVarLen = VarInt.size(targetNodesCount);
            if (pos + targetNodesVarLen + targetNodesCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("TargetNodes", pos + targetNodesVarLen + targetNodesCount * 1, buf.readableBytes());
            }
            pos += targetNodesVarLen;
            obj.targetNodes = new CameraNode[targetNodesCount];
            for (int i = 0; i < targetNodesCount; ++i) {
                obj.targetNodes[i] = CameraNode.fromValue(buf.getByte(pos));
                ++pos;
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 9;
        if ((nullBits & 0x2) != 0x0) {
            final int arrLen = VarInt.peek(buf, pos);
            pos += VarInt.length(buf, pos) + arrLen * 1;
        }
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.angleRange != null) {
            nullBits |= 0x1;
        }
        if (this.targetNodes != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        if (this.angleRange != null) {
            this.angleRange.serialize(buf);
        }
        else {
            buf.writeZero(8);
        }
        if (this.targetNodes != null) {
            if (this.targetNodes.length > 4096000) {
                throw ProtocolException.arrayTooLong("TargetNodes", this.targetNodes.length, 4096000);
            }
            VarInt.write(buf, this.targetNodes.length);
            for (final CameraNode item : this.targetNodes) {
                buf.writeByte(item.getValue());
            }
        }
    }
    
    public int computeSize() {
        int size = 9;
        if (this.targetNodes != null) {
            size += VarInt.size(this.targetNodes.length) + this.targetNodes.length * 1;
        }
        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);
        int pos = offset + 9;
        if ((nullBits & 0x2) != 0x0) {
            final int targetNodesCount = VarInt.peek(buffer, pos);
            if (targetNodesCount < 0) {
                return ValidationResult.error("Invalid array count for TargetNodes");
            }
            if (targetNodesCount > 4096000) {
                return ValidationResult.error("TargetNodes exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += targetNodesCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading TargetNodes");
            }
        }
        return ValidationResult.OK;
    }
    
    public CameraAxis clone() {
        final CameraAxis copy = new CameraAxis();
        copy.angleRange = ((this.angleRange != null) ? this.angleRange.clone() : null);
        copy.targetNodes = (CameraNode[])((this.targetNodes != null) ? ((CameraNode[])Arrays.copyOf(this.targetNodes, this.targetNodes.length)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final CameraAxis other) {
            return Objects.equals(this.angleRange, other.angleRange) && Arrays.equals(this.targetNodes, other.targetNodes);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.angleRange);
        result = 31 * result + Arrays.hashCode(this.targetNodes);
        return result;
    }
}
