// 
// 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 ModelTrail
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 27;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 35;
    public static final int MAX_SIZE = 32768045;
    @Nullable
    public String trailId;
    @Nonnull
    public EntityPart targetEntityPart;
    @Nullable
    public String targetNodeName;
    @Nullable
    public Vector3f positionOffset;
    @Nullable
    public Direction rotationOffset;
    public boolean fixedRotation;
    
    public ModelTrail() {
        this.targetEntityPart = EntityPart.Self;
    }
    
    public ModelTrail(@Nullable final String trailId, @Nonnull final EntityPart targetEntityPart, @Nullable final String targetNodeName, @Nullable final Vector3f positionOffset, @Nullable final Direction rotationOffset, final boolean fixedRotation) {
        this.targetEntityPart = EntityPart.Self;
        this.trailId = trailId;
        this.targetEntityPart = targetEntityPart;
        this.targetNodeName = targetNodeName;
        this.positionOffset = positionOffset;
        this.rotationOffset = rotationOffset;
        this.fixedRotation = fixedRotation;
    }
    
    public ModelTrail(@Nonnull final ModelTrail other) {
        this.targetEntityPart = EntityPart.Self;
        this.trailId = other.trailId;
        this.targetEntityPart = other.targetEntityPart;
        this.targetNodeName = other.targetNodeName;
        this.positionOffset = other.positionOffset;
        this.rotationOffset = other.rotationOffset;
        this.fixedRotation = other.fixedRotation;
    }
    
    @Nonnull
    public static ModelTrail deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ModelTrail obj = new ModelTrail();
        final byte nullBits = buf.getByte(offset);
        obj.targetEntityPart = EntityPart.fromValue(buf.getByte(offset + 1));
        if ((nullBits & 0x1) != 0x0) {
            obj.positionOffset = Vector3f.deserialize(buf, offset + 2);
        }
        if ((nullBits & 0x2) != 0x0) {
            obj.rotationOffset = Direction.deserialize(buf, offset + 14);
        }
        obj.fixedRotation = (buf.getByte(offset + 26) != 0);
        if ((nullBits & 0x4) != 0x0) {
            final int varPos0 = offset + 35 + buf.getIntLE(offset + 27);
            final int trailIdLen = VarInt.peek(buf, varPos0);
            if (trailIdLen < 0) {
                throw ProtocolException.negativeLength("TrailId", trailIdLen);
            }
            if (trailIdLen > 4096000) {
                throw ProtocolException.stringTooLong("TrailId", trailIdLen, 4096000);
            }
            obj.trailId = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos2 = offset + 35 + buf.getIntLE(offset + 31);
            final int targetNodeNameLen = VarInt.peek(buf, varPos2);
            if (targetNodeNameLen < 0) {
                throw ProtocolException.negativeLength("TargetNodeName", targetNodeNameLen);
            }
            if (targetNodeNameLen > 4096000) {
                throw ProtocolException.stringTooLong("TargetNodeName", targetNodeNameLen, 4096000);
            }
            obj.targetNodeName = 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 = 35;
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 27);
            int pos0 = offset + 35 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 31);
            int pos2 = offset + 35 + 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.positionOffset != null) {
            nullBits |= 0x1;
        }
        if (this.rotationOffset != null) {
            nullBits |= 0x2;
        }
        if (this.trailId != null) {
            nullBits |= 0x4;
        }
        if (this.targetNodeName != null) {
            nullBits |= 0x8;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.targetEntityPart.getValue());
        if (this.positionOffset != null) {
            this.positionOffset.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        if (this.rotationOffset != null) {
            this.rotationOffset.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        buf.writeByte(this.fixedRotation ? 1 : 0);
        final int trailIdOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int targetNodeNameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.trailId != null) {
            buf.setIntLE(trailIdOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.trailId, 4096000);
        }
        else {
            buf.setIntLE(trailIdOffsetSlot, -1);
        }
        if (this.targetNodeName != null) {
            buf.setIntLE(targetNodeNameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.targetNodeName, 4096000);
        }
        else {
            buf.setIntLE(targetNodeNameOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 35;
        if (this.trailId != null) {
            size += PacketIO.stringSize(this.trailId);
        }
        if (this.targetNodeName != null) {
            size += PacketIO.stringSize(this.targetNodeName);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 35) {
            return ValidationResult.error("Buffer too small: expected at least 35 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x4) != 0x0) {
            final int trailIdOffset = buffer.getIntLE(offset + 27);
            if (trailIdOffset < 0) {
                return ValidationResult.error("Invalid offset for TrailId");
            }
            int pos = offset + 35 + trailIdOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for TrailId");
            }
            final int trailIdLen = VarInt.peek(buffer, pos);
            if (trailIdLen < 0) {
                return ValidationResult.error("Invalid string length for TrailId");
            }
            if (trailIdLen > 4096000) {
                return ValidationResult.error("TrailId exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += trailIdLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading TrailId");
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int targetNodeNameOffset = buffer.getIntLE(offset + 31);
            if (targetNodeNameOffset < 0) {
                return ValidationResult.error("Invalid offset for TargetNodeName");
            }
            int pos = offset + 35 + targetNodeNameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for TargetNodeName");
            }
            final int targetNodeNameLen = VarInt.peek(buffer, pos);
            if (targetNodeNameLen < 0) {
                return ValidationResult.error("Invalid string length for TargetNodeName");
            }
            if (targetNodeNameLen > 4096000) {
                return ValidationResult.error("TargetNodeName exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += targetNodeNameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading TargetNodeName");
            }
        }
        return ValidationResult.OK;
    }
    
    public ModelTrail clone() {
        final ModelTrail copy = new ModelTrail();
        copy.trailId = this.trailId;
        copy.targetEntityPart = this.targetEntityPart;
        copy.targetNodeName = this.targetNodeName;
        copy.positionOffset = ((this.positionOffset != null) ? this.positionOffset.clone() : null);
        copy.rotationOffset = ((this.rotationOffset != null) ? this.rotationOffset.clone() : null);
        copy.fixedRotation = this.fixedRotation;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ModelTrail other) {
            return Objects.equals(this.trailId, other.trailId) && Objects.equals(this.targetEntityPart, other.targetEntityPart) && Objects.equals(this.targetNodeName, other.targetNodeName) && Objects.equals(this.positionOffset, other.positionOffset) && Objects.equals(this.rotationOffset, other.rotationOffset) && this.fixedRotation == other.fixedRotation;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.trailId, this.targetEntityPart, this.targetNodeName, this.positionOffset, this.rotationOffset, this.fixedRotation);
    }
}
