// 
// 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 AssetEditorAssetListSetup implements Packet
{
    public static final int PACKET_ID = 319;
    public static final boolean IS_COMPRESSED = true;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 4;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 12;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String pack;
    public boolean isReadOnly;
    public boolean canBeDeleted;
    @Nonnull
    public AssetEditorFileTree tree;
    @Nullable
    public AssetEditorFileEntry[] paths;
    
    @Override
    public int getId() {
        return 319;
    }
    
    public AssetEditorAssetListSetup() {
        this.tree = AssetEditorFileTree.Server;
    }
    
    public AssetEditorAssetListSetup(@Nullable final String pack, final boolean isReadOnly, final boolean canBeDeleted, @Nonnull final AssetEditorFileTree tree, @Nullable final AssetEditorFileEntry[] paths) {
        this.tree = AssetEditorFileTree.Server;
        this.pack = pack;
        this.isReadOnly = isReadOnly;
        this.canBeDeleted = canBeDeleted;
        this.tree = tree;
        this.paths = paths;
    }
    
    public AssetEditorAssetListSetup(@Nonnull final AssetEditorAssetListSetup other) {
        this.tree = AssetEditorFileTree.Server;
        this.pack = other.pack;
        this.isReadOnly = other.isReadOnly;
        this.canBeDeleted = other.canBeDeleted;
        this.tree = other.tree;
        this.paths = other.paths;
    }
    
    @Nonnull
    public static AssetEditorAssetListSetup deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetEditorAssetListSetup obj = new AssetEditorAssetListSetup();
        final byte nullBits = buf.getByte(offset);
        obj.isReadOnly = (buf.getByte(offset + 1) != 0);
        obj.canBeDeleted = (buf.getByte(offset + 2) != 0);
        obj.tree = AssetEditorFileTree.fromValue(buf.getByte(offset + 3));
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 12 + buf.getIntLE(offset + 4);
            final int packLen = VarInt.peek(buf, varPos0);
            if (packLen < 0) {
                throw ProtocolException.negativeLength("Pack", packLen);
            }
            if (packLen > 4096000) {
                throw ProtocolException.stringTooLong("Pack", packLen, 4096000);
            }
            obj.pack = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 12 + buf.getIntLE(offset + 8);
            final int pathsCount = VarInt.peek(buf, varPos2);
            if (pathsCount < 0) {
                throw ProtocolException.negativeLength("Paths", pathsCount);
            }
            if (pathsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Paths", pathsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + pathsCount * 2L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Paths", varPos2 + varIntLen + pathsCount * 2, buf.readableBytes());
            }
            obj.paths = new AssetEditorFileEntry[pathsCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < pathsCount; ++i) {
                obj.paths[i] = AssetEditorFileEntry.deserialize(buf, elemPos);
                elemPos += AssetEditorFileEntry.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 12;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 4);
            int pos0 = offset + 12 + 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 + 8);
            int pos2 = offset + 12 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                pos2 += AssetEditorFileEntry.computeBytesConsumed(buf, pos2);
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.pack != null) {
            nullBits |= 0x1;
        }
        if (this.paths != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.isReadOnly ? 1 : 0);
        buf.writeByte(this.canBeDeleted ? 1 : 0);
        buf.writeByte(this.tree.getValue());
        final int packOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int pathsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.pack != null) {
            buf.setIntLE(packOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.pack, 4096000);
        }
        else {
            buf.setIntLE(packOffsetSlot, -1);
        }
        if (this.paths != null) {
            buf.setIntLE(pathsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.paths.length > 4096000) {
                throw ProtocolException.arrayTooLong("Paths", this.paths.length, 4096000);
            }
            VarInt.write(buf, this.paths.length);
            for (final AssetEditorFileEntry item : this.paths) {
                item.serialize(buf);
            }
        }
        else {
            buf.setIntLE(pathsOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 12;
        if (this.pack != null) {
            size += PacketIO.stringSize(this.pack);
        }
        if (this.paths != null) {
            int pathsSize = 0;
            for (final AssetEditorFileEntry elem : this.paths) {
                pathsSize += elem.computeSize();
            }
            size += VarInt.size(this.paths.length) + pathsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 12) {
            return ValidationResult.error("Buffer too small: expected at least 12 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int packOffset = buffer.getIntLE(offset + 4);
            if (packOffset < 0) {
                return ValidationResult.error("Invalid offset for Pack");
            }
            int pos = offset + 12 + packOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Pack");
            }
            final int packLen = VarInt.peek(buffer, pos);
            if (packLen < 0) {
                return ValidationResult.error("Invalid string length for Pack");
            }
            if (packLen > 4096000) {
                return ValidationResult.error("Pack exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += packLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Pack");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int pathsOffset = buffer.getIntLE(offset + 8);
            if (pathsOffset < 0) {
                return ValidationResult.error("Invalid offset for Paths");
            }
            int pos = offset + 12 + pathsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Paths");
            }
            final int pathsCount = VarInt.peek(buffer, pos);
            if (pathsCount < 0) {
                return ValidationResult.error("Invalid array count for Paths");
            }
            if (pathsCount > 4096000) {
                return ValidationResult.error("Paths exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < pathsCount; ++i) {
                final ValidationResult structResult = AssetEditorFileEntry.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid AssetEditorFileEntry in Paths[" + i + "]: " + structResult.error());
                }
                pos += AssetEditorFileEntry.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public AssetEditorAssetListSetup clone() {
        final AssetEditorAssetListSetup copy = new AssetEditorAssetListSetup();
        copy.pack = this.pack;
        copy.isReadOnly = this.isReadOnly;
        copy.canBeDeleted = this.canBeDeleted;
        copy.tree = this.tree;
        copy.paths = (AssetEditorFileEntry[])((this.paths != null) ? ((AssetEditorFileEntry[])Arrays.stream(this.paths).map(e -> e.clone()).toArray(AssetEditorFileEntry[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetEditorAssetListSetup other) {
            return Objects.equals(this.pack, other.pack) && this.isReadOnly == other.isReadOnly && this.canBeDeleted == other.canBeDeleted && Objects.equals(this.tree, other.tree) && Arrays.equals(this.paths, other.paths);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.pack);
        result = 31 * result + Boolean.hashCode(this.isReadOnly);
        result = 31 * result + Boolean.hashCode(this.canBeDeleted);
        result = 31 * result + Objects.hashCode(this.tree);
        result = 31 * result + Arrays.hashCode(this.paths);
        return result;
    }
}
