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

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

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

public class BuilderToolArgUpdate implements Packet
{
    public static final int PACKET_ID = 400;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 14;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 22;
    public static final int MAX_SIZE = 32768032;
    public int token;
    public int section;
    public int slot;
    @Nonnull
    public BuilderToolArgGroup group;
    @Nullable
    public String id;
    @Nullable
    public String value;
    
    @Override
    public int getId() {
        return 400;
    }
    
    public BuilderToolArgUpdate() {
        this.group = BuilderToolArgGroup.Tool;
    }
    
    public BuilderToolArgUpdate(final int token, final int section, final int slot, @Nonnull final BuilderToolArgGroup group, @Nullable final String id, @Nullable final String value) {
        this.group = BuilderToolArgGroup.Tool;
        this.token = token;
        this.section = section;
        this.slot = slot;
        this.group = group;
        this.id = id;
        this.value = value;
    }
    
    public BuilderToolArgUpdate(@Nonnull final BuilderToolArgUpdate other) {
        this.group = BuilderToolArgGroup.Tool;
        this.token = other.token;
        this.section = other.section;
        this.slot = other.slot;
        this.group = other.group;
        this.id = other.id;
        this.value = other.value;
    }
    
    @Nonnull
    public static BuilderToolArgUpdate deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BuilderToolArgUpdate obj = new BuilderToolArgUpdate();
        final byte nullBits = buf.getByte(offset);
        obj.token = buf.getIntLE(offset + 1);
        obj.section = buf.getIntLE(offset + 5);
        obj.slot = buf.getIntLE(offset + 9);
        obj.group = BuilderToolArgGroup.fromValue(buf.getByte(offset + 13));
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 22 + buf.getIntLE(offset + 14);
            final int idLen = VarInt.peek(buf, varPos0);
            if (idLen < 0) {
                throw ProtocolException.negativeLength("Id", idLen);
            }
            if (idLen > 4096000) {
                throw ProtocolException.stringTooLong("Id", idLen, 4096000);
            }
            obj.id = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 22 + buf.getIntLE(offset + 18);
            final int valueLen = VarInt.peek(buf, varPos2);
            if (valueLen < 0) {
                throw ProtocolException.negativeLength("Value", valueLen);
            }
            if (valueLen > 4096000) {
                throw ProtocolException.stringTooLong("Value", valueLen, 4096000);
            }
            obj.value = 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 = 22;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 14);
            int pos0 = offset + 22 + 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 + 18);
            int pos2 = offset + 22 + fieldOffset2;
            final int sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            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.id != null) {
            nullBits |= 0x1;
        }
        if (this.value != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.token);
        buf.writeIntLE(this.section);
        buf.writeIntLE(this.slot);
        buf.writeByte(this.group.getValue());
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int valueOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.id != null) {
            buf.setIntLE(idOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.id, 4096000);
        }
        else {
            buf.setIntLE(idOffsetSlot, -1);
        }
        if (this.value != null) {
            buf.setIntLE(valueOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.value, 4096000);
        }
        else {
            buf.setIntLE(valueOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 22;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.value != null) {
            size += PacketIO.stringSize(this.value);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 22) {
            return ValidationResult.error("Buffer too small: expected at least 22 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 14);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 22 + idOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Id");
            }
            final int idLen = VarInt.peek(buffer, pos);
            if (idLen < 0) {
                return ValidationResult.error("Invalid string length for Id");
            }
            if (idLen > 4096000) {
                return ValidationResult.error("Id exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += idLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Id");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int valueOffset = buffer.getIntLE(offset + 18);
            if (valueOffset < 0) {
                return ValidationResult.error("Invalid offset for Value");
            }
            int pos = offset + 22 + valueOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Value");
            }
            final int valueLen = VarInt.peek(buffer, pos);
            if (valueLen < 0) {
                return ValidationResult.error("Invalid string length for Value");
            }
            if (valueLen > 4096000) {
                return ValidationResult.error("Value exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += valueLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Value");
            }
        }
        return ValidationResult.OK;
    }
    
    public BuilderToolArgUpdate clone() {
        final BuilderToolArgUpdate copy = new BuilderToolArgUpdate();
        copy.token = this.token;
        copy.section = this.section;
        copy.slot = this.slot;
        copy.group = this.group;
        copy.id = this.id;
        copy.value = this.value;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BuilderToolArgUpdate other) {
            return this.token == other.token && this.section == other.section && this.slot == other.slot && Objects.equals(this.group, other.group) && Objects.equals(this.id, other.id) && Objects.equals(this.value, other.value);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.token, this.section, this.slot, this.group, this.id, this.value);
    }
}
