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

package com.hypixel.hytale.protocol;

import java.util.Objects;
import com.hypixel.hytale.protocol.io.ValidationResult;
import com.hypixel.hytale.protocol.io.PacketIO;
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 ModelDisplay
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 37;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 45;
    public static final int MAX_SIZE = 32768055;
    @Nullable
    public String node;
    @Nullable
    public String attachTo;
    @Nullable
    public Vector3f translation;
    @Nullable
    public Vector3f rotation;
    @Nullable
    public Vector3f scale;
    
    public ModelDisplay() {
    }
    
    public ModelDisplay(@Nullable final String node, @Nullable final String attachTo, @Nullable final Vector3f translation, @Nullable final Vector3f rotation, @Nullable final Vector3f scale) {
        this.node = node;
        this.attachTo = attachTo;
        this.translation = translation;
        this.rotation = rotation;
        this.scale = scale;
    }
    
    public ModelDisplay(@Nonnull final ModelDisplay other) {
        this.node = other.node;
        this.attachTo = other.attachTo;
        this.translation = other.translation;
        this.rotation = other.rotation;
        this.scale = other.scale;
    }
    
    @Nonnull
    public static ModelDisplay deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ModelDisplay obj = new ModelDisplay();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.translation = Vector3f.deserialize(buf, offset + 1);
        }
        if ((nullBits & 0x2) != 0x0) {
            obj.rotation = Vector3f.deserialize(buf, offset + 13);
        }
        if ((nullBits & 0x4) != 0x0) {
            obj.scale = Vector3f.deserialize(buf, offset + 25);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos0 = offset + 45 + buf.getIntLE(offset + 37);
            final int nodeLen = VarInt.peek(buf, varPos0);
            if (nodeLen < 0) {
                throw ProtocolException.negativeLength("Node", nodeLen);
            }
            if (nodeLen > 4096000) {
                throw ProtocolException.stringTooLong("Node", nodeLen, 4096000);
            }
            obj.node = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos2 = offset + 45 + buf.getIntLE(offset + 41);
            final int attachToLen = VarInt.peek(buf, varPos2);
            if (attachToLen < 0) {
                throw ProtocolException.negativeLength("AttachTo", attachToLen);
            }
            if (attachToLen > 4096000) {
                throw ProtocolException.stringTooLong("AttachTo", attachToLen, 4096000);
            }
            obj.attachTo = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 45;
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 37);
            int pos0 = offset + 45 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 41);
            int pos2 = offset + 45 + fieldOffset2;
            final int sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            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.translation != null) {
            nullBits |= 0x1;
        }
        if (this.rotation != null) {
            nullBits |= 0x2;
        }
        if (this.scale != null) {
            nullBits |= 0x4;
        }
        if (this.node != null) {
            nullBits |= 0x8;
        }
        if (this.attachTo != null) {
            nullBits |= 0x10;
        }
        buf.writeByte(nullBits);
        if (this.translation != null) {
            this.translation.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        if (this.rotation != null) {
            this.rotation.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        if (this.scale != null) {
            this.scale.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        final int nodeOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int attachToOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.node != null) {
            buf.setIntLE(nodeOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.node, 4096000);
        }
        else {
            buf.setIntLE(nodeOffsetSlot, -1);
        }
        if (this.attachTo != null) {
            buf.setIntLE(attachToOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.attachTo, 4096000);
        }
        else {
            buf.setIntLE(attachToOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 45;
        if (this.node != null) {
            size += PacketIO.stringSize(this.node);
        }
        if (this.attachTo != null) {
            size += PacketIO.stringSize(this.attachTo);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 45) {
            return ValidationResult.error("Buffer too small: expected at least 45 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x8) != 0x0) {
            final int nodeOffset = buffer.getIntLE(offset + 37);
            if (nodeOffset < 0) {
                return ValidationResult.error("Invalid offset for Node");
            }
            int pos = offset + 45 + nodeOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Node");
            }
            final int nodeLen = VarInt.peek(buffer, pos);
            if (nodeLen < 0) {
                return ValidationResult.error("Invalid string length for Node");
            }
            if (nodeLen > 4096000) {
                return ValidationResult.error("Node exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += nodeLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Node");
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int attachToOffset = buffer.getIntLE(offset + 41);
            if (attachToOffset < 0) {
                return ValidationResult.error("Invalid offset for AttachTo");
            }
            int pos = offset + 45 + attachToOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for AttachTo");
            }
            final int attachToLen = VarInt.peek(buffer, pos);
            if (attachToLen < 0) {
                return ValidationResult.error("Invalid string length for AttachTo");
            }
            if (attachToLen > 4096000) {
                return ValidationResult.error("AttachTo exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += attachToLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading AttachTo");
            }
        }
        return ValidationResult.OK;
    }
    
    public ModelDisplay clone() {
        final ModelDisplay copy = new ModelDisplay();
        copy.node = this.node;
        copy.attachTo = this.attachTo;
        copy.translation = ((this.translation != null) ? this.translation.clone() : null);
        copy.rotation = ((this.rotation != null) ? this.rotation.clone() : null);
        copy.scale = ((this.scale != null) ? this.scale.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ModelDisplay other) {
            return Objects.equals(this.node, other.node) && Objects.equals(this.attachTo, other.attachTo) && Objects.equals(this.translation, other.translation) && Objects.equals(this.rotation, other.rotation) && Objects.equals(this.scale, other.scale);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.node, this.attachTo, this.translation, this.rotation, this.scale);
    }
}
