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

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

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 AssetEditorFetchAutoCompleteData implements Packet
{
    public static final int PACKET_ID = 331;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 5;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 13;
    public static final int MAX_SIZE = 32768023;
    public int token;
    @Nullable
    public String dataset;
    @Nullable
    public String query;
    
    @Override
    public int getId() {
        return 331;
    }
    
    public AssetEditorFetchAutoCompleteData() {
    }
    
    public AssetEditorFetchAutoCompleteData(final int token, @Nullable final String dataset, @Nullable final String query) {
        this.token = token;
        this.dataset = dataset;
        this.query = query;
    }
    
    public AssetEditorFetchAutoCompleteData(@Nonnull final AssetEditorFetchAutoCompleteData other) {
        this.token = other.token;
        this.dataset = other.dataset;
        this.query = other.query;
    }
    
    @Nonnull
    public static AssetEditorFetchAutoCompleteData deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetEditorFetchAutoCompleteData obj = new AssetEditorFetchAutoCompleteData();
        final byte nullBits = buf.getByte(offset);
        obj.token = buf.getIntLE(offset + 1);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 13 + buf.getIntLE(offset + 5);
            final int datasetLen = VarInt.peek(buf, varPos0);
            if (datasetLen < 0) {
                throw ProtocolException.negativeLength("Dataset", datasetLen);
            }
            if (datasetLen > 4096000) {
                throw ProtocolException.stringTooLong("Dataset", datasetLen, 4096000);
            }
            obj.dataset = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 13 + buf.getIntLE(offset + 9);
            final int queryLen = VarInt.peek(buf, varPos2);
            if (queryLen < 0) {
                throw ProtocolException.negativeLength("Query", queryLen);
            }
            if (queryLen > 4096000) {
                throw ProtocolException.stringTooLong("Query", queryLen, 4096000);
            }
            obj.query = 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 = 13;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 5);
            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 + 9);
            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;
            }
        }
        return maxEnd;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.dataset != null) {
            nullBits |= 0x1;
        }
        if (this.query != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.token);
        final int datasetOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int queryOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.dataset != null) {
            buf.setIntLE(datasetOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.dataset, 4096000);
        }
        else {
            buf.setIntLE(datasetOffsetSlot, -1);
        }
        if (this.query != null) {
            buf.setIntLE(queryOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.query, 4096000);
        }
        else {
            buf.setIntLE(queryOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 13;
        if (this.dataset != null) {
            size += PacketIO.stringSize(this.dataset);
        }
        if (this.query != null) {
            size += PacketIO.stringSize(this.query);
        }
        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 datasetOffset = buffer.getIntLE(offset + 5);
            if (datasetOffset < 0) {
                return ValidationResult.error("Invalid offset for Dataset");
            }
            int pos = offset + 13 + datasetOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Dataset");
            }
            final int datasetLen = VarInt.peek(buffer, pos);
            if (datasetLen < 0) {
                return ValidationResult.error("Invalid string length for Dataset");
            }
            if (datasetLen > 4096000) {
                return ValidationResult.error("Dataset exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += datasetLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Dataset");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int queryOffset = buffer.getIntLE(offset + 9);
            if (queryOffset < 0) {
                return ValidationResult.error("Invalid offset for Query");
            }
            int pos = offset + 13 + queryOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Query");
            }
            final int queryLen = VarInt.peek(buffer, pos);
            if (queryLen < 0) {
                return ValidationResult.error("Invalid string length for Query");
            }
            if (queryLen > 4096000) {
                return ValidationResult.error("Query exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += queryLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Query");
            }
        }
        return ValidationResult.OK;
    }
    
    public AssetEditorFetchAutoCompleteData clone() {
        final AssetEditorFetchAutoCompleteData copy = new AssetEditorFetchAutoCompleteData();
        copy.token = this.token;
        copy.dataset = this.dataset;
        copy.query = this.query;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetEditorFetchAutoCompleteData other) {
            return this.token == other.token && Objects.equals(this.dataset, other.dataset) && Objects.equals(this.query, other.query);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.token, this.dataset, this.query);
    }
}
