// 
// 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.Nullable;
import javax.annotation.Nonnull;

public class JsonUpdateCommand
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 7;
    public static final int VARIABLE_FIELD_COUNT = 4;
    public static final int VARIABLE_BLOCK_START = 23;
    public static final int MAX_SIZE = 1677721600;
    @Nonnull
    public JsonUpdateType type;
    @Nullable
    public String[] path;
    @Nullable
    public String value;
    @Nullable
    public String previousValue;
    @Nullable
    public String[] firstCreatedProperty;
    @Nullable
    public AssetEditorRebuildCaches rebuildCaches;
    
    public JsonUpdateCommand() {
        this.type = JsonUpdateType.SetProperty;
    }
    
    public JsonUpdateCommand(@Nonnull final JsonUpdateType type, @Nullable final String[] path, @Nullable final String value, @Nullable final String previousValue, @Nullable final String[] firstCreatedProperty, @Nullable final AssetEditorRebuildCaches rebuildCaches) {
        this.type = JsonUpdateType.SetProperty;
        this.type = type;
        this.path = path;
        this.value = value;
        this.previousValue = previousValue;
        this.firstCreatedProperty = firstCreatedProperty;
        this.rebuildCaches = rebuildCaches;
    }
    
    public JsonUpdateCommand(@Nonnull final JsonUpdateCommand other) {
        this.type = JsonUpdateType.SetProperty;
        this.type = other.type;
        this.path = other.path;
        this.value = other.value;
        this.previousValue = other.previousValue;
        this.firstCreatedProperty = other.firstCreatedProperty;
        this.rebuildCaches = other.rebuildCaches;
    }
    
    @Nonnull
    public static JsonUpdateCommand deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final JsonUpdateCommand obj = new JsonUpdateCommand();
        final byte nullBits = buf.getByte(offset);
        obj.type = JsonUpdateType.fromValue(buf.getByte(offset + 1));
        if ((nullBits & 0x1) != 0x0) {
            obj.rebuildCaches = AssetEditorRebuildCaches.deserialize(buf, offset + 2);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos0 = offset + 23 + buf.getIntLE(offset + 7);
            final int pathCount = VarInt.peek(buf, varPos0);
            if (pathCount < 0) {
                throw ProtocolException.negativeLength("Path", pathCount);
            }
            if (pathCount > 4096000) {
                throw ProtocolException.arrayTooLong("Path", pathCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + pathCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Path", varPos0 + varIntLen + pathCount * 1, buf.readableBytes());
            }
            obj.path = new String[pathCount];
            int elemPos = varPos0 + varIntLen;
            for (int i = 0; i < pathCount; ++i) {
                final int strLen = VarInt.peek(buf, elemPos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("path[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("path[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, elemPos);
                obj.path[i] = PacketIO.readVarString(buf, elemPos);
                elemPos += strVarLen + strLen;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos2 = offset + 23 + buf.getIntLE(offset + 11);
            final int valueLen = VarInt.peek(buf, varPos2);
            if (valueLen < 0) {
                throw ProtocolException.negativeLength("Value", valueLen);
            }
            if (valueLen > 4096000) {
                throw ProtocolException.stringTooLong("Value", valueLen, 4096000);
            }
            obj.value = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos3 = offset + 23 + buf.getIntLE(offset + 15);
            final int previousValueLen = VarInt.peek(buf, varPos3);
            if (previousValueLen < 0) {
                throw ProtocolException.negativeLength("PreviousValue", previousValueLen);
            }
            if (previousValueLen > 4096000) {
                throw ProtocolException.stringTooLong("PreviousValue", previousValueLen, 4096000);
            }
            obj.previousValue = PacketIO.readVarString(buf, varPos3, PacketIO.UTF8);
        }
        if ((nullBits & 0x10) != 0x0) {
            final int varPos4 = offset + 23 + buf.getIntLE(offset + 19);
            final int firstCreatedPropertyCount = VarInt.peek(buf, varPos4);
            if (firstCreatedPropertyCount < 0) {
                throw ProtocolException.negativeLength("FirstCreatedProperty", firstCreatedPropertyCount);
            }
            if (firstCreatedPropertyCount > 4096000) {
                throw ProtocolException.arrayTooLong("FirstCreatedProperty", firstCreatedPropertyCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos4);
            if (varPos4 + varIntLen + firstCreatedPropertyCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("FirstCreatedProperty", varPos4 + varIntLen + firstCreatedPropertyCount * 1, buf.readableBytes());
            }
            obj.firstCreatedProperty = new String[firstCreatedPropertyCount];
            int elemPos = varPos4 + varIntLen;
            for (int i = 0; i < firstCreatedPropertyCount; ++i) {
                final int strLen = VarInt.peek(buf, elemPos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("firstCreatedProperty[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("firstCreatedProperty[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, elemPos);
                obj.firstCreatedProperty[i] = PacketIO.readVarString(buf, elemPos);
                elemPos += strVarLen + strLen;
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 23;
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 7);
            int pos0 = offset + 23 + fieldOffset0;
            final int arrLen = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0);
            for (int i = 0; i < arrLen; ++i) {
                final int sl = VarInt.peek(buf, pos0);
                pos0 += VarInt.length(buf, pos0) + sl;
            }
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 11);
            int pos2 = offset + 23 + fieldOffset2;
            final int sl2 = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl2;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 15);
            int pos3 = offset + 23 + fieldOffset3;
            final int sl2 = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + sl2;
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 19);
            int pos4 = offset + 23 + fieldOffset4;
            final int arrLen = VarInt.peek(buf, pos4);
            pos4 += VarInt.length(buf, pos4);
            for (int i = 0; i < arrLen; ++i) {
                final int sl = VarInt.peek(buf, pos4);
                pos4 += VarInt.length(buf, pos4) + sl;
            }
            if (pos4 - offset > maxEnd) {
                maxEnd = pos4 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.rebuildCaches != null) {
            nullBits |= 0x1;
        }
        if (this.path != null) {
            nullBits |= 0x2;
        }
        if (this.value != null) {
            nullBits |= 0x4;
        }
        if (this.previousValue != null) {
            nullBits |= 0x8;
        }
        if (this.firstCreatedProperty != null) {
            nullBits |= 0x10;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.type.getValue());
        if (this.rebuildCaches != null) {
            this.rebuildCaches.serialize(buf);
        }
        else {
            buf.writeZero(5);
        }
        final int pathOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int valueOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int previousValueOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int firstCreatedPropertyOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.path != null) {
            buf.setIntLE(pathOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.path.length > 4096000) {
                throw ProtocolException.arrayTooLong("Path", this.path.length, 4096000);
            }
            VarInt.write(buf, this.path.length);
            for (final String item : this.path) {
                PacketIO.writeVarString(buf, item, 4096000);
            }
        }
        else {
            buf.setIntLE(pathOffsetSlot, -1);
        }
        if (this.value != null) {
            buf.setIntLE(valueOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.value, 4096000);
        }
        else {
            buf.setIntLE(valueOffsetSlot, -1);
        }
        if (this.previousValue != null) {
            buf.setIntLE(previousValueOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.previousValue, 4096000);
        }
        else {
            buf.setIntLE(previousValueOffsetSlot, -1);
        }
        if (this.firstCreatedProperty != null) {
            buf.setIntLE(firstCreatedPropertyOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.firstCreatedProperty.length > 4096000) {
                throw ProtocolException.arrayTooLong("FirstCreatedProperty", this.firstCreatedProperty.length, 4096000);
            }
            VarInt.write(buf, this.firstCreatedProperty.length);
            for (final String item : this.firstCreatedProperty) {
                PacketIO.writeVarString(buf, item, 4096000);
            }
        }
        else {
            buf.setIntLE(firstCreatedPropertyOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 23;
        if (this.path != null) {
            int pathSize = 0;
            for (final String elem : this.path) {
                pathSize += PacketIO.stringSize(elem);
            }
            size += VarInt.size(this.path.length) + pathSize;
        }
        if (this.value != null) {
            size += PacketIO.stringSize(this.value);
        }
        if (this.previousValue != null) {
            size += PacketIO.stringSize(this.previousValue);
        }
        if (this.firstCreatedProperty != null) {
            int firstCreatedPropertySize = 0;
            for (final String elem : this.firstCreatedProperty) {
                firstCreatedPropertySize += PacketIO.stringSize(elem);
            }
            size += VarInt.size(this.firstCreatedProperty.length) + firstCreatedPropertySize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 23) {
            return ValidationResult.error("Buffer too small: expected at least 23 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x2) != 0x0) {
            final int pathOffset = buffer.getIntLE(offset + 7);
            if (pathOffset < 0) {
                return ValidationResult.error("Invalid offset for Path");
            }
            int pos = offset + 23 + pathOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Path");
            }
            final int pathCount = VarInt.peek(buffer, pos);
            if (pathCount < 0) {
                return ValidationResult.error("Invalid array count for Path");
            }
            if (pathCount > 4096000) {
                return ValidationResult.error("Path exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < pathCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in Path");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in Path");
                }
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int valueOffset = buffer.getIntLE(offset + 11);
            if (valueOffset < 0) {
                return ValidationResult.error("Invalid offset for Value");
            }
            int pos = offset + 23 + valueOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Value");
            }
            final int valueLen = VarInt.peek(buffer, pos);
            if (valueLen < 0) {
                return ValidationResult.error("Invalid string length for Value");
            }
            if (valueLen > 4096000) {
                return ValidationResult.error("Value exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += valueLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Value");
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int previousValueOffset = buffer.getIntLE(offset + 15);
            if (previousValueOffset < 0) {
                return ValidationResult.error("Invalid offset for PreviousValue");
            }
            int pos = offset + 23 + previousValueOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for PreviousValue");
            }
            final int previousValueLen = VarInt.peek(buffer, pos);
            if (previousValueLen < 0) {
                return ValidationResult.error("Invalid string length for PreviousValue");
            }
            if (previousValueLen > 4096000) {
                return ValidationResult.error("PreviousValue exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += previousValueLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading PreviousValue");
            }
        }
        if ((nullBits & 0x10) != 0x0) {
            final int firstCreatedPropertyOffset = buffer.getIntLE(offset + 19);
            if (firstCreatedPropertyOffset < 0) {
                return ValidationResult.error("Invalid offset for FirstCreatedProperty");
            }
            int pos = offset + 23 + firstCreatedPropertyOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for FirstCreatedProperty");
            }
            final int firstCreatedPropertyCount = VarInt.peek(buffer, pos);
            if (firstCreatedPropertyCount < 0) {
                return ValidationResult.error("Invalid array count for FirstCreatedProperty");
            }
            if (firstCreatedPropertyCount > 4096000) {
                return ValidationResult.error("FirstCreatedProperty exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < firstCreatedPropertyCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in FirstCreatedProperty");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in FirstCreatedProperty");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public JsonUpdateCommand clone() {
        final JsonUpdateCommand copy = new JsonUpdateCommand();
        copy.type = this.type;
        copy.path = (String[])((this.path != null) ? ((String[])Arrays.copyOf(this.path, this.path.length)) : null);
        copy.value = this.value;
        copy.previousValue = this.previousValue;
        copy.firstCreatedProperty = (String[])((this.firstCreatedProperty != null) ? ((String[])Arrays.copyOf(this.firstCreatedProperty, this.firstCreatedProperty.length)) : null);
        copy.rebuildCaches = ((this.rebuildCaches != null) ? this.rebuildCaches.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final JsonUpdateCommand other) {
            return Objects.equals(this.type, other.type) && Arrays.equals(this.path, other.path) && Objects.equals(this.value, other.value) && Objects.equals(this.previousValue, other.previousValue) && Arrays.equals(this.firstCreatedProperty, other.firstCreatedProperty) && Objects.equals(this.rebuildCaches, other.rebuildCaches);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.type);
        result = 31 * result + Arrays.hashCode(this.path);
        result = 31 * result + Objects.hashCode(this.value);
        result = 31 * result + Objects.hashCode(this.previousValue);
        result = 31 * result + Arrays.hashCode(this.firstCreatedProperty);
        result = 31 * result + Objects.hashCode(this.rebuildCaches);
        return result;
    }
}
