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

package com.hypixel.hytale.protocol.packets.asseteditor;

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

public class AssetEditorUpdateModelPreview implements Packet
{
    public static final int PACKET_ID = 355;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 30;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 42;
    public static final int MAX_SIZE = 1677721600;
    @Nullable
    public AssetPath assetPath;
    @Nullable
    public Model model;
    @Nullable
    public BlockType block;
    @Nullable
    public AssetEditorPreviewCameraSettings camera;
    
    @Override
    public int getId() {
        return 355;
    }
    
    public AssetEditorUpdateModelPreview() {
    }
    
    public AssetEditorUpdateModelPreview(@Nullable final AssetPath assetPath, @Nullable final Model model, @Nullable final BlockType block, @Nullable final AssetEditorPreviewCameraSettings camera) {
        this.assetPath = assetPath;
        this.model = model;
        this.block = block;
        this.camera = camera;
    }
    
    public AssetEditorUpdateModelPreview(@Nonnull final AssetEditorUpdateModelPreview other) {
        this.assetPath = other.assetPath;
        this.model = other.model;
        this.block = other.block;
        this.camera = other.camera;
    }
    
    @Nonnull
    public static AssetEditorUpdateModelPreview deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetEditorUpdateModelPreview obj = new AssetEditorUpdateModelPreview();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.camera = AssetEditorPreviewCameraSettings.deserialize(buf, offset + 1);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos0 = offset + 42 + buf.getIntLE(offset + 30);
            obj.assetPath = AssetPath.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos2 = offset + 42 + buf.getIntLE(offset + 34);
            obj.model = Model.deserialize(buf, varPos2);
        }
        if ((nullBits & 0x8) != 0x0) {
            final int varPos3 = offset + 42 + buf.getIntLE(offset + 38);
            obj.block = BlockType.deserialize(buf, varPos3);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 42;
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 30);
            int pos0 = offset + 42 + fieldOffset0;
            pos0 += AssetPath.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 34);
            int pos2 = offset + 42 + fieldOffset2;
            pos2 += Model.computeBytesConsumed(buf, pos2);
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x8) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 38);
            int pos3 = offset + 42 + fieldOffset3;
            pos3 += BlockType.computeBytesConsumed(buf, pos3);
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.camera != null) {
            nullBits |= 0x1;
        }
        if (this.assetPath != null) {
            nullBits |= 0x2;
        }
        if (this.model != null) {
            nullBits |= 0x4;
        }
        if (this.block != null) {
            nullBits |= 0x8;
        }
        buf.writeByte(nullBits);
        if (this.camera != null) {
            this.camera.serialize(buf);
        }
        else {
            buf.writeZero(29);
        }
        final int assetPathOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int modelOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int blockOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.assetPath != null) {
            buf.setIntLE(assetPathOffsetSlot, buf.writerIndex() - varBlockStart);
            this.assetPath.serialize(buf);
        }
        else {
            buf.setIntLE(assetPathOffsetSlot, -1);
        }
        if (this.model != null) {
            buf.setIntLE(modelOffsetSlot, buf.writerIndex() - varBlockStart);
            this.model.serialize(buf);
        }
        else {
            buf.setIntLE(modelOffsetSlot, -1);
        }
        if (this.block != null) {
            buf.setIntLE(blockOffsetSlot, buf.writerIndex() - varBlockStart);
            this.block.serialize(buf);
        }
        else {
            buf.setIntLE(blockOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 42;
        if (this.assetPath != null) {
            size += this.assetPath.computeSize();
        }
        if (this.model != null) {
            size += this.model.computeSize();
        }
        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 < 42) {
            return ValidationResult.error("Buffer too small: expected at least 42 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x2) != 0x0) {
            final int assetPathOffset = buffer.getIntLE(offset + 30);
            if (assetPathOffset < 0) {
                return ValidationResult.error("Invalid offset for AssetPath");
            }
            int pos = offset + 42 + assetPathOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for AssetPath");
            }
            final ValidationResult assetPathResult = AssetPath.validateStructure(buffer, pos);
            if (!assetPathResult.isValid()) {
                return ValidationResult.error("Invalid AssetPath: " + assetPathResult.error());
            }
            pos += AssetPath.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int modelOffset = buffer.getIntLE(offset + 34);
            if (modelOffset < 0) {
                return ValidationResult.error("Invalid offset for Model");
            }
            int pos = offset + 42 + 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 & 0x8) != 0x0) {
            final int blockOffset = buffer.getIntLE(offset + 38);
            if (blockOffset < 0) {
                return ValidationResult.error("Invalid offset for Block");
            }
            int pos = offset + 42 + blockOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Block");
            }
            final ValidationResult blockResult = BlockType.validateStructure(buffer, pos);
            if (!blockResult.isValid()) {
                return ValidationResult.error("Invalid Block: " + blockResult.error());
            }
            pos += BlockType.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public AssetEditorUpdateModelPreview clone() {
        final AssetEditorUpdateModelPreview copy = new AssetEditorUpdateModelPreview();
        copy.assetPath = ((this.assetPath != null) ? this.assetPath.clone() : null);
        copy.model = ((this.model != null) ? this.model.clone() : null);
        copy.block = ((this.block != null) ? this.block.clone() : null);
        copy.camera = ((this.camera != null) ? this.camera.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetEditorUpdateModelPreview other) {
            return Objects.equals(this.assetPath, other.assetPath) && Objects.equals(this.model, other.model) && Objects.equals(this.block, other.block) && Objects.equals(this.camera, other.camera);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.assetPath, this.model, this.block, this.camera);
    }
}
