// 
// 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 javax.annotation.Nullable;
import com.hypixel.hytale.protocol.Packet;

public class AssetEditorExportAssetInitialize implements Packet
{
    public static final int PACKET_ID = 343;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 6;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 14;
    public static final int MAX_SIZE = 81920066;
    @Nullable
    public AssetEditorAsset asset;
    @Nullable
    public AssetPath oldPath;
    public int size;
    public boolean failed;
    
    @Override
    public int getId() {
        return 343;
    }
    
    public AssetEditorExportAssetInitialize() {
    }
    
    public AssetEditorExportAssetInitialize(@Nullable final AssetEditorAsset asset, @Nullable final AssetPath oldPath, final int size, final boolean failed) {
        this.asset = asset;
        this.oldPath = oldPath;
        this.size = size;
        this.failed = failed;
    }
    
    public AssetEditorExportAssetInitialize(@Nonnull final AssetEditorExportAssetInitialize other) {
        this.asset = other.asset;
        this.oldPath = other.oldPath;
        this.size = other.size;
        this.failed = other.failed;
    }
    
    @Nonnull
    public static AssetEditorExportAssetInitialize deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetEditorExportAssetInitialize obj = new AssetEditorExportAssetInitialize();
        final byte nullBits = buf.getByte(offset);
        obj.size = buf.getIntLE(offset + 1);
        obj.failed = (buf.getByte(offset + 5) != 0);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 14 + buf.getIntLE(offset + 6);
            obj.asset = AssetEditorAsset.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 14 + buf.getIntLE(offset + 10);
            obj.oldPath = AssetPath.deserialize(buf, varPos2);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 14;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 6);
            int pos0 = offset + 14 + fieldOffset0;
            pos0 += AssetEditorAsset.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 10);
            int pos2 = offset + 14 + fieldOffset2;
            pos2 += AssetPath.computeBytesConsumed(buf, pos2);
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.asset != null) {
            nullBits |= 0x1;
        }
        if (this.oldPath != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.size);
        buf.writeByte(this.failed ? 1 : 0);
        final int assetOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int oldPathOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.asset != null) {
            buf.setIntLE(assetOffsetSlot, buf.writerIndex() - varBlockStart);
            this.asset.serialize(buf);
        }
        else {
            buf.setIntLE(assetOffsetSlot, -1);
        }
        if (this.oldPath != null) {
            buf.setIntLE(oldPathOffsetSlot, buf.writerIndex() - varBlockStart);
            this.oldPath.serialize(buf);
        }
        else {
            buf.setIntLE(oldPathOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 14;
        if (this.asset != null) {
            size += this.asset.computeSize();
        }
        if (this.oldPath != null) {
            size += this.oldPath.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 14) {
            return ValidationResult.error("Buffer too small: expected at least 14 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int assetOffset = buffer.getIntLE(offset + 6);
            if (assetOffset < 0) {
                return ValidationResult.error("Invalid offset for Asset");
            }
            int pos = offset + 14 + assetOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Asset");
            }
            final ValidationResult assetResult = AssetEditorAsset.validateStructure(buffer, pos);
            if (!assetResult.isValid()) {
                return ValidationResult.error("Invalid Asset: " + assetResult.error());
            }
            pos += AssetEditorAsset.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int oldPathOffset = buffer.getIntLE(offset + 10);
            if (oldPathOffset < 0) {
                return ValidationResult.error("Invalid offset for OldPath");
            }
            int pos = offset + 14 + oldPathOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for OldPath");
            }
            final ValidationResult oldPathResult = AssetPath.validateStructure(buffer, pos);
            if (!oldPathResult.isValid()) {
                return ValidationResult.error("Invalid OldPath: " + oldPathResult.error());
            }
            pos += AssetPath.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public AssetEditorExportAssetInitialize clone() {
        final AssetEditorExportAssetInitialize copy = new AssetEditorExportAssetInitialize();
        copy.asset = ((this.asset != null) ? this.asset.clone() : null);
        copy.oldPath = ((this.oldPath != null) ? this.oldPath.clone() : null);
        copy.size = this.size;
        copy.failed = this.failed;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetEditorExportAssetInitialize other) {
            return Objects.equals(this.asset, other.asset) && Objects.equals(this.oldPath, other.oldPath) && this.size == other.size && this.failed == other.failed;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.asset, this.oldPath, this.size, this.failed);
    }
}
