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

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

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;
import com.hypixel.hytale.protocol.Packet;

public class RequestMachinimaActorModel implements Packet
{
    public static final int PACKET_ID = 260;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 13;
    public static final int MAX_SIZE = 49152028;
    @Nullable
    public String modelId;
    @Nullable
    public String sceneName;
    @Nullable
    public String actorName;
    
    @Override
    public int getId() {
        return 260;
    }
    
    public RequestMachinimaActorModel() {
    }
    
    public RequestMachinimaActorModel(@Nullable final String modelId, @Nullable final String sceneName, @Nullable final String actorName) {
        this.modelId = modelId;
        this.sceneName = sceneName;
        this.actorName = actorName;
    }
    
    public RequestMachinimaActorModel(@Nonnull final RequestMachinimaActorModel other) {
        this.modelId = other.modelId;
        this.sceneName = other.sceneName;
        this.actorName = other.actorName;
    }
    
    @Nonnull
    public static RequestMachinimaActorModel deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final RequestMachinimaActorModel obj = new RequestMachinimaActorModel();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 13 + buf.getIntLE(offset + 1);
            final int modelIdLen = VarInt.peek(buf, varPos0);
            if (modelIdLen < 0) {
                throw ProtocolException.negativeLength("ModelId", modelIdLen);
            }
            if (modelIdLen > 4096000) {
                throw ProtocolException.stringTooLong("ModelId", modelIdLen, 4096000);
            }
            obj.modelId = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 13 + buf.getIntLE(offset + 5);
            final int sceneNameLen = VarInt.peek(buf, varPos2);
            if (sceneNameLen < 0) {
                throw ProtocolException.negativeLength("SceneName", sceneNameLen);
            }
            if (sceneNameLen > 4096000) {
                throw ProtocolException.stringTooLong("SceneName", sceneNameLen, 4096000);
            }
            obj.sceneName = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 13 + buf.getIntLE(offset + 9);
            final int actorNameLen = VarInt.peek(buf, varPos3);
            if (actorNameLen < 0) {
                throw ProtocolException.negativeLength("ActorName", actorNameLen);
            }
            if (actorNameLen > 4096000) {
                throw ProtocolException.stringTooLong("ActorName", actorNameLen, 4096000);
            }
            obj.actorName = PacketIO.readVarString(buf, varPos3, PacketIO.UTF8);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 13;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 13 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 5);
            int pos2 = offset + 13 + fieldOffset2;
            final int sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 9);
            int pos3 = offset + 13 + fieldOffset3;
            final int sl = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + sl;
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.modelId != null) {
            nullBits |= 0x1;
        }
        if (this.sceneName != null) {
            nullBits |= 0x2;
        }
        if (this.actorName != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        final int modelIdOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int sceneNameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int actorNameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.modelId != null) {
            buf.setIntLE(modelIdOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.modelId, 4096000);
        }
        else {
            buf.setIntLE(modelIdOffsetSlot, -1);
        }
        if (this.sceneName != null) {
            buf.setIntLE(sceneNameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.sceneName, 4096000);
        }
        else {
            buf.setIntLE(sceneNameOffsetSlot, -1);
        }
        if (this.actorName != null) {
            buf.setIntLE(actorNameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.actorName, 4096000);
        }
        else {
            buf.setIntLE(actorNameOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 13;
        if (this.modelId != null) {
            size += PacketIO.stringSize(this.modelId);
        }
        if (this.sceneName != null) {
            size += PacketIO.stringSize(this.sceneName);
        }
        if (this.actorName != null) {
            size += PacketIO.stringSize(this.actorName);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 13) {
            return ValidationResult.error("Buffer too small: expected at least 13 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int modelIdOffset = buffer.getIntLE(offset + 1);
            if (modelIdOffset < 0) {
                return ValidationResult.error("Invalid offset for ModelId");
            }
            int pos = offset + 13 + modelIdOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ModelId");
            }
            final int modelIdLen = VarInt.peek(buffer, pos);
            if (modelIdLen < 0) {
                return ValidationResult.error("Invalid string length for ModelId");
            }
            if (modelIdLen > 4096000) {
                return ValidationResult.error("ModelId exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += modelIdLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading ModelId");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int sceneNameOffset = buffer.getIntLE(offset + 5);
            if (sceneNameOffset < 0) {
                return ValidationResult.error("Invalid offset for SceneName");
            }
            int pos = offset + 13 + sceneNameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for SceneName");
            }
            final int sceneNameLen = VarInt.peek(buffer, pos);
            if (sceneNameLen < 0) {
                return ValidationResult.error("Invalid string length for SceneName");
            }
            if (sceneNameLen > 4096000) {
                return ValidationResult.error("SceneName exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += sceneNameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading SceneName");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int actorNameOffset = buffer.getIntLE(offset + 9);
            if (actorNameOffset < 0) {
                return ValidationResult.error("Invalid offset for ActorName");
            }
            int pos = offset + 13 + actorNameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ActorName");
            }
            final int actorNameLen = VarInt.peek(buffer, pos);
            if (actorNameLen < 0) {
                return ValidationResult.error("Invalid string length for ActorName");
            }
            if (actorNameLen > 4096000) {
                return ValidationResult.error("ActorName exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += actorNameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading ActorName");
            }
        }
        return ValidationResult.OK;
    }
    
    public RequestMachinimaActorModel clone() {
        final RequestMachinimaActorModel copy = new RequestMachinimaActorModel();
        copy.modelId = this.modelId;
        copy.sceneName = this.sceneName;
        copy.actorName = this.actorName;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final RequestMachinimaActorModel other) {
            return Objects.equals(this.modelId, other.modelId) && Objects.equals(this.sceneName, other.sceneName) && Objects.equals(this.actorName, other.actorName);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.modelId, this.sceneName, this.actorName);
    }
}
