// 
// 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 AssetEditorAssetListUpdate implements Packet
{
    public static final int PACKET_ID = 320;
    public static final boolean IS_COMPRESSED = true;
    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 = 1677721600;
    @Nullable
    public String pack;
    @Nullable
    public AssetEditorFileEntry[] additions;
    @Nullable
    public AssetEditorFileEntry[] deletions;
    
    @Override
    public int getId() {
        return 320;
    }
    
    public AssetEditorAssetListUpdate() {
    }
    
    public AssetEditorAssetListUpdate(@Nullable final String pack, @Nullable final AssetEditorFileEntry[] additions, @Nullable final AssetEditorFileEntry[] deletions) {
        this.pack = pack;
        this.additions = additions;
        this.deletions = deletions;
    }
    
    public AssetEditorAssetListUpdate(@Nonnull final AssetEditorAssetListUpdate other) {
        this.pack = other.pack;
        this.additions = other.additions;
        this.deletions = other.deletions;
    }
    
    @Nonnull
    public static AssetEditorAssetListUpdate deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetEditorAssetListUpdate obj = new AssetEditorAssetListUpdate();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 13 + buf.getIntLE(offset + 1);
            final int packLen = VarInt.peek(buf, varPos0);
            if (packLen < 0) {
                throw ProtocolException.negativeLength("Pack", packLen);
            }
            if (packLen > 4096000) {
                throw ProtocolException.stringTooLong("Pack", packLen, 4096000);
            }
            obj.pack = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 13 + buf.getIntLE(offset + 5);
            final int additionsCount = VarInt.peek(buf, varPos2);
            if (additionsCount < 0) {
                throw ProtocolException.negativeLength("Additions", additionsCount);
            }
            if (additionsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Additions", additionsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + additionsCount * 2L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Additions", varPos2 + varIntLen + additionsCount * 2, buf.readableBytes());
            }
            obj.additions = new AssetEditorFileEntry[additionsCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < additionsCount; ++i) {
                obj.additions[i] = AssetEditorFileEntry.deserialize(buf, elemPos);
                elemPos += AssetEditorFileEntry.computeBytesConsumed(buf, elemPos);
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 13 + buf.getIntLE(offset + 9);
            final int deletionsCount = VarInt.peek(buf, varPos3);
            if (deletionsCount < 0) {
                throw ProtocolException.negativeLength("Deletions", deletionsCount);
            }
            if (deletionsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Deletions", deletionsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            if (varPos3 + varIntLen + deletionsCount * 2L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Deletions", varPos3 + varIntLen + deletionsCount * 2, buf.readableBytes());
            }
            obj.deletions = new AssetEditorFileEntry[deletionsCount];
            int elemPos = varPos3 + varIntLen;
            for (int i = 0; i < deletionsCount; ++i) {
                obj.deletions[i] = AssetEditorFileEntry.deserialize(buf, elemPos);
                elemPos += AssetEditorFileEntry.computeBytesConsumed(buf, elemPos);
            }
        }
        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 arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                pos2 += AssetEditorFileEntry.computeBytesConsumed(buf, pos2);
            }
            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 arrLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3);
            for (int i = 0; i < arrLen; ++i) {
                pos3 += AssetEditorFileEntry.computeBytesConsumed(buf, pos3);
            }
            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.pack != null) {
            nullBits |= 0x1;
        }
        if (this.additions != null) {
            nullBits |= 0x2;
        }
        if (this.deletions != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        final int packOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int additionsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int deletionsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.pack != null) {
            buf.setIntLE(packOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.pack, 4096000);
        }
        else {
            buf.setIntLE(packOffsetSlot, -1);
        }
        if (this.additions != null) {
            buf.setIntLE(additionsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.additions.length > 4096000) {
                throw ProtocolException.arrayTooLong("Additions", this.additions.length, 4096000);
            }
            VarInt.write(buf, this.additions.length);
            for (final AssetEditorFileEntry item : this.additions) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(additionsOffsetSlot, -1);
        }
        if (this.deletions != null) {
            buf.setIntLE(deletionsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.deletions.length > 4096000) {
                throw ProtocolException.arrayTooLong("Deletions", this.deletions.length, 4096000);
            }
            VarInt.write(buf, this.deletions.length);
            for (final AssetEditorFileEntry item : this.deletions) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(deletionsOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 13;
        if (this.pack != null) {
            size += PacketIO.stringSize(this.pack);
        }
        if (this.additions != null) {
            int additionsSize = 0;
            for (final AssetEditorFileEntry elem : this.additions) {
                additionsSize += elem.computeSize();
            }
            size += VarInt.size(this.additions.length) + additionsSize;
        }
        if (this.deletions != null) {
            int deletionsSize = 0;
            for (final AssetEditorFileEntry elem : this.deletions) {
                deletionsSize += elem.computeSize();
            }
            size += VarInt.size(this.deletions.length) + deletionsSize;
        }
        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 packOffset = buffer.getIntLE(offset + 1);
            if (packOffset < 0) {
                return ValidationResult.error("Invalid offset for Pack");
            }
            int pos = offset + 13 + packOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Pack");
            }
            final int packLen = VarInt.peek(buffer, pos);
            if (packLen < 0) {
                return ValidationResult.error("Invalid string length for Pack");
            }
            if (packLen > 4096000) {
                return ValidationResult.error("Pack exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += packLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Pack");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int additionsOffset = buffer.getIntLE(offset + 5);
            if (additionsOffset < 0) {
                return ValidationResult.error("Invalid offset for Additions");
            }
            int pos = offset + 13 + additionsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Additions");
            }
            final int additionsCount = VarInt.peek(buffer, pos);
            if (additionsCount < 0) {
                return ValidationResult.error("Invalid array count for Additions");
            }
            if (additionsCount > 4096000) {
                return ValidationResult.error("Additions exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < additionsCount; ++i) {
                final ValidationResult structResult = AssetEditorFileEntry.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid AssetEditorFileEntry in Additions[" + i + "]: " + structResult.error());
                }
                pos += AssetEditorFileEntry.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int deletionsOffset = buffer.getIntLE(offset + 9);
            if (deletionsOffset < 0) {
                return ValidationResult.error("Invalid offset for Deletions");
            }
            int pos = offset + 13 + deletionsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Deletions");
            }
            final int deletionsCount = VarInt.peek(buffer, pos);
            if (deletionsCount < 0) {
                return ValidationResult.error("Invalid array count for Deletions");
            }
            if (deletionsCount > 4096000) {
                return ValidationResult.error("Deletions exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < deletionsCount; ++i) {
                final ValidationResult structResult = AssetEditorFileEntry.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid AssetEditorFileEntry in Deletions[" + i + "]: " + structResult.error());
                }
                pos += AssetEditorFileEntry.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public AssetEditorAssetListUpdate clone() {
        final AssetEditorAssetListUpdate copy = new AssetEditorAssetListUpdate();
        copy.pack = this.pack;
        copy.additions = (AssetEditorFileEntry[])((this.additions != null) ? ((AssetEditorFileEntry[])Arrays.stream(this.additions).map(e -> e.clone()).toArray(AssetEditorFileEntry[]::new)) : null);
        copy.deletions = (AssetEditorFileEntry[])((this.deletions != null) ? ((AssetEditorFileEntry[])Arrays.stream(this.deletions).map(e -> e.clone()).toArray(AssetEditorFileEntry[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetEditorAssetListUpdate other) {
            return Objects.equals(this.pack, other.pack) && Arrays.equals(this.additions, other.additions) && Arrays.equals(this.deletions, other.deletions);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.pack);
        result = 31 * result + Arrays.hashCode(this.additions);
        result = 31 * result + Arrays.hashCode(this.deletions);
        return result;
    }
}
