// 
// 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 BlockMatcher
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 3;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 3;
    public static final int MAX_SIZE = 32768026;
    @Nullable
    public BlockIdMatcher block;
    @Nonnull
    public BlockFace face;
    public boolean staticFace;
    
    public BlockMatcher() {
        this.face = BlockFace.None;
    }
    
    public BlockMatcher(@Nullable final BlockIdMatcher block, @Nonnull final BlockFace face, final boolean staticFace) {
        this.face = BlockFace.None;
        this.block = block;
        this.face = face;
        this.staticFace = staticFace;
    }
    
    public BlockMatcher(@Nonnull final BlockMatcher other) {
        this.face = BlockFace.None;
        this.block = other.block;
        this.face = other.face;
        this.staticFace = other.staticFace;
    }
    
    @Nonnull
    public static BlockMatcher deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final BlockMatcher obj = new BlockMatcher();
        final byte nullBits = buf.getByte(offset);
        obj.face = BlockFace.fromValue(buf.getByte(offset + 1));
        obj.staticFace = (buf.getByte(offset + 2) != 0);
        int pos = offset + 3;
        if ((nullBits & 0x1) != 0x0) {
            obj.block = BlockIdMatcher.deserialize(buf, pos);
            pos += BlockIdMatcher.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 + 3;
        if ((nullBits & 0x1) != 0x0) {
            pos += BlockIdMatcher.computeBytesConsumed(buf, pos);
        }
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.block != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.face.getValue());
        buf.writeByte(this.staticFace ? 1 : 0);
        if (this.block != null) {
            this.block.serialize(buf);
        }
    }
    
    public int computeSize() {
        int size = 3;
        if (this.block != null) {
            size += this.block.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 3) {
            return ValidationResult.error("Buffer too small: expected at least 3 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 3;
        if ((nullBits & 0x1) != 0x0) {
            final ValidationResult blockResult = BlockIdMatcher.validateStructure(buffer, pos);
            if (!blockResult.isValid()) {
                return ValidationResult.error("Invalid Block: " + blockResult.error());
            }
            pos += BlockIdMatcher.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public BlockMatcher clone() {
        final BlockMatcher copy = new BlockMatcher();
        copy.block = ((this.block != null) ? this.block.clone() : null);
        copy.face = this.face;
        copy.staticFace = this.staticFace;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final BlockMatcher other) {
            return Objects.equals(this.block, other.block) && Objects.equals(this.face, other.face) && this.staticFace == other.staticFace;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.block, this.face, this.staticFace);
    }
}
