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

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

import java.util.Arrays;
import com.hypixel.hytale.protocol.io.ValidationResult;
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 AssetEditorFetchAssetReply implements Packet
{
    public static final int PACKET_ID = 312;
    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 = 1;
    public static final int VARIABLE_BLOCK_START = 5;
    public static final int MAX_SIZE = 4096010;
    public int token;
    @Nullable
    public byte[] contents;
    
    @Override
    public int getId() {
        return 312;
    }
    
    public AssetEditorFetchAssetReply() {
    }
    
    public AssetEditorFetchAssetReply(final int token, @Nullable final byte[] contents) {
        this.token = token;
        this.contents = contents;
    }
    
    public AssetEditorFetchAssetReply(@Nonnull final AssetEditorFetchAssetReply other) {
        this.token = other.token;
        this.contents = other.contents;
    }
    
    @Nonnull
    public static AssetEditorFetchAssetReply deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetEditorFetchAssetReply obj = new AssetEditorFetchAssetReply();
        final byte nullBits = buf.getByte(offset);
        obj.token = buf.getIntLE(offset + 1);
        int pos = offset + 5;
        if ((nullBits & 0x1) != 0x0) {
            final int contentsCount = VarInt.peek(buf, pos);
            if (contentsCount < 0) {
                throw ProtocolException.negativeLength("Contents", contentsCount);
            }
            if (contentsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Contents", contentsCount, 4096000);
            }
            final int contentsVarLen = VarInt.size(contentsCount);
            if (pos + contentsVarLen + contentsCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Contents", pos + contentsVarLen + contentsCount * 1, buf.readableBytes());
            }
            pos += contentsVarLen;
            obj.contents = new byte[contentsCount];
            for (int i = 0; i < contentsCount; ++i) {
                obj.contents[i] = buf.getByte(pos + i * 1);
            }
            pos += contentsCount * 1;
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 5;
        if ((nullBits & 0x1) != 0x0) {
            final int arrLen = VarInt.peek(buf, pos);
            pos += VarInt.length(buf, pos) + arrLen * 1;
        }
        return pos - offset;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.contents != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.token);
        if (this.contents != null) {
            if (this.contents.length > 4096000) {
                throw ProtocolException.arrayTooLong("Contents", this.contents.length, 4096000);
            }
            VarInt.write(buf, this.contents.length);
            for (final byte item : this.contents) {
                buf.writeByte(item);
            }
        }
    }
    
    @Override
    public int computeSize() {
        int size = 5;
        if (this.contents != null) {
            size += VarInt.size(this.contents.length) + this.contents.length * 1;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 5) {
            return ValidationResult.error("Buffer too small: expected at least 5 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 5;
        if ((nullBits & 0x1) != 0x0) {
            final int contentsCount = VarInt.peek(buffer, pos);
            if (contentsCount < 0) {
                return ValidationResult.error("Invalid array count for Contents");
            }
            if (contentsCount > 4096000) {
                return ValidationResult.error("Contents exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += contentsCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Contents");
            }
        }
        return ValidationResult.OK;
    }
    
    public AssetEditorFetchAssetReply clone() {
        final AssetEditorFetchAssetReply copy = new AssetEditorFetchAssetReply();
        copy.token = this.token;
        copy.contents = (byte[])((this.contents != null) ? Arrays.copyOf(this.contents, this.contents.length) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetEditorFetchAssetReply other) {
            return this.token == other.token && Arrays.equals(this.contents, other.contents);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Integer.hashCode(this.token);
        result = 31 * result + Arrays.hashCode(this.contents);
        return result;
    }
}
