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

package com.hypixel.hytale.protocol;

import java.util.Objects;
import com.hypixel.hytale.protocol.io.ValidationResult;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nullable;
import javax.annotation.Nonnull;

public class ConnectedBlockRuleSet
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 2;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 10;
    public static final int MAX_SIZE = 65536114;
    @Nonnull
    public ConnectedBlockRuleSetType type;
    @Nullable
    public StairConnectedBlockRuleSet stair;
    @Nullable
    public RoofConnectedBlockRuleSet roof;
    
    public ConnectedBlockRuleSet() {
        this.type = ConnectedBlockRuleSetType.Stair;
    }
    
    public ConnectedBlockRuleSet(@Nonnull final ConnectedBlockRuleSetType type, @Nullable final StairConnectedBlockRuleSet stair, @Nullable final RoofConnectedBlockRuleSet roof) {
        this.type = ConnectedBlockRuleSetType.Stair;
        this.type = type;
        this.stair = stair;
        this.roof = roof;
    }
    
    public ConnectedBlockRuleSet(@Nonnull final ConnectedBlockRuleSet other) {
        this.type = ConnectedBlockRuleSetType.Stair;
        this.type = other.type;
        this.stair = other.stair;
        this.roof = other.roof;
    }
    
    @Nonnull
    public static ConnectedBlockRuleSet deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ConnectedBlockRuleSet obj = new ConnectedBlockRuleSet();
        final byte nullBits = buf.getByte(offset);
        obj.type = ConnectedBlockRuleSetType.fromValue(buf.getByte(offset + 1));
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 10 + buf.getIntLE(offset + 2);
            obj.stair = StairConnectedBlockRuleSet.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 10 + buf.getIntLE(offset + 6);
            obj.roof = RoofConnectedBlockRuleSet.deserialize(buf, varPos2);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 10;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 2);
            int pos0 = offset + 10 + fieldOffset0;
            pos0 += StairConnectedBlockRuleSet.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 6);
            int pos2 = offset + 10 + fieldOffset2;
            pos2 += RoofConnectedBlockRuleSet.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.stair != null) {
            nullBits |= 0x1;
        }
        if (this.roof != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.type.getValue());
        final int stairOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int roofOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.stair != null) {
            buf.setIntLE(stairOffsetSlot, buf.writerIndex() - varBlockStart);
            this.stair.serialize(buf);
        }
        else {
            buf.setIntLE(stairOffsetSlot, -1);
        }
        if (this.roof != null) {
            buf.setIntLE(roofOffsetSlot, buf.writerIndex() - varBlockStart);
            this.roof.serialize(buf);
        }
        else {
            buf.setIntLE(roofOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 10;
        if (this.stair != null) {
            size += this.stair.computeSize();
        }
        if (this.roof != null) {
            size += this.roof.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 10) {
            return ValidationResult.error("Buffer too small: expected at least 10 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int stairOffset = buffer.getIntLE(offset + 2);
            if (stairOffset < 0) {
                return ValidationResult.error("Invalid offset for Stair");
            }
            int pos = offset + 10 + stairOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Stair");
            }
            final ValidationResult stairResult = StairConnectedBlockRuleSet.validateStructure(buffer, pos);
            if (!stairResult.isValid()) {
                return ValidationResult.error("Invalid Stair: " + stairResult.error());
            }
            pos += StairConnectedBlockRuleSet.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int roofOffset = buffer.getIntLE(offset + 6);
            if (roofOffset < 0) {
                return ValidationResult.error("Invalid offset for Roof");
            }
            int pos = offset + 10 + roofOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Roof");
            }
            final ValidationResult roofResult = RoofConnectedBlockRuleSet.validateStructure(buffer, pos);
            if (!roofResult.isValid()) {
                return ValidationResult.error("Invalid Roof: " + roofResult.error());
            }
            pos += RoofConnectedBlockRuleSet.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public ConnectedBlockRuleSet clone() {
        final ConnectedBlockRuleSet copy = new ConnectedBlockRuleSet();
        copy.type = this.type;
        copy.stair = ((this.stair != null) ? this.stair.clone() : null);
        copy.roof = ((this.roof != null) ? this.roof.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ConnectedBlockRuleSet other) {
            return Objects.equals(this.type, other.type) && Objects.equals(this.stair, other.stair) && Objects.equals(this.roof, other.roof);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.type, this.stair, this.roof);
    }
}
