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

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

import java.util.Objects;
import com.hypixel.hytale.protocol.io.ValidationResult;
import java.util.Iterator;
import java.util.HashMap;
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 java.util.Map;
import javax.annotation.Nullable;

public class BuilderToolState
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 2;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 14;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public String id;
    public boolean isBrush;
    @Nullable
    public BuilderToolBrushData brushData;
    @Nullable
    public Map<String, BuilderToolArg> args;
    
    public BuilderToolState() {
    }
    
    public BuilderToolState(@Nullable final String id, final boolean isBrush, @Nullable final BuilderToolBrushData brushData, @Nullable final Map<String, BuilderToolArg> args) {
        this.id = id;
        this.isBrush = isBrush;
        this.brushData = brushData;
        this.args = args;
    }
    
    public BuilderToolState(@Nonnull final BuilderToolState other) {
        this.id = other.id;
        this.isBrush = other.isBrush;
        this.brushData = other.brushData;
        this.args = other.args;
    }
    
    @Nonnull
    public static BuilderToolState deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BuilderToolState obj = new BuilderToolState();
        final byte nullBits = buf.getByte(offset);
        obj.isBrush = (buf.getByte(offset + 1) != 0);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 14 + buf.getIntLE(offset + 2);
            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 + 6);
            obj.brushData = BuilderToolBrushData.deserialize(buf, varPos2);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 14 + buf.getIntLE(offset + 10);
            final int argsCount = VarInt.peek(buf, varPos3);
            if (argsCount < 0) {
                throw ProtocolException.negativeLength("Args", argsCount);
            }
            if (argsCount > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Args", argsCount, 4096000);
            }
            final int varIntLen = VarInt.length(buf, varPos3);
            obj.args = new HashMap<String, BuilderToolArg>(argsCount);
            int dictPos = varPos3 + varIntLen;
            for (int i = 0; i < argsCount; ++i) {
                final int keyLen = VarInt.peek(buf, dictPos);
                if (keyLen < 0) {
                    throw ProtocolException.negativeLength("key", keyLen);
                }
                if (keyLen > 4096000) {
                    throw ProtocolException.stringTooLong("key", keyLen, 4096000);
                }
                final int keyVarLen = VarInt.length(buf, dictPos);
                final String key = PacketIO.readVarString(buf, dictPos);
                dictPos += keyVarLen + keyLen;
                final BuilderToolArg val = BuilderToolArg.deserialize(buf, dictPos);
                dictPos += BuilderToolArg.computeBytesConsumed(buf, dictPos);
                if (obj.args.put(key, val) != null) {
                    throw ProtocolException.duplicateKey("args", key);
                }
            }
        }
        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 + 2);
            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 + 6);
            int pos2 = offset + 14 + fieldOffset2;
            pos2 += BuilderToolBrushData.computeBytesConsumed(buf, pos2);
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 10);
            int pos3 = offset + 14 + fieldOffset3;
            final int dictLen = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3);
            for (int i = 0; i < dictLen; ++i) {
                final int sl2 = VarInt.peek(buf, pos3);
                pos3 += VarInt.length(buf, pos3) + sl2;
                pos3 += BuilderToolArg.computeBytesConsumed(buf, pos3);
            }
            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.id != null) {
            nullBits |= 0x1;
        }
        if (this.brushData != null) {
            nullBits |= 0x2;
        }
        if (this.args != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.isBrush ? 1 : 0);
        final int idOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int brushDataOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int argsOffsetSlot = 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.brushData != null) {
            buf.setIntLE(brushDataOffsetSlot, buf.writerIndex() - varBlockStart);
            this.brushData.serialize(buf);
        }
        else {
            buf.setIntLE(brushDataOffsetSlot, -1);
        }
        if (this.args != null) {
            buf.setIntLE(argsOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.args.size() > 4096000) {
                throw ProtocolException.dictionaryTooLarge("Args", this.args.size(), 4096000);
            }
            VarInt.write(buf, this.args.size());
            for (final Map.Entry<String, BuilderToolArg> e : this.args.entrySet()) {
                PacketIO.writeVarString(buf, e.getKey(), 4096000);
                e.getValue().serialize(buf);
            }
        }
        else {
            buf.setIntLE(argsOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 14;
        if (this.id != null) {
            size += PacketIO.stringSize(this.id);
        }
        if (this.brushData != null) {
            size += this.brushData.computeSize();
        }
        if (this.args != null) {
            int argsSize = 0;
            for (final Map.Entry<String, BuilderToolArg> kvp : this.args.entrySet()) {
                argsSize += PacketIO.stringSize(kvp.getKey()) + kvp.getValue().computeSize();
            }
            size += VarInt.size(this.args.size()) + argsSize;
        }
        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 + 2);
            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 brushDataOffset = buffer.getIntLE(offset + 6);
            if (brushDataOffset < 0) {
                return ValidationResult.error("Invalid offset for BrushData");
            }
            int pos = offset + 14 + brushDataOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for BrushData");
            }
            final ValidationResult brushDataResult = BuilderToolBrushData.validateStructure(buffer, pos);
            if (!brushDataResult.isValid()) {
                return ValidationResult.error("Invalid BrushData: " + brushDataResult.error());
            }
            pos += BuilderToolBrushData.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int argsOffset = buffer.getIntLE(offset + 10);
            if (argsOffset < 0) {
                return ValidationResult.error("Invalid offset for Args");
            }
            int pos = offset + 14 + argsOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Args");
            }
            final int argsCount = VarInt.peek(buffer, pos);
            if (argsCount < 0) {
                return ValidationResult.error("Invalid dictionary count for Args");
            }
            if (argsCount > 4096000) {
                return ValidationResult.error("Args exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            for (int i = 0; i < argsCount; ++i) {
                final int keyLen = VarInt.peek(buffer, pos);
                if (keyLen < 0) {
                    return ValidationResult.error("Invalid string length for key");
                }
                if (keyLen > 4096000) {
                    return ValidationResult.error("key exceeds max length 4096000");
                }
                pos += VarInt.length(buffer, pos);
                pos += keyLen;
                if (pos > buffer.writerIndex()) {
                    return ValidationResult.error("Buffer overflow reading key");
                }
                pos += BuilderToolArg.computeBytesConsumed(buffer, pos);
            }
        }
        return ValidationResult.OK;
    }
    
    public BuilderToolState clone() {
        final BuilderToolState copy = new BuilderToolState();
        copy.id = this.id;
        copy.isBrush = this.isBrush;
        copy.brushData = ((this.brushData != null) ? this.brushData.clone() : null);
        if (this.args != null) {
            final Map<String, BuilderToolArg> m = new HashMap<String, BuilderToolArg>();
            for (final Map.Entry<String, BuilderToolArg> e : this.args.entrySet()) {
                m.put(e.getKey(), e.getValue().clone());
            }
            copy.args = m;
        }
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BuilderToolState other) {
            return Objects.equals(this.id, other.id) && this.isBrush == other.isBrush && Objects.equals(this.brushData, other.brushData) && Objects.equals(this.args, other.args);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.id, this.isBrush, this.brushData, this.args);
    }
}
