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

package com.hypixel.hytale.protocol.packets.player;

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 com.hypixel.hytale.protocol.Vector3f;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;
import com.hypixel.hytale.protocol.DebugShape;
import com.hypixel.hytale.protocol.Packet;

public class DisplayDebug implements Packet
{
    public static final int PACKET_ID = 114;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 19;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 27;
    public static final int MAX_SIZE = 32768037;
    @Nonnull
    public DebugShape shape;
    @Nullable
    public float[] matrix;
    @Nullable
    public Vector3f color;
    public float time;
    public boolean fade;
    @Nullable
    public float[] frustumProjection;
    
    @Override
    public int getId() {
        return 114;
    }
    
    public DisplayDebug() {
        this.shape = DebugShape.Sphere;
    }
    
    public DisplayDebug(@Nonnull final DebugShape shape, @Nullable final float[] matrix, @Nullable final Vector3f color, final float time, final boolean fade, @Nullable final float[] frustumProjection) {
        this.shape = DebugShape.Sphere;
        this.shape = shape;
        this.matrix = matrix;
        this.color = color;
        this.time = time;
        this.fade = fade;
        this.frustumProjection = frustumProjection;
    }
    
    public DisplayDebug(@Nonnull final DisplayDebug other) {
        this.shape = DebugShape.Sphere;
        this.shape = other.shape;
        this.matrix = other.matrix;
        this.color = other.color;
        this.time = other.time;
        this.fade = other.fade;
        this.frustumProjection = other.frustumProjection;
    }
    
    @Nonnull
    public static DisplayDebug deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final DisplayDebug obj = new DisplayDebug();
        final byte nullBits = buf.getByte(offset);
        obj.shape = DebugShape.fromValue(buf.getByte(offset + 1));
        if ((nullBits & 0x1) != 0x0) {
            obj.color = Vector3f.deserialize(buf, offset + 2);
        }
        obj.time = buf.getFloatLE(offset + 14);
        obj.fade = (buf.getByte(offset + 18) != 0);
        if ((nullBits & 0x2) != 0x0) {
            final int varPos0 = offset + 27 + buf.getIntLE(offset + 19);
            final int matrixCount = VarInt.peek(buf, varPos0);
            if (matrixCount < 0) {
                throw ProtocolException.negativeLength("Matrix", matrixCount);
            }
            if (matrixCount > 4096000) {
                throw ProtocolException.arrayTooLong("Matrix", matrixCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + matrixCount * 4L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Matrix", varPos0 + varIntLen + matrixCount * 4, buf.readableBytes());
            }
            obj.matrix = new float[matrixCount];
            for (int i = 0; i < matrixCount; ++i) {
                obj.matrix[i] = buf.getFloatLE(varPos0 + varIntLen + i * 4);
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos2 = offset + 27 + buf.getIntLE(offset + 23);
            final int frustumProjectionCount = VarInt.peek(buf, varPos2);
            if (frustumProjectionCount < 0) {
                throw ProtocolException.negativeLength("FrustumProjection", frustumProjectionCount);
            }
            if (frustumProjectionCount > 4096000) {
                throw ProtocolException.arrayTooLong("FrustumProjection", frustumProjectionCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + frustumProjectionCount * 4L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("FrustumProjection", varPos2 + varIntLen + frustumProjectionCount * 4, buf.readableBytes());
            }
            obj.frustumProjection = new float[frustumProjectionCount];
            for (int i = 0; i < frustumProjectionCount; ++i) {
                obj.frustumProjection[i] = buf.getFloatLE(varPos2 + varIntLen + i * 4);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 27;
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 19);
            int pos0 = offset + 27 + fieldOffset0;
            final int arrLen = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + arrLen * 4;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 23);
            int pos2 = offset + 27 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + arrLen * 4;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.color != null) {
            nullBits |= 0x1;
        }
        if (this.matrix != null) {
            nullBits |= 0x2;
        }
        if (this.frustumProjection != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.shape.getValue());
        if (this.color != null) {
            this.color.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        buf.writeFloatLE(this.time);
        buf.writeByte(this.fade ? 1 : 0);
        final int matrixOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int frustumProjectionOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.matrix != null) {
            buf.setIntLE(matrixOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.matrix.length > 4096000) {
                throw ProtocolException.arrayTooLong("Matrix", this.matrix.length, 4096000);
            }
            VarInt.write(buf, this.matrix.length);
            for (final float item : this.matrix) {
                buf.writeFloatLE(item);
            }
        }
        else {
            buf.setIntLE(matrixOffsetSlot, -1);
        }
        if (this.frustumProjection != null) {
            buf.setIntLE(frustumProjectionOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.frustumProjection.length > 4096000) {
                throw ProtocolException.arrayTooLong("FrustumProjection", this.frustumProjection.length, 4096000);
            }
            VarInt.write(buf, this.frustumProjection.length);
            for (final float item : this.frustumProjection) {
                buf.writeFloatLE(item);
            }
        }
        else {
            buf.setIntLE(frustumProjectionOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 27;
        if (this.matrix != null) {
            size += VarInt.size(this.matrix.length) + this.matrix.length * 4;
        }
        if (this.frustumProjection != null) {
            size += VarInt.size(this.frustumProjection.length) + this.frustumProjection.length * 4;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 27) {
            return ValidationResult.error("Buffer too small: expected at least 27 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x2) != 0x0) {
            final int matrixOffset = buffer.getIntLE(offset + 19);
            if (matrixOffset < 0) {
                return ValidationResult.error("Invalid offset for Matrix");
            }
            int pos = offset + 27 + matrixOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Matrix");
            }
            final int matrixCount = VarInt.peek(buffer, pos);
            if (matrixCount < 0) {
                return ValidationResult.error("Invalid array count for Matrix");
            }
            if (matrixCount > 4096000) {
                return ValidationResult.error("Matrix exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += matrixCount * 4;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Matrix");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int frustumProjectionOffset = buffer.getIntLE(offset + 23);
            if (frustumProjectionOffset < 0) {
                return ValidationResult.error("Invalid offset for FrustumProjection");
            }
            int pos = offset + 27 + frustumProjectionOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for FrustumProjection");
            }
            final int frustumProjectionCount = VarInt.peek(buffer, pos);
            if (frustumProjectionCount < 0) {
                return ValidationResult.error("Invalid array count for FrustumProjection");
            }
            if (frustumProjectionCount > 4096000) {
                return ValidationResult.error("FrustumProjection exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += frustumProjectionCount * 4;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading FrustumProjection");
            }
        }
        return ValidationResult.OK;
    }
    
    public DisplayDebug clone() {
        final DisplayDebug copy = new DisplayDebug();
        copy.shape = this.shape;
        copy.matrix = (float[])((this.matrix != null) ? Arrays.copyOf(this.matrix, this.matrix.length) : null);
        copy.color = ((this.color != null) ? this.color.clone() : null);
        copy.time = this.time;
        copy.fade = this.fade;
        copy.frustumProjection = (float[])((this.frustumProjection != null) ? Arrays.copyOf(this.frustumProjection, this.frustumProjection.length) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final DisplayDebug other) {
            return Objects.equals(this.shape, other.shape) && Arrays.equals(this.matrix, other.matrix) && Objects.equals(this.color, other.color) && this.time == other.time && this.fade == other.fade && Arrays.equals(this.frustumProjection, other.frustumProjection);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.shape);
        result = 31 * result + Arrays.hashCode(this.matrix);
        result = 31 * result + Objects.hashCode(this.color);
        result = 31 * result + Float.hashCode(this.time);
        result = 31 * result + Boolean.hashCode(this.fade);
        result = 31 * result + Arrays.hashCode(this.frustumProjection);
        return result;
    }
}
