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

package com.hypixel.hytale.protocol.packets.buildertools;

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 BuilderToolOptionArg
{
    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 defaultValue;
    @Nullable
    public String[] options;
    
    public BuilderToolOptionArg() {
    }
    
    public BuilderToolOptionArg(@Nullable final String defaultValue, @Nullable final String[] options) {
        this.defaultValue = defaultValue;
        this.options = options;
    }
    
    public BuilderToolOptionArg(@Nonnull final BuilderToolOptionArg other) {
        this.defaultValue = other.defaultValue;
        this.options = other.options;
    }
    
    @Nonnull
    public static BuilderToolOptionArg deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BuilderToolOptionArg obj = new BuilderToolOptionArg();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 9 + buf.getIntLE(offset + 1);
            final int defaultValueLen = VarInt.peek(buf, varPos0);
            if (defaultValueLen < 0) {
                throw ProtocolException.negativeLength("Default", defaultValueLen);
            }
            if (defaultValueLen > 4096000) {
                throw ProtocolException.stringTooLong("Default", defaultValueLen, 4096000);
            }
            obj.defaultValue = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 9 + buf.getIntLE(offset + 5);
            final int optionsCount = VarInt.peek(buf, varPos2);
            if (optionsCount < 0) {
                throw ProtocolException.negativeLength("Options", optionsCount);
            }
            if (optionsCount > 4096000) {
                throw ProtocolException.arrayTooLong("Options", optionsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + optionsCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Options", varPos2 + varIntLen + optionsCount * 1, buf.readableBytes());
            }
            obj.options = new String[optionsCount];
            int elemPos = varPos2 + varIntLen;
            for (int i = 0; i < optionsCount; ++i) {
                final int strLen = VarInt.peek(buf, elemPos);
                if (strLen < 0) {
                    throw ProtocolException.negativeLength("options[" + i, strLen);
                }
                if (strLen > 4096000) {
                    throw ProtocolException.stringTooLong("options[" + i, strLen, 4096000);
                }
                final int strVarLen = VarInt.length(buf, elemPos);
                obj.options[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 = 9;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 9 + 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 + 9 + 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.defaultValue != null) {
            nullBits |= 0x1;
        }
        if (this.options != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        final int defaultValueOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int optionsOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.defaultValue != null) {
            buf.setIntLE(defaultValueOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.defaultValue, 4096000);
        }
        else {
            buf.setIntLE(defaultValueOffsetSlot, -1);
        }
        if (this.options != null) {
            buf.setIntLE(optionsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.options.length > 4096000) {
                throw ProtocolException.arrayTooLong("Options", this.options.length, 4096000);
            }
            VarInt.write(buf, this.options.length);
            for (final String item : this.options) {
                PacketIO.writeVarString(buf, item, 4096000);
            }
        }
        else {
            buf.setIntLE(optionsOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 9;
        if (this.defaultValue != null) {
            size += PacketIO.stringSize(this.defaultValue);
        }
        if (this.options != null) {
            int optionsSize = 0;
            for (final String elem : this.options) {
                optionsSize += PacketIO.stringSize(elem);
            }
            size += VarInt.size(this.options.length) + optionsSize;
        }
        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 defaultOffset = buffer.getIntLE(offset + 1);
            if (defaultOffset < 0) {
                return ValidationResult.error("Invalid offset for Default");
            }
            int pos = offset + 9 + defaultOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Default");
            }
            final int defaultLen = VarInt.peek(buffer, pos);
            if (defaultLen < 0) {
                return ValidationResult.error("Invalid string length for Default");
            }
            if (defaultLen > 4096000) {
                return ValidationResult.error("Default exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += defaultLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Default");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int optionsOffset = buffer.getIntLE(offset + 5);
            if (optionsOffset < 0) {
                return ValidationResult.error("Invalid offset for Options");
            }
            int pos = offset + 9 + optionsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Options");
            }
            final int optionsCount = VarInt.peek(buffer, pos);
            if (optionsCount < 0) {
                return ValidationResult.error("Invalid array count for Options");
            }
            if (optionsCount > 4096000) {
                return ValidationResult.error("Options exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < optionsCount; ++i) {
                final int strLen = VarInt.peek(buffer, pos);
                if (strLen < 0) {
                    return ValidationResult.error("Invalid string length in Options");
                }
                pos += VarInt.length(buffer, pos);
                pos += strLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading string in Options");
                }
            }
        }
        return ValidationResult.OK;
    }
    
    public BuilderToolOptionArg clone() {
        final BuilderToolOptionArg copy = new BuilderToolOptionArg();
        copy.defaultValue = this.defaultValue;
        copy.options = (String[])((this.options != null) ? ((String[])Arrays.copyOf(this.options, this.options.length)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BuilderToolOptionArg other) {
            return Objects.equals(this.defaultValue, other.defaultValue) && Arrays.equals(this.options, other.options);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.defaultValue);
        result = 31 * result + Arrays.hashCode(this.options);
        return result;
    }
}
