// 
// 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.Nullable;
import javax.annotation.Nonnull;

public class BenchRequirement
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 6;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 14;
    public static final int MAX_SIZE = 1677721600;
    @Nonnull
    public BenchType type;
    @Nullable
    public String id;
    @Nullable
    public String[] categories;
    public int requiredTierLevel;
    
    public BenchRequirement() {
        this.type = BenchType.Crafting;
    }
    
    public BenchRequirement(@Nonnull final BenchType type, @Nullable final String id, @Nullable final String[] categories, final int requiredTierLevel) {
        this.type = BenchType.Crafting;
        this.type = type;
        this.id = id;
        this.categories = categories;
        this.requiredTierLevel = requiredTierLevel;
    }
    
    public BenchRequirement(@Nonnull final BenchRequirement other) {
        this.type = BenchType.Crafting;
        this.type = other.type;
        this.id = other.id;
        this.categories = other.categories;
        this.requiredTierLevel = other.requiredTierLevel;
    }
    
    @Nonnull
    public static BenchRequirement deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BenchRequirement obj = new BenchRequirement();
        final byte nullBits = buf.getByte(offset);
        obj.type = BenchType.fromValue(buf.getByte(offset + 1));
        obj.requiredTierLevel = buf.getIntLE(offset + 2);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 14 + buf.getIntLE(offset + 6);
            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 + 14 + buf.getIntLE(offset + 10);
            final int categoriesCount = VarInt.peek(buf, varPos2);
            if (categoriesCount < 0) {
                throw ProtocolException.negativeLength("Categories", categoriesCount);
            }
            if (categoriesCount > 4096000) {
                throw ProtocolException.arrayTooLong("Categories", categoriesCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + categoriesCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Categories", varPos2 + varIntLen + categoriesCount * 1, buf.readableBytes());
            }
            obj.categories = new String[categoriesCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < categoriesCount; ++i) {
                final int strLen = VarInt.peek(buf, elemPos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("categories[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("categories[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, elemPos);
                obj.categories[i] = PacketIO.readVarString(buf, elemPos);
                elemPos += strVarLen + strLen;
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 14;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 6);
            int pos0 = offset + 14 + 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 + 10);
            int pos2 = offset + 14 + 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;
            }
        }
        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.categories != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.type.getValue());
        buf.writeIntLE(this.requiredTierLevel);
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int categoriesOffsetSlot = 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.categories != null) {
            buf.setIntLE(categoriesOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.categories.length > 4096000) {
                throw ProtocolException.arrayTooLong("Categories", this.categories.length, 4096000);
            }
            VarInt.write(buf, this.categories.length);
            for (final String item : this.categories) {
                PacketIO.writeVarString(buf, item, 4096000);
            }
        }
        else {
            buf.setIntLE(categoriesOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 14;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.categories != null) {
            int categoriesSize = 0;
            for (final String elem : this.categories) {
                categoriesSize += PacketIO.stringSize(elem);
            }
            size += VarInt.size(this.categories.length) + categoriesSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 14) {
            return ValidationResult.error("Buffer too small: expected at least 14 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int idOffset = buffer.getIntLE(offset + 6);
            if (idOffset < 0) {
                return ValidationResult.error("Invalid offset for Id");
            }
            int pos = offset + 14 + 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 categoriesOffset = buffer.getIntLE(offset + 10);
            if (categoriesOffset < 0) {
                return ValidationResult.error("Invalid offset for Categories");
            }
            int pos = offset + 14 + categoriesOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Categories");
            }
            final int categoriesCount = VarInt.peek(buffer, pos);
            if (categoriesCount < 0) {
                return ValidationResult.error("Invalid array count for Categories");
            }
            if (categoriesCount > 4096000) {
                return ValidationResult.error("Categories exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < categoriesCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in Categories");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in Categories");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public BenchRequirement clone() {
        final BenchRequirement copy = new BenchRequirement();
        copy.type = this.type;
        copy.id = this.id;
        copy.categories = (String[])((this.categories != null) ? ((String[])Arrays.copyOf(this.categories, this.categories.length)) : null);
        copy.requiredTierLevel = this.requiredTierLevel;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BenchRequirement other) {
            return Objects.equals(this.type, other.type) && Objects.equals(this.id, other.id) && Arrays.equals(this.categories, other.categories) && this.requiredTierLevel == other.requiredTierLevel;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.type);
        result = 31 * result + Objects.hashCode(this.id);
        result = 31 * result + Arrays.hashCode(this.categories);
        result = 31 * result + Integer.hashCode(this.requiredTierLevel);
        return result;
    }
}
