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

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

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 com.hypixel.hytale.protocol.Packet;

public class SyncInteractionChains implements Packet
{
    public static final int PACKET_ID = 290;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 0;
    public static final int FIXED_BLOCK_SIZE = 0;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 0;
    public static final int MAX_SIZE = 1677721600;
    @Nonnull
    public SyncInteractionChain[] updates;
    
    @Override
    public int getId() {
        return 290;
    }
    
    public SyncInteractionChains() {
        this.updates = new SyncInteractionChain[0];
    }
    
    public SyncInteractionChains(@Nonnull final SyncInteractionChain[] updates) {
        this.updates = new SyncInteractionChain[0];
        this.updates = updates;
    }
    
    public SyncInteractionChains(@Nonnull final SyncInteractionChains other) {
        this.updates = new SyncInteractionChain[0];
        this.updates = other.updates;
    }
    
    @Nonnull
    public static SyncInteractionChains deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final SyncInteractionChains obj = new SyncInteractionChains();
        int pos = offset + 0;
        final int updatesCount = VarInt.peek(buf, pos);
        if (updatesCount < 0) {
            throw ProtocolException.negativeLength("Updates", updatesCount);
        }
        if (updatesCount > 4096000) {
            throw ProtocolException.arrayTooLong("Updates", updatesCount, 4096000);
        }
        final int updatesVarLen = VarInt.size(updatesCount);
        if (pos + updatesVarLen + updatesCount * 33L > buf.readableBytes()) {
            throw ProtocolException.bufferTooSmall("Updates", pos + updatesVarLen + updatesCount * 33, buf.readableBytes());
        }
        pos += updatesVarLen;
        obj.updates = new SyncInteractionChain[updatesCount];
        for (int i = 0; i < updatesCount; ++i) {
            obj.updates[i] = SyncInteractionChain.deserialize(buf, pos);
            pos += SyncInteractionChain.computeBytesConsumed(buf, pos);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        int pos = offset + 0;
        final int arrLen = VarInt.peek(buf, pos);
        pos += VarInt.length(buf, pos);
        for (int i = 0; i < arrLen; ++i) {
            pos += SyncInteractionChain.computeBytesConsumed(buf, pos);
        }
        return pos - offset;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        if (this.updates.length > 4096000) {
            throw ProtocolException.arrayTooLong("Updates", this.updates.length, 4096000);
        }
        VarInt.write(buf, this.updates.length);
        for (final SyncInteractionChain item : this.updates) {
            item.serialize(buf);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 0;
        int updatesSize = 0;
        for (final SyncInteractionChain elem : this.updates) {
            updatesSize += elem.computeSize();
        }
        size += VarInt.size(this.updates.length) + updatesSize;
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 0) {
            return ValidationResult.error("Buffer too small: expected at least 0 bytes");
        }
        int pos = offset + 0;
        final int updatesCount = VarInt.peek(buffer, pos);
        if (updatesCount < 0) {
            return ValidationResult.error("Invalid array count for Updates");
        }
        if (updatesCount > 4096000) {
            return ValidationResult.error("Updates exceeds max length 4096000");
        }
        pos += VarInt.length(buffer, pos);
        for (int i = 0; i < updatesCount; ++i) {
            final ValidationResult structResult = SyncInteractionChain.validateStructure(buffer, pos);
            if (!structResult.isValid()) {
                return ValidationResult.error("Invalid SyncInteractionChain in Updates[" + i + "]: " + structResult.error());
            }
            pos += SyncInteractionChain.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public SyncInteractionChains clone() {
        final SyncInteractionChains copy = new SyncInteractionChains();
        copy.updates = Arrays.stream(this.updates).map(e -> e.clone()).toArray(SyncInteractionChain[]::new);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final SyncInteractionChains other) {
            return Arrays.equals(this.updates, other.updates);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Arrays.hashCode(this.updates);
        return result;
    }
}
