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

package com.hypixel.hytale.protocol;

import java.util.Objects;
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 RoofConnectedBlockRuleSet
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 9;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 21;
    public static final int MAX_SIZE = 49152078;
    @Nullable
    public StairConnectedBlockRuleSet regular;
    @Nullable
    public StairConnectedBlockRuleSet hollow;
    public int topperBlockId;
    public int width;
    @Nullable
    public String materialName;
    
    public RoofConnectedBlockRuleSet() {
    }
    
    public RoofConnectedBlockRuleSet(@Nullable final StairConnectedBlockRuleSet regular, @Nullable final StairConnectedBlockRuleSet hollow, final int topperBlockId, final int width, @Nullable final String materialName) {
        this.regular = regular;
        this.hollow = hollow;
        this.topperBlockId = topperBlockId;
        this.width = width;
        this.materialName = materialName;
    }
    
    public RoofConnectedBlockRuleSet(@Nonnull final RoofConnectedBlockRuleSet other) {
        this.regular = other.regular;
        this.hollow = other.hollow;
        this.topperBlockId = other.topperBlockId;
        this.width = other.width;
        this.materialName = other.materialName;
    }
    
    @Nonnull
    public static RoofConnectedBlockRuleSet deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final RoofConnectedBlockRuleSet obj = new RoofConnectedBlockRuleSet();
        final byte nullBits = buf.getByte(offset);
        obj.topperBlockId = buf.getIntLE(offset + 1);
        obj.width = buf.getIntLE(offset + 5);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 21 + buf.getIntLE(offset + 9);
            obj.regular = StairConnectedBlockRuleSet.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 21 + buf.getIntLE(offset + 13);
            obj.hollow = StairConnectedBlockRuleSet.deserialize(buf, varPos2);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 21 + buf.getIntLE(offset + 17);
            final int materialNameLen = VarInt.peek(buf, varPos3);
            if (materialNameLen < 0) {
                throw ProtocolException.negativeLength("MaterialName", materialNameLen);
            }
            if (materialNameLen > 4096000) {
                throw ProtocolException.stringTooLong("MaterialName", materialNameLen, 4096000);
            }
            obj.materialName = 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 = 21;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 9);
            int pos0 = offset + 21 + fieldOffset0;
            pos0 += StairConnectedBlockRuleSet.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 13);
            int pos2 = offset + 21 + fieldOffset2;
            pos2 += StairConnectedBlockRuleSet.computeBytesConsumed(buf, pos2);
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 17);
            int pos3 = offset + 21 + fieldOffset3;
            final int sl = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + sl;
            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.regular != null) {
            nullBits |= 0x1;
        }
        if (this.hollow != null) {
            nullBits |= 0x2;
        }
        if (this.materialName != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.topperBlockId);
        buf.writeIntLE(this.width);
        final int regularOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int hollowOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int materialNameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.regular != null) {
            buf.setIntLE(regularOffsetSlot, buf.writerIndex() - varBlockStart);
            this.regular.serialize(buf);
        }
        else {
            buf.setIntLE(regularOffsetSlot, -1);
        }
        if (this.hollow != null) {
            buf.setIntLE(hollowOffsetSlot, buf.writerIndex() - varBlockStart);
            this.hollow.serialize(buf);
        }
        else {
            buf.setIntLE(hollowOffsetSlot, -1);
        }
        if (this.materialName != null) {
            buf.setIntLE(materialNameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.materialName, 4096000);
        }
        else {
            buf.setIntLE(materialNameOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 21;
        if (this.regular != null) {
            size += this.regular.computeSize();
        }
        if (this.hollow != null) {
            size += this.hollow.computeSize();
        }
        if (this.materialName != null) {
            size += PacketIO.stringSize(this.materialName);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 21) {
            return ValidationResult.error("Buffer too small: expected at least 21 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int regularOffset = buffer.getIntLE(offset + 9);
            if (regularOffset < 0) {
                return ValidationResult.error("Invalid offset for Regular");
            }
            int pos = offset + 21 + regularOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Regular");
            }
            final ValidationResult regularResult = StairConnectedBlockRuleSet.validateStructure(buffer, pos);
            if (!regularResult.isValid()) {
                return ValidationResult.error("Invalid Regular: " + regularResult.error());
            }
            pos += StairConnectedBlockRuleSet.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int hollowOffset = buffer.getIntLE(offset + 13);
            if (hollowOffset < 0) {
                return ValidationResult.error("Invalid offset for Hollow");
            }
            int pos = offset + 21 + hollowOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Hollow");
            }
            final ValidationResult hollowResult = StairConnectedBlockRuleSet.validateStructure(buffer, pos);
            if (!hollowResult.isValid()) {
                return ValidationResult.error("Invalid Hollow: " + hollowResult.error());
            }
            pos += StairConnectedBlockRuleSet.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int materialNameOffset = buffer.getIntLE(offset + 17);
            if (materialNameOffset < 0) {
                return ValidationResult.error("Invalid offset for MaterialName");
            }
            int pos = offset + 21 + materialNameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for MaterialName");
            }
            final int materialNameLen = VarInt.peek(buffer, pos);
            if (materialNameLen < 0) {
                return ValidationResult.error("Invalid string length for MaterialName");
            }
            if (materialNameLen > 4096000) {
                return ValidationResult.error("MaterialName exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += materialNameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading MaterialName");
            }
        }
        return ValidationResult.OK;
    }
    
    public RoofConnectedBlockRuleSet clone() {
        final RoofConnectedBlockRuleSet copy = new RoofConnectedBlockRuleSet();
        copy.regular = ((this.regular != null) ? this.regular.clone() : null);
        copy.hollow = ((this.hollow != null) ? this.hollow.clone() : null);
        copy.topperBlockId = this.topperBlockId;
        copy.width = this.width;
        copy.materialName = this.materialName;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final RoofConnectedBlockRuleSet other) {
            return Objects.equals(this.regular, other.regular) && Objects.equals(this.hollow, other.hollow) && this.topperBlockId == other.topperBlockId && this.width == other.width && Objects.equals(this.materialName, other.materialName);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.regular, this.hollow, this.topperBlockId, this.width, this.materialName);
    }
}
