// 
// 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 AssetEditorUpdateJsonAsset implements Packet
{
    public static final int PACKET_ID = 323;
    public static final boolean IS_COMPRESSED = true;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 9;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 21;
    public static final int MAX_SIZE = 1677721600;
    public int token;
    @Nullable
    public String assetType;
    @Nullable
    public AssetPath path;
    public int assetIndex;
    @Nullable
    public JsonUpdateCommand[] commands;
    
    @Override
    public int getId() {
        return 323;
    }
    
    public AssetEditorUpdateJsonAsset() {
        this.assetIndex = Integer.MIN_VALUE;
    }
    
    public AssetEditorUpdateJsonAsset(final int token, @Nullable final String assetType, @Nullable final AssetPath path, final int assetIndex, @Nullable final JsonUpdateCommand[] commands) {
        this.assetIndex = Integer.MIN_VALUE;
        this.token = token;
        this.assetType = assetType;
        this.path = path;
        this.assetIndex = assetIndex;
        this.commands = commands;
    }
    
    public AssetEditorUpdateJsonAsset(@Nonnull final AssetEditorUpdateJsonAsset other) {
        this.assetIndex = Integer.MIN_VALUE;
        this.token = other.token;
        this.assetType = other.assetType;
        this.path = other.path;
        this.assetIndex = other.assetIndex;
        this.commands = other.commands;
    }
    
    @Nonnull
    public static AssetEditorUpdateJsonAsset deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetEditorUpdateJsonAsset obj = new AssetEditorUpdateJsonAsset();
        final byte nullBits = buf.getByte(offset);
        obj.token = buf.getIntLE(offset + 1);
        obj.assetIndex = buf.getIntLE(offset + 5);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 21 + buf.getIntLE(offset + 9);
            final int assetTypeLen = VarInt.peek(buf, varPos0);
            if (assetTypeLen < 0) {
                throw ProtocolException.negativeLength("AssetType", assetTypeLen);
            }
            if (assetTypeLen > 4096000) {
                throw ProtocolException.stringTooLong("AssetType", assetTypeLen, 4096000);
            }
            obj.assetType = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 21 + buf.getIntLE(offset + 13);
            obj.path = AssetPath.deserialize(buf, varPos2);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 21 + buf.getIntLE(offset + 17);
            final int commandsCount = VarInt.peek(buf, varPos3);
            if (commandsCount < 0) {
                throw ProtocolException.negativeLength("Commands", commandsCount);
            }
            if (commandsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Commands", commandsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            if (varPos3 + varIntLen + commandsCount * 7L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Commands", varPos3 + varIntLen + commandsCount * 7, buf.readableBytes());
            }
            obj.commands = new JsonUpdateCommand[commandsCount];
            int elemPos = varPos3 + varIntLen;
            for (int i = 0; i < commandsCount; ++i) {
                obj.commands[i] = JsonUpdateCommand.deserialize(buf, elemPos);
                elemPos += JsonUpdateCommand.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 21;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 9);
            int pos0 = offset + 21 + 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 + 13);
            int pos2 = offset + 21 + fieldOffset2;
            pos2 += AssetPath.computeBytesConsumed(buf, pos2);
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 17);
            int pos3 = offset + 21 + fieldOffset3;
            final int arrLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3);
            for (int i = 0; i < arrLen; ++i) {
                pos3 += JsonUpdateCommand.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.assetType != null) {
            nullBits |= 0x1;
        }
        if (this.path != null) {
            nullBits |= 0x2;
        }
        if (this.commands != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.token);
        buf.writeIntLE(this.assetIndex);
        final int assetTypeOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int pathOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int commandsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.assetType != null) {
            buf.setIntLE(assetTypeOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.assetType, 4096000);
        }
        else {
            buf.setIntLE(assetTypeOffsetSlot, -1);
        }
        if (this.path != null) {
            buf.setIntLE(pathOffsetSlot, buf.writerIndex() - varBlockStart);
            this.path.serialize(buf);
        }
        else {
            buf.setIntLE(pathOffsetSlot, -1);
        }
        if (this.commands != null) {
            buf.setIntLE(commandsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.commands.length > 4096000) {
                throw ProtocolException.arrayTooLong("Commands", this.commands.length, 4096000);
            }
            VarInt.write(buf, this.commands.length);
            for (final JsonUpdateCommand item : this.commands) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(commandsOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 21;
        if (this.assetType != null) {
            size += PacketIO.stringSize(this.assetType);
        }
        if (this.path != null) {
            size += this.path.computeSize();
        }
        if (this.commands != null) {
            int commandsSize = 0;
            for (final JsonUpdateCommand elem : this.commands) {
                commandsSize += elem.computeSize();
            }
            size += VarInt.size(this.commands.length) + commandsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 21) {
            return ValidationResult.error("Buffer too small: expected at least 21 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int assetTypeOffset = buffer.getIntLE(offset + 9);
            if (assetTypeOffset < 0) {
                return ValidationResult.error("Invalid offset for AssetType");
            }
            int pos = offset + 21 + assetTypeOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for AssetType");
            }
            final int assetTypeLen = VarInt.peek(buffer, pos);
            if (assetTypeLen < 0) {
                return ValidationResult.error("Invalid string length for AssetType");
            }
            if (assetTypeLen > 4096000) {
                return ValidationResult.error("AssetType exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += assetTypeLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading AssetType");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int pathOffset = buffer.getIntLE(offset + 13);
            if (pathOffset < 0) {
                return ValidationResult.error("Invalid offset for Path");
            }
            int pos = offset + 21 + pathOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Path");
            }
            final ValidationResult pathResult = AssetPath.validateStructure(buffer, pos);
            if (!pathResult.isValid()) {
                return ValidationResult.error("Invalid Path: " + pathResult.error());
            }
            pos += AssetPath.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int commandsOffset = buffer.getIntLE(offset + 17);
            if (commandsOffset < 0) {
                return ValidationResult.error("Invalid offset for Commands");
            }
            int pos = offset + 21 + commandsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Commands");
            }
            final int commandsCount = VarInt.peek(buffer, pos);
            if (commandsCount < 0) {
                return ValidationResult.error("Invalid array count for Commands");
            }
            if (commandsCount > 4096000) {
                return ValidationResult.error("Commands exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < commandsCount; ++i) {
                final ValidationResult structResult = JsonUpdateCommand.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid JsonUpdateCommand in Commands[" + i + "]: " + structResult.error());
                }
                pos += JsonUpdateCommand.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public AssetEditorUpdateJsonAsset clone() {
        final AssetEditorUpdateJsonAsset copy = new AssetEditorUpdateJsonAsset();
        copy.token = this.token;
        copy.assetType = this.assetType;
        copy.path = ((this.path != null) ? this.path.clone() : null);
        copy.assetIndex = this.assetIndex;
        copy.commands = (JsonUpdateCommand[])((this.commands != null) ? ((JsonUpdateCommand[])Arrays.stream(this.commands).map(e -> e.clone()).toArray(JsonUpdateCommand[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetEditorUpdateJsonAsset other) {
            return this.token == other.token && Objects.equals(this.assetType, other.assetType) && Objects.equals(this.path, other.path) && this.assetIndex == other.assetIndex && Arrays.equals(this.commands, other.commands);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Integer.hashCode(this.token);
        result = 31 * result + Objects.hashCode(this.assetType);
        result = 31 * result + Objects.hashCode(this.path);
        result = 31 * result + Integer.hashCode(this.assetIndex);
        result = 31 * result + Arrays.hashCode(this.commands);
        return result;
    }
}
