// 
// 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 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;

public class Equipment
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 13;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String[] armorIds;
    @Nullable
    public String rightHandItemId;
    @Nullable
    public String leftHandItemId;
    
    public Equipment() {
    }
    
    public Equipment(@Nullable final String[] armorIds, @Nullable final String rightHandItemId, @Nullable final String leftHandItemId) {
        this.armorIds = armorIds;
        this.rightHandItemId = rightHandItemId;
        this.leftHandItemId = leftHandItemId;
    }
    
    public Equipment(@Nonnull final Equipment other) {
        this.armorIds = other.armorIds;
        this.rightHandItemId = other.rightHandItemId;
        this.leftHandItemId = other.leftHandItemId;
    }
    
    @Nonnull
    public static Equipment deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final Equipment obj = new Equipment();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 13 + buf.getIntLE(offset + 1);
            final int armorIdsCount = VarInt.peek(buf, varPos0);
            if (armorIdsCount < 0) {
                throw ProtocolException.negativeLength("ArmorIds", armorIdsCount);
            }
            if (armorIdsCount > 4096000) {
                throw ProtocolException.arrayTooLong("ArmorIds", armorIdsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + armorIdsCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("ArmorIds", varPos0 + varIntLen + armorIdsCount * 1, buf.readableBytes());
            }
            obj.armorIds = new String[armorIdsCount];
            int elemPos = varPos0 + varIntLen;
            for (int i = 0; i < armorIdsCount; ++i) {
                final int strLen = VarInt.peek(buf, elemPos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("armorIds[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("armorIds[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, elemPos);
                obj.armorIds[i] = PacketIO.readVarString(buf, elemPos);
                elemPos += strVarLen + strLen;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 13 + buf.getIntLE(offset + 5);
            final int rightHandItemIdLen = VarInt.peek(buf, varPos2);
            if (rightHandItemIdLen < 0) {
                throw ProtocolException.negativeLength("RightHandItemId", rightHandItemIdLen);
            }
            if (rightHandItemIdLen > 4096000) {
                throw ProtocolException.stringTooLong("RightHandItemId", rightHandItemIdLen, 4096000);
            }
            obj.rightHandItemId = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 13 + buf.getIntLE(offset + 9);
            final int leftHandItemIdLen = VarInt.peek(buf, varPos3);
            if (leftHandItemIdLen < 0) {
                throw ProtocolException.negativeLength("LeftHandItemId", leftHandItemIdLen);
            }
            if (leftHandItemIdLen > 4096000) {
                throw ProtocolException.stringTooLong("LeftHandItemId", leftHandItemIdLen, 4096000);
            }
            obj.leftHandItemId = PacketIO.readVarString(buf, varPos3, PacketIO.UTF8);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 13;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 13 + 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 & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 5);
            int pos2 = offset + 13 + fieldOffset2;
            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 + 13 + fieldOffset3;
            final int sl2 = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + sl2;
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.armorIds != null) {
            nullBits |= 0x1;
        }
        if (this.rightHandItemId != null) {
            nullBits |= 0x2;
        }
        if (this.leftHandItemId != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        final int armorIdsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int rightHandItemIdOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int leftHandItemIdOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.armorIds != null) {
            buf.setIntLE(armorIdsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.armorIds.length > 4096000) {
                throw ProtocolException.arrayTooLong("ArmorIds", this.armorIds.length, 4096000);
            }
            VarInt.write(buf, this.armorIds.length);
            for (final String item : this.armorIds) {
                PacketIO.writeVarString(buf, item, 4096000);
            }
        }
        else {
            buf.setIntLE(armorIdsOffsetSlot, -1);
        }
        if (this.rightHandItemId != null) {
            buf.setIntLE(rightHandItemIdOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.rightHandItemId, 4096000);
        }
        else {
            buf.setIntLE(rightHandItemIdOffsetSlot, -1);
        }
        if (this.leftHandItemId != null) {
            buf.setIntLE(leftHandItemIdOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.leftHandItemId, 4096000);
        }
        else {
            buf.setIntLE(leftHandItemIdOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 13;
        if (this.armorIds != null) {
            int armorIdsSize = 0;
            for (final String elem : this.armorIds) {
                armorIdsSize += PacketIO.stringSize(elem);
            }
            size += VarInt.size(this.armorIds.length) + armorIdsSize;
        }
        if (this.rightHandItemId != null) {
            size += PacketIO.stringSize(this.rightHandItemId);
        }
        if (this.leftHandItemId != null) {
            size += PacketIO.stringSize(this.leftHandItemId);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 13) {
            return ValidationResult.error("Buffer too small: expected at least 13 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int armorIdsOffset = buffer.getIntLE(offset + 1);
            if (armorIdsOffset < 0) {
                return ValidationResult.error("Invalid offset for ArmorIds");
            }
            int pos = offset + 13 + armorIdsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ArmorIds");
            }
            final int armorIdsCount = VarInt.peek(buffer, pos);
            if (armorIdsCount < 0) {
                return ValidationResult.error("Invalid array count for ArmorIds");
            }
            if (armorIdsCount > 4096000) {
                return ValidationResult.error("ArmorIds exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < armorIdsCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in ArmorIds");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in ArmorIds");
                }
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int rightHandItemIdOffset = buffer.getIntLE(offset + 5);
            if (rightHandItemIdOffset < 0) {
                return ValidationResult.error("Invalid offset for RightHandItemId");
            }
            int pos = offset + 13 + rightHandItemIdOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for RightHandItemId");
            }
            final int rightHandItemIdLen = VarInt.peek(buffer, pos);
            if (rightHandItemIdLen < 0) {
                return ValidationResult.error("Invalid string length for RightHandItemId");
            }
            if (rightHandItemIdLen > 4096000) {
                return ValidationResult.error("RightHandItemId exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += rightHandItemIdLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading RightHandItemId");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int leftHandItemIdOffset = buffer.getIntLE(offset + 9);
            if (leftHandItemIdOffset < 0) {
                return ValidationResult.error("Invalid offset for LeftHandItemId");
            }
            int pos = offset + 13 + leftHandItemIdOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for LeftHandItemId");
            }
            final int leftHandItemIdLen = VarInt.peek(buffer, pos);
            if (leftHandItemIdLen < 0) {
                return ValidationResult.error("Invalid string length for LeftHandItemId");
            }
            if (leftHandItemIdLen > 4096000) {
                return ValidationResult.error("LeftHandItemId exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += leftHandItemIdLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading LeftHandItemId");
            }
        }
        return ValidationResult.OK;
    }
    
    public Equipment clone() {
        final Equipment copy = new Equipment();
        copy.armorIds = (String[])((this.armorIds != null) ? ((String[])Arrays.copyOf(this.armorIds, this.armorIds.length)) : null);
        copy.rightHandItemId = this.rightHandItemId;
        copy.leftHandItemId = this.leftHandItemId;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final Equipment other) {
            return Arrays.equals(this.armorIds, other.armorIds) && Objects.equals(this.rightHandItemId, other.rightHandItemId) && Objects.equals(this.leftHandItemId, other.leftHandItemId);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Arrays.hashCode(this.armorIds);
        result = 31 * result + Objects.hashCode(this.rightHandItemId);
        result = 31 * result + Objects.hashCode(this.leftHandItemId);
        return result;
    }
}
