// 
// 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.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 ItemWeapon
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 2;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 10;
    public static final int MAX_SIZE = 1626112020;
    @Nullable
    public int[] entityStatsToClear;
    @Nullable
    public Map<Integer, Modifier[]> statModifiers;
    public boolean renderDualWielded;
    
    public ItemWeapon() {
    }
    
    public ItemWeapon(@Nullable final int[] entityStatsToClear, @Nullable final Map<Integer, Modifier[]> statModifiers, final boolean renderDualWielded) {
        this.entityStatsToClear = entityStatsToClear;
        this.statModifiers = statModifiers;
        this.renderDualWielded = renderDualWielded;
    }
    
    public ItemWeapon(@Nonnull final ItemWeapon other) {
        this.entityStatsToClear = other.entityStatsToClear;
        this.statModifiers = other.statModifiers;
        this.renderDualWielded = other.renderDualWielded;
    }
    
    @Nonnull
    public static ItemWeapon deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ItemWeapon obj = new ItemWeapon();
        final byte nullBits = buf.getByte(offset);
        obj.renderDualWielded = (buf.getByte(offset + 1) != 0);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 10 + buf.getIntLE(offset + 2);
            final int entityStatsToClearCount = VarInt.peek(buf, varPos0);
            if (entityStatsToClearCount < 0) {
                throw ProtocolException.negativeLength("EntityStatsToClear", entityStatsToClearCount);
            }
            if (entityStatsToClearCount > 4096000) {
                throw ProtocolException.arrayTooLong("EntityStatsToClear", entityStatsToClearCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + entityStatsToClearCount * 4L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("EntityStatsToClear", varPos0 + varIntLen + entityStatsToClearCount * 4, buf.readableBytes());
            }
            obj.entityStatsToClear = new int[entityStatsToClearCount];
            for (int i = 0; i < entityStatsToClearCount; ++i) {
                obj.entityStatsToClear[i] = buf.getIntLE(varPos0 + varIntLen + i * 4);
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 10 + buf.getIntLE(offset + 6);
            final int statModifiersCount = VarInt.peek(buf, varPos2);
            if (statModifiersCount < 0) {
                throw ProtocolException.negativeLength("StatModifiers", statModifiersCount);
            }
            if (statModifiersCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("StatModifiers", statModifiersCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            obj.statModifiers = new HashMap<Integer, Modifier[]>(statModifiersCount);
            int dictPos = varPos2 + varIntLen;
            for (int j = 0; j < statModifiersCount; ++j) {
                final int key = buf.getIntLE(dictPos);
                dictPos += 4;
                final int valLen = VarInt.peek(buf, dictPos);
                if (valLen < 0) {
                    throw ProtocolException.negativeLength("val", valLen);
                }
                if (valLen > 64) {
                    throw ProtocolException.arrayTooLong("val", valLen, 64);
                }
                final int valVarLen = VarInt.length(buf, dictPos);
                if (dictPos + valVarLen + valLen * 6L > buf.readableBytes()) {
                    throw ProtocolException.bufferTooSmall("val", dictPos + valVarLen + valLen * 6, buf.readableBytes());
                }
                dictPos += valVarLen;
                final Modifier[] val = new Modifier[valLen];
                for (int valIdx = 0; valIdx < valLen; ++valIdx) {
                    val[valIdx] = Modifier.deserialize(buf, dictPos);
                    dictPos += Modifier.computeBytesConsumed(buf, dictPos);
                }
                if (obj.statModifiers.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("statModifiers", key);
                }
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 10;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 2);
            int pos0 = offset + 10 + fieldOffset0;
            final int arrLen = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + arrLen * 4;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 6);
            int pos2 = offset + 10 + fieldOffset2;
            final int dictLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < dictLen; ++i) {
                pos2 += 4;
                final int al = VarInt.peek(buf, pos2);
                pos2 += VarInt.length(buf, pos2);
                for (int j = 0; j < al; ++j) {
                    pos2 += Modifier.computeBytesConsumed(buf, pos2);
                }
            }
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.entityStatsToClear != null) {
            nullBits |= 0x1;
        }
        if (this.statModifiers != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.renderDualWielded ? 1 : 0);
        final int entityStatsToClearOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int statModifiersOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.entityStatsToClear != null) {
            buf.setIntLE(entityStatsToClearOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.entityStatsToClear.length > 4096000) {
                throw ProtocolException.arrayTooLong("EntityStatsToClear", this.entityStatsToClear.length, 4096000);
            }
            VarInt.write(buf, this.entityStatsToClear.length);
            for (final int item : this.entityStatsToClear) {
                buf.writeIntLE(item);
            }
        }
        else {
            buf.setIntLE(entityStatsToClearOffsetSlot, -1);
        }
        if (this.statModifiers != null) {
            buf.setIntLE(statModifiersOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.statModifiers.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("StatModifiers", this.statModifiers.size(), 4096000);
            }
            VarInt.write(buf, this.statModifiers.size());
            for (final Map.Entry<Integer, Modifier[]> e : this.statModifiers.entrySet()) {
                buf.writeIntLE(e.getKey());
                VarInt.write(buf, e.getValue().length);
                for (final Modifier arrItem : e.getValue()) {
                    arrItem.serialize(buf);
                }
            }
        }
        else {
            buf.setIntLE(statModifiersOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 10;
        if (this.entityStatsToClear != null) {
            size += VarInt.size(this.entityStatsToClear.length) + this.entityStatsToClear.length * 4;
        }
        if (this.statModifiers != null) {
            int statModifiersSize = 0;
            for (final Map.Entry<Integer, Modifier[]> kvp : this.statModifiers.entrySet()) {
                statModifiersSize += 4 + VarInt.size(kvp.getValue().length) + kvp.getValue().length * 6;
            }
            size += VarInt.size(this.statModifiers.size()) + statModifiersSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 10) {
            return ValidationResult.error("Buffer too small: expected at least 10 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int entityStatsToClearOffset = buffer.getIntLE(offset + 2);
            if (entityStatsToClearOffset < 0) {
                return ValidationResult.error("Invalid offset for EntityStatsToClear");
            }
            int pos = offset + 10 + entityStatsToClearOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for EntityStatsToClear");
            }
            final int entityStatsToClearCount = VarInt.peek(buffer, pos);
            if (entityStatsToClearCount < 0) {
                return ValidationResult.error("Invalid array count for EntityStatsToClear");
            }
            if (entityStatsToClearCount > 4096000) {
                return ValidationResult.error("EntityStatsToClear exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += entityStatsToClearCount * 4;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading EntityStatsToClear");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int statModifiersOffset = buffer.getIntLE(offset + 6);
            if (statModifiersOffset < 0) {
                return ValidationResult.error("Invalid offset for StatModifiers");
            }
            int pos = offset + 10 + statModifiersOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for StatModifiers");
            }
            final int statModifiersCount = VarInt.peek(buffer, pos);
            if (statModifiersCount < 0) {
                return ValidationResult.error("Invalid dictionary count for StatModifiers");
            }
            if (statModifiersCount > 4096000) {
                return ValidationResult.error("StatModifiers exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < statModifiersCount; ++i) {
                pos += 4;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                final int valueArrCount = VarInt.peek(buffer, pos);
                if (valueArrCount < 0) {
                    return ValidationResult.error("Invalid array count for value");
                }
                pos += VarInt.length(buffer, pos);
                for (int valueArrIdx = 0; valueArrIdx < valueArrCount; ++valueArrIdx) {
                    pos += 6;
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public ItemWeapon clone() {
        final ItemWeapon copy = new ItemWeapon();
        copy.entityStatsToClear = (int[])((this.entityStatsToClear != null) ? Arrays.copyOf(this.entityStatsToClear, this.entityStatsToClear.length) : null);
        if (this.statModifiers != null) {
            final Map<Integer, Modifier[]> m = new HashMap<Integer, Modifier[]>();
            for (final Map.Entry<Integer, Modifier[]> e : this.statModifiers.entrySet()) {
                m.put(e.getKey(), Arrays.stream(e.getValue()).map(x -> x.clone()).toArray(Modifier[]::new));
            }
            copy.statModifiers = m;
        }
        copy.renderDualWielded = this.renderDualWielded;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ItemWeapon other) {
            return Arrays.equals(this.entityStatsToClear, other.entityStatsToClear) && Objects.equals(this.statModifiers, other.statModifiers) && this.renderDualWielded == other.renderDualWielded;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Arrays.hashCode(this.entityStatsToClear);
        result = 31 * result + Objects.hashCode(this.statModifiers);
        result = 31 * result + Boolean.hashCode(this.renderDualWielded);
        return result;
    }
}
