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

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

import java.util.Objects;
import com.hypixel.hytale.protocol.io.ValidationResult;
import java.util.Iterator;
import com.hypixel.hytale.protocol.io.PacketIO;
import java.util.HashMap;
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 java.util.Map;
import com.hypixel.hytale.protocol.Packet;

public class AssetEditorAssetPackSetup implements Packet
{
    public static final int PACKET_ID = 314;
    public static final boolean IS_COMPRESSED = false;
    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 Map<String, AssetPackManifest> packs;
    
    @Override
    public int getId() {
        return 314;
    }
    
    public AssetEditorAssetPackSetup() {
    }
    
    public AssetEditorAssetPackSetup(@Nullable final Map<String, AssetPackManifest> packs) {
        this.packs = packs;
    }
    
    public AssetEditorAssetPackSetup(@Nonnull final AssetEditorAssetPackSetup other) {
        this.packs = other.packs;
    }
    
    @Nonnull
    public static AssetEditorAssetPackSetup deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetEditorAssetPackSetup obj = new AssetEditorAssetPackSetup();
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 1;
        if ((nullBits & 0x1) != 0x0) {
            final int packsCount = VarInt.peek(buf, pos);
            if (packsCount < 0) {
                throw ProtocolException.negativeLength("Packs", packsCount);
            }
            if (packsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Packs", packsCount, 4096000);
            }
            pos += VarInt.size(packsCount);
            obj.packs = new HashMap<String, AssetPackManifest>(packsCount);
            for (int i = 0; i < packsCount; ++i) {
                final int keyLen = VarInt.peek(buf, pos);
                if (keyLen < 0) {
                    throw ProtocolException.negativeLength("key", keyLen);
                }
                if (keyLen > 4096000) {
                    throw ProtocolException.stringTooLong("key", keyLen, 4096000);
                }
                final int keyVarLen = VarInt.length(buf, pos);
                final String key = PacketIO.readVarString(buf, pos);
                pos += keyVarLen + keyLen;
                final AssetPackManifest val = AssetPackManifest.deserialize(buf, pos);
                pos += AssetPackManifest.computeBytesConsumed(buf, pos);
                if (obj.packs.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("packs", key);
                }
            }
        }
        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 dictLen = VarInt.peek(buf, pos);
            pos += VarInt.length(buf, pos);
            for (int i = 0; i < dictLen; ++i) {
                final int sl = VarInt.peek(buf, pos);
                pos += VarInt.length(buf, pos) + sl;
                pos += AssetPackManifest.computeBytesConsumed(buf, pos);
            }
        }
        return pos - offset;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.packs != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        if (this.packs != null) {
            if (this.packs.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Packs", this.packs.size(), 4096000);
            }
            VarInt.write(buf, this.packs.size());
            for (final Map.Entry<String, AssetPackManifest> e : this.packs.entrySet()) {
                PacketIO.writeVarString(buf, e.getKey(), 4096000);
                e.getValue().serialize(buf);
            }
        }
    }
    
    @Override
    public int computeSize() {
        int size = 1;
        if (this.packs != null) {
            int packsSize = 0;
            for (final Map.Entry<String, AssetPackManifest> kvp : this.packs.entrySet()) {
                packsSize += PacketIO.stringSize(kvp.getKey()) + kvp.getValue().computeSize();
            }
            size += VarInt.size(this.packs.size()) + packsSize;
        }
        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 packsCount = VarInt.peek(buffer, pos);
            if (packsCount < 0) {
                return ValidationResult.error("Invalid dictionary count for Packs");
            }
            if (packsCount > 4096000) {
                return ValidationResult.error("Packs exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < packsCount; ++i) {
                final int keyLen = VarInt.peek(buffer, pos);
                if (keyLen < 0) {
                    return ValidationResult.error("Invalid string length for key");
                }
                if (keyLen > 4096000) {
                    return ValidationResult.error("key exceeds max length 4096000");
                }
                pos += VarInt.length(buffer, pos);
                pos += keyLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                pos += AssetPackManifest.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public AssetEditorAssetPackSetup clone() {
        final AssetEditorAssetPackSetup copy = new AssetEditorAssetPackSetup();
        if (this.packs != null) {
            final Map<String, AssetPackManifest> m = new HashMap<String, AssetPackManifest>();
            for (final Map.Entry<String, AssetPackManifest> e : this.packs.entrySet()) {
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.packs = m;
        }
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetEditorAssetPackSetup other) {
            return Objects.equals(this.packs, other.packs);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.packs);
    }
}
