// 
// 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 AssetEditorSetupSchemas implements Packet
{
    public static final int PACKET_ID = 305;
    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 = 1;
    public static final int VARIABLE_BLOCK_START = 1;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public SchemaFile[] schemas;
    
    @Override
    public int getId() {
        return 305;
    }
    
    public AssetEditorSetupSchemas() {
    }
    
    public AssetEditorSetupSchemas(@Nullable final SchemaFile[] schemas) {
        this.schemas = schemas;
    }
    
    public AssetEditorSetupSchemas(@Nonnull final AssetEditorSetupSchemas other) {
        this.schemas = other.schemas;
    }
    
    @Nonnull
    public static AssetEditorSetupSchemas deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetEditorSetupSchemas obj = new AssetEditorSetupSchemas();
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 1;
        if ((nullBits & 0x1) != 0x0) {
            final int schemasCount = VarInt.peek(buf, pos);
            if (schemasCount < 0) {
                throw ProtocolException.negativeLength("Schemas", schemasCount);
            }
            if (schemasCount > 4096000) {
                throw ProtocolException.arrayTooLong("Schemas", schemasCount, 4096000);
            }
            final int schemasVarLen = VarInt.size(schemasCount);
            if (pos + schemasVarLen + schemasCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Schemas", pos + schemasVarLen + schemasCount * 1, buf.readableBytes());
            }
            pos += schemasVarLen;
            obj.schemas = new SchemaFile[schemasCount];
            for (int i = 0; i < schemasCount; ++i) {
                obj.schemas[i] = SchemaFile.deserialize(buf, pos);
                pos += SchemaFile.computeBytesConsumed(buf, pos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 1;
        if ((nullBits & 0x1) != 0x0) {
            final int arrLen = VarInt.peek(buf, pos);
            pos += VarInt.length(buf, pos);
            for (int i = 0; i < arrLen; ++i) {
                pos += SchemaFile.computeBytesConsumed(buf, pos);
            }
        }
        return pos - offset;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.schemas != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        if (this.schemas != null) {
            if (this.schemas.length > 4096000) {
                throw ProtocolException.arrayTooLong("Schemas", this.schemas.length, 4096000);
            }
            VarInt.write(buf, this.schemas.length);
            for (final SchemaFile item : this.schemas) {
                item.serialize(buf);
            }
        }
    }
    
    @Override
    public int computeSize() {
        int size = 1;
        if (this.schemas != null) {
            int schemasSize = 0;
            for (final SchemaFile elem : this.schemas) {
                schemasSize += elem.computeSize();
            }
            size += VarInt.size(this.schemas.length) + schemasSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 1) {
            return ValidationResult.error("Buffer too small: expected at least 1 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 1;
        if ((nullBits & 0x1) != 0x0) {
            final int schemasCount = VarInt.peek(buffer, pos);
            if (schemasCount < 0) {
                return ValidationResult.error("Invalid array count for Schemas");
            }
            if (schemasCount > 4096000) {
                return ValidationResult.error("Schemas exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < schemasCount; ++i) {
                final ValidationResult structResult = SchemaFile.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid SchemaFile in Schemas[" + i + "]: " + structResult.error());
                }
                pos += SchemaFile.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public AssetEditorSetupSchemas clone() {
        final AssetEditorSetupSchemas copy = new AssetEditorSetupSchemas();
        copy.schemas = (SchemaFile[])((this.schemas != null) ? ((SchemaFile[])Arrays.stream(this.schemas).map(e -> e.clone()).toArray(SchemaFile[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetEditorSetupSchemas other) {
            return Arrays.equals(this.schemas, other.schemas);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Arrays.hashCode(this.schemas);
        return result;
    }
}
