// 
// 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 DeployableConfig
{
    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 = 2058;
    @Nullable
    public Model model;
    @Nullable
    public Model modelPreview;
    public boolean allowPlaceOnWalls;
    
    public DeployableConfig() {
    }
    
    public DeployableConfig(@Nullable final Model model, @Nullable final Model modelPreview, final boolean allowPlaceOnWalls) {
        this.model = model;
        this.modelPreview = modelPreview;
        this.allowPlaceOnWalls = allowPlaceOnWalls;
    }
    
    public DeployableConfig(@Nonnull final DeployableConfig other) {
        this.model = other.model;
        this.modelPreview = other.modelPreview;
        this.allowPlaceOnWalls = other.allowPlaceOnWalls;
    }
    
    @Nonnull
    public static DeployableConfig deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final DeployableConfig obj = new DeployableConfig();
        final byte nullBits = buf.getByte(offset);
        obj.allowPlaceOnWalls = (buf.getByte(offset + 1) != 0);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 10 + buf.getIntLE(offset + 2);
            obj.model = Model.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 10 + buf.getIntLE(offset + 6);
            obj.modelPreview = Model.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 += Model.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 += Model.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.model != null) {
            nullBits |= 0x1;
        }
        if (this.modelPreview != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.allowPlaceOnWalls ? 1 : 0);
        final int modelOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int modelPreviewOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.model != null) {
            buf.setIntLE(modelOffsetSlot, buf.writerIndex() - varBlockStart);
            this.model.serialize(buf);
        }
        else {
            buf.setIntLE(modelOffsetSlot, -1);
        }
        if (this.modelPreview != null) {
            buf.setIntLE(modelPreviewOffsetSlot, buf.writerIndex() - varBlockStart);
            this.modelPreview.serialize(buf);
        }
        else {
            buf.setIntLE(modelPreviewOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 10;
        if (this.model != null) {
            size += this.model.computeSize();
        }
        if (this.modelPreview != null) {
            size += this.modelPreview.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 modelOffset = buffer.getIntLE(offset + 2);
            if (modelOffset < 0) {
                return ValidationResult.error("Invalid offset for Model");
            }
            int pos = offset + 10 + modelOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Model");
            }
            final ValidationResult modelResult = Model.validateStructure(buffer, pos);
            if (!modelResult.isValid()) {
                return ValidationResult.error("Invalid Model: " + modelResult.error());
            }
            pos += Model.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int modelPreviewOffset = buffer.getIntLE(offset + 6);
            if (modelPreviewOffset < 0) {
                return ValidationResult.error("Invalid offset for ModelPreview");
            }
            int pos = offset + 10 + modelPreviewOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ModelPreview");
            }
            final ValidationResult modelPreviewResult = Model.validateStructure(buffer, pos);
            if (!modelPreviewResult.isValid()) {
                return ValidationResult.error("Invalid ModelPreview: " + modelPreviewResult.error());
            }
            pos += Model.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public DeployableConfig clone() {
        final DeployableConfig copy = new DeployableConfig();
        copy.model = ((this.model != null) ? this.model.clone() : null);
        copy.modelPreview = ((this.modelPreview != null) ? this.modelPreview.clone() : null);
        copy.allowPlaceOnWalls = this.allowPlaceOnWalls;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final DeployableConfig other) {
            return Objects.equals(this.model, other.model) && Objects.equals(this.modelPreview, other.modelPreview) && this.allowPlaceOnWalls == other.allowPlaceOnWalls;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.model, this.modelPreview, this.allowPlaceOnWalls);
    }
}
