// 
// 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.Nonnull;
import javax.annotation.Nullable;

public class BlockGathering
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 13;
    public static final int MAX_SIZE = 114688092;
    @Nullable
    public BlockBreaking breaking;
    @Nullable
    public Harvesting harvest;
    @Nullable
    public SoftBlock soft;
    
    public BlockGathering() {
    }
    
    public BlockGathering(@Nullable final BlockBreaking breaking, @Nullable final Harvesting harvest, @Nullable final SoftBlock soft) {
        this.breaking = breaking;
        this.harvest = harvest;
        this.soft = soft;
    }
    
    public BlockGathering(@Nonnull final BlockGathering other) {
        this.breaking = other.breaking;
        this.harvest = other.harvest;
        this.soft = other.soft;
    }
    
    @Nonnull
    public static BlockGathering deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BlockGathering obj = new BlockGathering();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 13 + buf.getIntLE(offset + 1);
            obj.breaking = BlockBreaking.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 13 + buf.getIntLE(offset + 5);
            obj.harvest = Harvesting.deserialize(buf, varPos2);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 13 + buf.getIntLE(offset + 9);
            obj.soft = SoftBlock.deserialize(buf, varPos3);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 13;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 13 + fieldOffset0;
            pos0 += BlockBreaking.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 5);
            int pos2 = offset + 13 + fieldOffset2;
            pos2 += Harvesting.computeBytesConsumed(buf, pos2);
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 9);
            int pos3 = offset + 13 + fieldOffset3;
            pos3 += SoftBlock.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.breaking != null) {
            nullBits |= 0x1;
        }
        if (this.harvest != null) {
            nullBits |= 0x2;
        }
        if (this.soft != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        final int breakingOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int harvestOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int softOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.breaking != null) {
            buf.setIntLE(breakingOffsetSlot, buf.writerIndex() - varBlockStart);
            this.breaking.serialize(buf);
        }
        else {
            buf.setIntLE(breakingOffsetSlot, -1);
        }
        if (this.harvest != null) {
            buf.setIntLE(harvestOffsetSlot, buf.writerIndex() - varBlockStart);
            this.harvest.serialize(buf);
        }
        else {
            buf.setIntLE(harvestOffsetSlot, -1);
        }
        if (this.soft != null) {
            buf.setIntLE(softOffsetSlot, buf.writerIndex() - varBlockStart);
            this.soft.serialize(buf);
        }
        else {
            buf.setIntLE(softOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 13;
        if (this.breaking != null) {
            size += this.breaking.computeSize();
        }
        if (this.harvest != null) {
            size += this.harvest.computeSize();
        }
        if (this.soft != null) {
            size += this.soft.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 13) {
            return ValidationResult.error("Buffer too small: expected at least 13 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int breakingOffset = buffer.getIntLE(offset + 1);
            if (breakingOffset < 0) {
                return ValidationResult.error("Invalid offset for Breaking");
            }
            int pos = offset + 13 + breakingOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Breaking");
            }
            final ValidationResult breakingResult = BlockBreaking.validateStructure(buffer, pos);
            if (!breakingResult.isValid()) {
                return ValidationResult.error("Invalid Breaking: " + breakingResult.error());
            }
            pos += BlockBreaking.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int harvestOffset = buffer.getIntLE(offset + 5);
            if (harvestOffset < 0) {
                return ValidationResult.error("Invalid offset for Harvest");
            }
            int pos = offset + 13 + harvestOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Harvest");
            }
            final ValidationResult harvestResult = Harvesting.validateStructure(buffer, pos);
            if (!harvestResult.isValid()) {
                return ValidationResult.error("Invalid Harvest: " + harvestResult.error());
            }
            pos += Harvesting.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int softOffset = buffer.getIntLE(offset + 9);
            if (softOffset < 0) {
                return ValidationResult.error("Invalid offset for Soft");
            }
            int pos = offset + 13 + softOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Soft");
            }
            final ValidationResult softResult = SoftBlock.validateStructure(buffer, pos);
            if (!softResult.isValid()) {
                return ValidationResult.error("Invalid Soft: " + softResult.error());
            }
            pos += SoftBlock.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public BlockGathering clone() {
        final BlockGathering copy = new BlockGathering();
        copy.breaking = ((this.breaking != null) ? this.breaking.clone() : null);
        copy.harvest = ((this.harvest != null) ? this.harvest.clone() : null);
        copy.soft = ((this.soft != null) ? this.soft.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BlockGathering other) {
            return Objects.equals(this.breaking, other.breaking) && Objects.equals(this.harvest, other.harvest) && Objects.equals(this.soft, other.soft);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.breaking, this.harvest, this.soft);
    }
}
