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

package com.hypixel.hytale.protocol;

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 com.hypixel.hytale.protocol.packets.buildertools.BuilderToolState;
import javax.annotation.Nullable;

public class ItemBuilderToolData
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String[] ui;
    @Nullable
    public BuilderToolState[] tools;
    
    public ItemBuilderToolData() {
    }
    
    public ItemBuilderToolData(@Nullable final String[] ui, @Nullable final BuilderToolState[] tools) {
        this.ui = ui;
        this.tools = tools;
    }
    
    public ItemBuilderToolData(@Nonnull final ItemBuilderToolData other) {
        this.ui = other.ui;
        this.tools = other.tools;
    }
    
    @Nonnull
    public static ItemBuilderToolData deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ItemBuilderToolData obj = new ItemBuilderToolData();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 9 + buf.getIntLE(offset + 1);
            final int uiCount = VarInt.peek(buf, varPos0);
            if (uiCount < 0) {
                throw ProtocolException.negativeLength("Ui", uiCount);
            }
            if (uiCount > 4096000) {
                throw ProtocolException.arrayTooLong("Ui", uiCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos0);
            if (varPos0 + varIntLen + uiCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Ui", varPos0 + varIntLen + uiCount * 1, buf.readableBytes());
            }
            obj.ui = new String[uiCount];
            int elemPos = varPos0 + varIntLen;
            for (int i = 0; i < uiCount; ++i) {
                final int strLen = VarInt.peek(buf, elemPos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("ui[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("ui[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, elemPos);
                obj.ui[i] = PacketIO.readVarString(buf, elemPos);
                elemPos += strVarLen + strLen;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 9 + buf.getIntLE(offset + 5);
            final int toolsCount = VarInt.peek(buf, varPos2);
            if (toolsCount < 0) {
                throw ProtocolException.negativeLength("Tools", toolsCount);
            }
            if (toolsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Tools", toolsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + toolsCount * 2L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Tools", varPos2 + varIntLen + toolsCount * 2, buf.readableBytes());
            }
            obj.tools = new BuilderToolState[toolsCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < toolsCount; ++i) {
                obj.tools[i] = BuilderToolState.deserialize(buf, elemPos);
                elemPos += BuilderToolState.computeBytesConsumed(buf, elemPos);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 9;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 9 + 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 + 9 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2);
            for (int i = 0; i < arrLen; ++i) {
                pos2 += BuilderToolState.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.ui != null) {
            nullBits |= 0x1;
        }
        if (this.tools != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        final int uiOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int toolsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.ui != null) {
            buf.setIntLE(uiOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.ui.length > 4096000) {
                throw ProtocolException.arrayTooLong("Ui", this.ui.length, 4096000);
            }
            VarInt.write(buf, this.ui.length);
            for (final String item : this.ui) {
                PacketIO.writeVarString(buf, item, 4096000);
            }
        }
        else {
            buf.setIntLE(uiOffsetSlot, -1);
        }
        if (this.tools != null) {
            buf.setIntLE(toolsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.tools.length > 4096000) {
                throw ProtocolException.arrayTooLong("Tools", this.tools.length, 4096000);
            }
            VarInt.write(buf, this.tools.length);
            for (final BuilderToolState item2 : this.tools) {
                item2.serialize(buf);
            }
        }
        else {
            buf.setIntLE(toolsOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 9;
        if (this.ui != null) {
            int uiSize = 0;
            for (final String elem : this.ui) {
                uiSize += PacketIO.stringSize(elem);
            }
            size += VarInt.size(this.ui.length) + uiSize;
        }
        if (this.tools != null) {
            int toolsSize = 0;
            for (final BuilderToolState elem2 : this.tools) {
                toolsSize += elem2.computeSize();
            }
            size += VarInt.size(this.tools.length) + toolsSize;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 9) {
            return ValidationResult.error("Buffer too small: expected at least 9 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int uiOffset = buffer.getIntLE(offset + 1);
            if (uiOffset < 0) {
                return ValidationResult.error("Invalid offset for Ui");
            }
            int pos = offset + 9 + uiOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Ui");
            }
            final int uiCount = VarInt.peek(buffer, pos);
            if (uiCount < 0) {
                return ValidationResult.error("Invalid array count for Ui");
            }
            if (uiCount > 4096000) {
                return ValidationResult.error("Ui exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < uiCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in Ui");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in Ui");
                }
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int toolsOffset = buffer.getIntLE(offset + 5);
            if (toolsOffset < 0) {
                return ValidationResult.error("Invalid offset for Tools");
            }
            int pos = offset + 9 + toolsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Tools");
            }
            final int toolsCount = VarInt.peek(buffer, pos);
            if (toolsCount < 0) {
                return ValidationResult.error("Invalid array count for Tools");
            }
            if (toolsCount > 4096000) {
                return ValidationResult.error("Tools exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < toolsCount; ++i) {
                final ValidationResult structResult = BuilderToolState.validateStructure(buffer, pos);
                if (!structResult.isValid()) {
                    return ValidationResult.error("Invalid BuilderToolState in Tools[" + i + "]: " + structResult.error());
                }
                pos += BuilderToolState.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public ItemBuilderToolData clone() {
        final ItemBuilderToolData copy = new ItemBuilderToolData();
        copy.ui = (String[])((this.ui != null) ? ((String[])Arrays.copyOf(this.ui, this.ui.length)) : null);
        copy.tools = (BuilderToolState[])((this.tools != null) ? ((BuilderToolState[])Arrays.stream(this.tools).map(e -> e.clone()).toArray(BuilderToolState[]::new)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ItemBuilderToolData other) {
            return Arrays.equals(this.ui, other.ui) && Arrays.equals(this.tools, other.tools);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Arrays.hashCode(this.ui);
        result = 31 * result + Arrays.hashCode(this.tools);
        return result;
    }
}
