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

package com.hypixel.hytale.protocol;

import java.util.Objects;
import java.util.Arrays;
import com.hypixel.hytale.protocol.io.ValidationResult;
import java.util.Iterator;
import java.util.HashMap;
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 java.util.Map;
import javax.annotation.Nullable;

public class ItemReticleConfig
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 4;
    public static final int VARIABLE_BLOCK_START = 17;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String id;
    @Nullable
    public String[] base;
    @Nullable
    public Map<Integer, ItemReticle> serverEvents;
    @Nullable
    public Map<ItemReticleClientEvent, ItemReticle> clientEvents;
    
    public ItemReticleConfig() {
    }
    
    public ItemReticleConfig(@Nullable final String id, @Nullable final String[] base, @Nullable final Map<Integer, ItemReticle> serverEvents, @Nullable final Map<ItemReticleClientEvent, ItemReticle> clientEvents) {
        this.id = id;
        this.base = base;
        this.serverEvents = serverEvents;
        this.clientEvents = clientEvents;
    }
    
    public ItemReticleConfig(@Nonnull final ItemReticleConfig other) {
        this.id = other.id;
        this.base = other.base;
        this.serverEvents = other.serverEvents;
        this.clientEvents = other.clientEvents;
    }
    
    @Nonnull
    public static ItemReticleConfig deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ItemReticleConfig obj = new ItemReticleConfig();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 17 + buf.getIntLE(offset + 1);
            final int idLen = VarInt.peek(buf, varPos0);
            if (idLen < 0) {
                throw ProtocolException.negativeLength("Id", idLen);
            }
            if (idLen > 4096000) {
                throw ProtocolException.stringTooLong("Id", idLen, 4096000);
            }
            obj.id = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 17 + buf.getIntLE(offset + 5);
            final int baseCount = VarInt.peek(buf, varPos2);
            if (baseCount < 0) {
                throw ProtocolException.negativeLength("Base", baseCount);
            }
            if (baseCount > 4096000) {
                throw ProtocolException.arrayTooLong("Base", baseCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + baseCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Base", varPos2 + varIntLen + baseCount * 1, buf.readableBytes());
            }
            obj.base = new String[baseCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < baseCount; ++i) {
                final int strLen = VarInt.peek(buf, elemPos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("base[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("base[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, elemPos);
                obj.base[i] = PacketIO.readVarString(buf, elemPos);
                elemPos += strVarLen + strLen;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 17 + buf.getIntLE(offset + 9);
            final int serverEventsCount = VarInt.peek(buf, varPos3);
            if (serverEventsCount < 0) {
                throw ProtocolException.negativeLength("ServerEvents", serverEventsCount);
            }
            if (serverEventsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("ServerEvents", serverEventsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            obj.serverEvents = new HashMap<Integer, ItemReticle>(serverEventsCount);
            int dictPos = varPos3 + varIntLen;
            for (int i = 0; i < serverEventsCount; ++i) {
                final int key = buf.getIntLE(dictPos);
                dictPos += 4;
                final ItemReticle val = ItemReticle.deserialize(buf, dictPos);
                dictPos += ItemReticle.computeBytesConsumed(buf, dictPos);
                if (obj.serverEvents.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("serverEvents", key);
                }
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos4 = offset + 17 + buf.getIntLE(offset + 13);
            final int clientEventsCount = VarInt.peek(buf, varPos4);
            if (clientEventsCount < 0) {
                throw ProtocolException.negativeLength("ClientEvents", clientEventsCount);
            }
            if (clientEventsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("ClientEvents", clientEventsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos4);
            obj.clientEvents = new HashMap<ItemReticleClientEvent, ItemReticle>(clientEventsCount);
            int dictPos = varPos4 + varIntLen;
            for (int i = 0; i < clientEventsCount; ++i) {
                final ItemReticleClientEvent key2 = ItemReticleClientEvent.fromValue(buf.getByte(dictPos));
                ++dictPos;
                final ItemReticle val = ItemReticle.deserialize(buf, dictPos);
                dictPos += ItemReticle.computeBytesConsumed(buf, dictPos);
                if (obj.clientEvents.put(key2, val) != null) {
                    throw ProtocolException.duplicateKey("clientEvents", key2);
                }
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 17;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 17 + 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 + 5);
            int pos2 = offset + 17 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                final int sl2 = VarInt.peek(buf, pos2);
                pos2 += VarInt.length(buf, pos2) + sl2;
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 9);
            int pos3 = offset + 17 + fieldOffset3;
            final int dictLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3);
            for (int i = 0; i < dictLen; ++i) {
                pos3 += 4;
                pos3 += ItemReticle.computeBytesConsumed(buf, pos3);
            }
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset4 = buf.getIntLE(offset + 13);
            int pos4 = offset + 17 + fieldOffset4;
            final int dictLen = VarInt.peek(buf, pos4);
            pos4 += VarInt.length(buf, pos4);
            for (int i = 0; i < dictLen; ++i) {
                pos4 = ++pos4 + ItemReticle.computeBytesConsumed(buf, pos4);
            }
            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.id != null) {
            nullBits |= 0x1;
        }
        if (this.base != null) {
            nullBits |= 0x2;
        }
        if (this.serverEvents != null) {
            nullBits |= 0x4;
        }
        if (this.clientEvents != null) {
            nullBits |= 0x8;
        }
        buf.writeByte(nullBits);
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int baseOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int serverEventsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int clientEventsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.id != null) {
            buf.setIntLE(idOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.id, 4096000);
        }
        else {
            buf.setIntLE(idOffsetSlot, -1);
        }
        if (this.base != null) {
            buf.setIntLE(baseOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.base.length > 4096000) {
                throw ProtocolException.arrayTooLong("Base", this.base.length, 4096000);
            }
            VarInt.write(buf, this.base.length);
            for (final String item : this.base) {
                PacketIO.writeVarString(buf, item, 4096000);
            }
        }
        else {
            buf.setIntLE(baseOffsetSlot, -1);
        }
        if (this.serverEvents != null) {
            buf.setIntLE(serverEventsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.serverEvents.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("ServerEvents", this.serverEvents.size(), 4096000);
            }
            VarInt.write(buf, this.serverEvents.size());
            for (final Map.Entry<Integer, ItemReticle> e : this.serverEvents.entrySet()) {
                buf.writeIntLE(e.getKey());
                e.getValue().serialize(buf);
            }
        }
        else {
            buf.setIntLE(serverEventsOffsetSlot, -1);
        }
        if (this.clientEvents != null) {
            buf.setIntLE(clientEventsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.clientEvents.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("ClientEvents", this.clientEvents.size(), 4096000);
            }
            VarInt.write(buf, this.clientEvents.size());
            for (final Map.Entry<ItemReticleClientEvent, ItemReticle> e2 : this.clientEvents.entrySet()) {
                buf.writeByte(e2.getKey().getValue());
                e2.getValue().serialize(buf);
            }
        }
        else {
            buf.setIntLE(clientEventsOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 17;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.base != null) {
            int baseSize = 0;
            for (final String elem : this.base) {
                baseSize += PacketIO.stringSize(elem);
            }
            size += VarInt.size(this.base.length) + baseSize;
        }
        if (this.serverEvents != null) {
            int serverEventsSize = 0;
            for (final Map.Entry<Integer, ItemReticle> kvp : this.serverEvents.entrySet()) {
                serverEventsSize += 4 + kvp.getValue().computeSize();
            }
            size += VarInt.size(this.serverEvents.size()) + serverEventsSize;
        }
        if (this.clientEvents != null) {
            int clientEventsSize = 0;
            for (final Map.Entry<ItemReticleClientEvent, ItemReticle> kvp2 : this.clientEvents.entrySet()) {
                clientEventsSize += 1 + kvp2.getValue().computeSize();
            }
            size += VarInt.size(this.clientEvents.size()) + clientEventsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 17) {
            return ValidationResult.error("Buffer too small: expected at least 17 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 1);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 17 + idOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Id");
            }
            final int idLen = VarInt.peek(buffer, pos);
            if (idLen < 0) {
                return ValidationResult.error("Invalid string length for Id");
            }
            if (idLen > 4096000) {
                return ValidationResult.error("Id exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += idLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Id");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int baseOffset = buffer.getIntLE(offset + 5);
            if (baseOffset < 0) {
                return ValidationResult.error("Invalid offset for Base");
            }
            int pos = offset + 17 + baseOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Base");
            }
            final int baseCount = VarInt.peek(buffer, pos);
            if (baseCount < 0) {
                return ValidationResult.error("Invalid array count for Base");
            }
            if (baseCount > 4096000) {
                return ValidationResult.error("Base exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < baseCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in Base");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in Base");
                }
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int serverEventsOffset = buffer.getIntLE(offset + 9);
            if (serverEventsOffset < 0) {
                return ValidationResult.error("Invalid offset for ServerEvents");
            }
            int pos = offset + 17 + serverEventsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ServerEvents");
            }
            final int serverEventsCount = VarInt.peek(buffer, pos);
            if (serverEventsCount < 0) {
                return ValidationResult.error("Invalid dictionary count for ServerEvents");
            }
            if (serverEventsCount > 4096000) {
                return ValidationResult.error("ServerEvents exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < serverEventsCount; ++i) {
                pos += 4;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                pos += ItemReticle.computeBytesConsumed(buffer, pos);
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int clientEventsOffset = buffer.getIntLE(offset + 13);
            if (clientEventsOffset < 0) {
                return ValidationResult.error("Invalid offset for ClientEvents");
            }
            int pos = offset + 17 + clientEventsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ClientEvents");
            }
            final int clientEventsCount = VarInt.peek(buffer, pos);
            if (clientEventsCount < 0) {
                return ValidationResult.error("Invalid dictionary count for ClientEvents");
            }
            if (clientEventsCount > 4096000) {
                return ValidationResult.error("ClientEvents exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < clientEventsCount; ++i) {
                pos = ++pos + ItemReticle.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public ItemReticleConfig clone() {
        final ItemReticleConfig copy = new ItemReticleConfig();
        copy.id = this.id;
        copy.base = (String[])((this.base != null) ? ((String[])Arrays.copyOf(this.base, this.base.length)) : null);
        if (this.serverEvents != null) {
            final Map<Integer, ItemReticle> m = new HashMap<Integer, ItemReticle>();
            for (final Map.Entry<Integer, ItemReticle> e : this.serverEvents.entrySet()) {
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.serverEvents = m;
        }
        if (this.clientEvents != null) {
            final Map<ItemReticleClientEvent, ItemReticle> i = new HashMap<ItemReticleClientEvent, ItemReticle>();
            for (final Map.Entry<ItemReticleClientEvent, ItemReticle> e2 : this.clientEvents.entrySet()) {
                i.put(e2.getKey(), e2.getValue().clone());
            }
            copy.clientEvents = i;
        }
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ItemReticleConfig other) {
            return Objects.equals(this.id, other.id) && Arrays.equals(this.base, other.base) && Objects.equals(this.serverEvents, other.serverEvents) && Objects.equals(this.clientEvents, other.clientEvents);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.id);
        result = 31 * result + Arrays.hashCode(this.base);
        result = 31 * result + Objects.hashCode(this.serverEvents);
        result = 31 * result + Objects.hashCode(this.clientEvents);
        return result;
    }
}
