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

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

import java.util.Objects;
import java.util.Arrays;
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 AssetEditorRequestDatasetReply implements Packet
{
    public static final int PACKET_ID = 334;
    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 = 2;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String name;
    @Nullable
    public String[] ids;
    
    @Override
    public int getId() {
        return 334;
    }
    
    public AssetEditorRequestDatasetReply() {
    }
    
    public AssetEditorRequestDatasetReply(@Nullable final String name, @Nullable final String[] ids) {
        this.name = name;
        this.ids = ids;
    }
    
    public AssetEditorRequestDatasetReply(@Nonnull final AssetEditorRequestDatasetReply other) {
        this.name = other.name;
        this.ids = other.ids;
    }
    
    @Nonnull
    public static AssetEditorRequestDatasetReply deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetEditorRequestDatasetReply obj = new AssetEditorRequestDatasetReply();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 9 + buf.getIntLE(offset + 1);
            final int nameLen = VarInt.peek(buf, varPos0);
            if (nameLen < 0) {
                throw ProtocolException.negativeLength("Name", nameLen);
            }
            if (nameLen > 4096000) {
                throw ProtocolException.stringTooLong("Name", nameLen, 4096000);
            }
            obj.name = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 9 + buf.getIntLE(offset + 5);
            final int idsCount = VarInt.peek(buf, varPos2);
            if (idsCount < 0) {
                throw ProtocolException.negativeLength("Ids", idsCount);
            }
            if (idsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Ids", idsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + idsCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Ids", varPos2 + varIntLen + idsCount * 1, buf.readableBytes());
            }
            obj.ids = new String[idsCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < idsCount; ++i) {
                final int strLen = VarInt.peek(buf, elemPos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("ids[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("ids[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, elemPos);
                obj.ids[i] = PacketIO.readVarString(buf, elemPos);
                elemPos += strVarLen + strLen;
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 9;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 9 + 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 + 9 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                final int sl2 = VarInt.peek(buf, pos2);
                pos2 += VarInt.length(buf, pos2) + sl2;
            }
            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.name != null) {
            nullBits |= 0x1;
        }
        if (this.ids != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        final int nameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int idsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.name != null) {
            buf.setIntLE(nameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.name, 4096000);
        }
        else {
            buf.setIntLE(nameOffsetSlot, -1);
        }
        if (this.ids != null) {
            buf.setIntLE(idsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.ids.length > 4096000) {
                throw ProtocolException.arrayTooLong("Ids", this.ids.length, 4096000);
            }
            VarInt.write(buf, this.ids.length);
            for (final String item : this.ids) {
                PacketIO.writeVarString(buf, item, 4096000);
            }
        }
        else {
            buf.setIntLE(idsOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 9;
        if (this.name != null) {
            size += PacketIO.stringSize(this.name);
        }
        if (this.ids != null) {
            int idsSize = 0;
            for (final String elem : this.ids) {
                idsSize += PacketIO.stringSize(elem);
            }
            size += VarInt.size(this.ids.length) + idsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 9) {
            return ValidationResult.error("Buffer too small: expected at least 9 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int nameOffset = buffer.getIntLE(offset + 1);
            if (nameOffset < 0) {
                return ValidationResult.error("Invalid offset for Name");
            }
            int pos = offset + 9 + nameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Name");
            }
            final int nameLen = VarInt.peek(buffer, pos);
            if (nameLen < 0) {
                return ValidationResult.error("Invalid string length for Name");
            }
            if (nameLen > 4096000) {
                return ValidationResult.error("Name exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += nameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Name");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int idsOffset = buffer.getIntLE(offset + 5);
            if (idsOffset < 0) {
                return ValidationResult.error("Invalid offset for Ids");
            }
            int pos = offset + 9 + idsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Ids");
            }
            final int idsCount = VarInt.peek(buffer, pos);
            if (idsCount < 0) {
                return ValidationResult.error("Invalid array count for Ids");
            }
            if (idsCount > 4096000) {
                return ValidationResult.error("Ids exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < idsCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in Ids");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in Ids");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public AssetEditorRequestDatasetReply clone() {
        final AssetEditorRequestDatasetReply copy = new AssetEditorRequestDatasetReply();
        copy.name = this.name;
        copy.ids = (String[])((this.ids != null) ? ((String[])Arrays.copyOf(this.ids, this.ids.length)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetEditorRequestDatasetReply other) {
            return Objects.equals(this.name, other.name) && Arrays.equals(this.ids, other.ids);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.name);
        result = 31 * result + Arrays.hashCode(this.ids);
        return result;
    }
}
