// 
// 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 ForkedChainId
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 9;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 1033;
    public int entryIndex;
    public int subIndex;
    @Nullable
    public ForkedChainId forkedId;
    
    public ForkedChainId() {
    }
    
    public ForkedChainId(final int entryIndex, final int subIndex, @Nullable final ForkedChainId forkedId) {
        this.entryIndex = entryIndex;
        this.subIndex = subIndex;
        this.forkedId = forkedId;
    }
    
    public ForkedChainId(@Nonnull final ForkedChainId other) {
        this.entryIndex = other.entryIndex;
        this.subIndex = other.subIndex;
        this.forkedId = other.forkedId;
    }
    
    @Nonnull
    public static ForkedChainId deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ForkedChainId obj = new ForkedChainId();
        final byte nullBits = buf.getByte(offset);
        obj.entryIndex = buf.getIntLE(offset + 1);
        obj.subIndex = buf.getIntLE(offset + 5);
        int pos = offset + 9;
        if ((nullBits & 0x1) != 0x0) {
            obj.forkedId = deserialize(buf, pos);
            pos += computeBytesConsumed(buf, pos);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 9;
        if ((nullBits & 0x1) != 0x0) {
            pos += computeBytesConsumed(buf, pos);
        }
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.forkedId != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.entryIndex);
        buf.writeIntLE(this.subIndex);
        if (this.forkedId != null) {
            this.forkedId.serialize(buf);
        }
    }
    
    public int computeSize() {
        int size = 9;
        if (this.forkedId != null) {
            size += this.forkedId.computeSize();
        }
        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);
        int pos = offset + 9;
        if ((nullBits & 0x1) != 0x0) {
            final ValidationResult forkedIdResult = validateStructure(buffer, pos);
            if (!forkedIdResult.isValid()) {
                return ValidationResult.error("Invalid ForkedId: " + forkedIdResult.error());
            }
            pos += computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public ForkedChainId clone() {
        final ForkedChainId copy = new ForkedChainId();
        copy.entryIndex = this.entryIndex;
        copy.subIndex = this.subIndex;
        copy.forkedId = ((this.forkedId != null) ? this.forkedId.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ForkedChainId other) {
            return this.entryIndex == other.entryIndex && this.subIndex == other.subIndex && Objects.equals(this.forkedId, other.forkedId);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.entryIndex, this.subIndex, this.forkedId);
    }
}
