// 
// 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 AssetEditorRenameDirectory implements Packet
{
    public static final int PACKET_ID = 309;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 5;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 13;
    public static final int MAX_SIZE = 65536051;
    public int token;
    @Nullable
    public AssetPath path;
    @Nullable
    public AssetPath newPath;
    
    @Override
    public int getId() {
        return 309;
    }
    
    public AssetEditorRenameDirectory() {
    }
    
    public AssetEditorRenameDirectory(final int token, @Nullable final AssetPath path, @Nullable final AssetPath newPath) {
        this.token = token;
        this.path = path;
        this.newPath = newPath;
    }
    
    public AssetEditorRenameDirectory(@Nonnull final AssetEditorRenameDirectory other) {
        this.token = other.token;
        this.path = other.path;
        this.newPath = other.newPath;
    }
    
    @Nonnull
    public static AssetEditorRenameDirectory deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final AssetEditorRenameDirectory obj = new AssetEditorRenameDirectory();
        final byte nullBits = buf.getByte(offset);
        obj.token = buf.getIntLE(offset + 1);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 13 + buf.getIntLE(offset + 5);
            obj.path = AssetPath.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 13 + buf.getIntLE(offset + 9);
            obj.newPath = 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 = 13;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 5);
            int pos0 = offset + 13 + fieldOffset0;
            pos0 += AssetPath.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 9);
            int pos2 = offset + 13 + 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.path != null) {
            nullBits |= 0x1;
        }
        if (this.newPath != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.token);
        final int pathOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int newPathOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.path != null) {
            buf.setIntLE(pathOffsetSlot, buf.writerIndex() - varBlockStart);
            this.path.serialize(buf);
        }
        else {
            buf.setIntLE(pathOffsetSlot, -1);
        }
        if (this.newPath != null) {
            buf.setIntLE(newPathOffsetSlot, buf.writerIndex() - varBlockStart);
            this.newPath.serialize(buf);
        }
        else {
            buf.setIntLE(newPathOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 13;
        if (this.path != null) {
            size += this.path.computeSize();
        }
        if (this.newPath != null) {
            size += this.newPath.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 13) {
            return ValidationResult.error("Buffer too small: expected at least 13 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int pathOffset = buffer.getIntLE(offset + 5);
            if (pathOffset < 0) {
                return ValidationResult.error("Invalid offset for Path");
            }
            int pos = offset + 13 + pathOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Path");
            }
            final ValidationResult pathResult = AssetPath.validateStructure(buffer, pos);
            if (!pathResult.isValid()) {
                return ValidationResult.error("Invalid Path: " + pathResult.error());
            }
            pos += AssetPath.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int newPathOffset = buffer.getIntLE(offset + 9);
            if (newPathOffset < 0) {
                return ValidationResult.error("Invalid offset for NewPath");
            }
            int pos = offset + 13 + newPathOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for NewPath");
            }
            final ValidationResult newPathResult = AssetPath.validateStructure(buffer, pos);
            if (!newPathResult.isValid()) {
                return ValidationResult.error("Invalid NewPath: " + newPathResult.error());
            }
            pos += AssetPath.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public AssetEditorRenameDirectory clone() {
        final AssetEditorRenameDirectory copy = new AssetEditorRenameDirectory();
        copy.token = this.token;
        copy.path = ((this.path != null) ? this.path.clone() : null);
        copy.newPath = ((this.newPath != null) ? this.newPath.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final AssetEditorRenameDirectory other) {
            return this.token == other.token && Objects.equals(this.path, other.path) && Objects.equals(this.newPath, other.newPath);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.token, this.path, this.newPath);
    }
}
