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

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

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

public class UpdateObjectiveTask implements Packet
{
    public static final int PACKET_ID = 71;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 21;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 21;
    public static final int MAX_SIZE = 16384035;
    @Nonnull
    public UUID objectiveUuid;
    public int taskIndex;
    @Nullable
    public ObjectiveTask task;
    
    @Override
    public int getId() {
        return 71;
    }
    
    public UpdateObjectiveTask() {
        this.objectiveUuid = new UUID(0L, 0L);
    }
    
    public UpdateObjectiveTask(@Nonnull final UUID objectiveUuid, final int taskIndex, @Nullable final ObjectiveTask task) {
        this.objectiveUuid = new UUID(0L, 0L);
        this.objectiveUuid = objectiveUuid;
        this.taskIndex = taskIndex;
        this.task = task;
    }
    
    public UpdateObjectiveTask(@Nonnull final UpdateObjectiveTask other) {
        this.objectiveUuid = new UUID(0L, 0L);
        this.objectiveUuid = other.objectiveUuid;
        this.taskIndex = other.taskIndex;
        this.task = other.task;
    }
    
    @Nonnull
    public static UpdateObjectiveTask deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final UpdateObjectiveTask obj = new UpdateObjectiveTask();
        final byte nullBits = buf.getByte(offset);
        obj.objectiveUuid = PacketIO.readUUID(buf, offset + 1);
        obj.taskIndex = buf.getIntLE(offset + 17);
        int pos = offset + 21;
        if ((nullBits & 0x1) != 0x0) {
            obj.task = ObjectiveTask.deserialize(buf, pos);
            pos += ObjectiveTask.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 + 21;
        if ((nullBits & 0x1) != 0x0) {
            pos += ObjectiveTask.computeBytesConsumed(buf, pos);
        }
        return pos - offset;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.task != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        PacketIO.writeUUID(buf, this.objectiveUuid);
        buf.writeIntLE(this.taskIndex);
        if (this.task != null) {
            this.task.serialize(buf);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 21;
        if (this.task != null) {
            size += this.task.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 21) {
            return ValidationResult.error("Buffer too small: expected at least 21 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 21;
        if ((nullBits & 0x1) != 0x0) {
            final ValidationResult taskResult = ObjectiveTask.validateStructure(buffer, pos);
            if (!taskResult.isValid()) {
                return ValidationResult.error("Invalid Task: " + taskResult.error());
            }
            pos += ObjectiveTask.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public UpdateObjectiveTask clone() {
        final UpdateObjectiveTask copy = new UpdateObjectiveTask();
        copy.objectiveUuid = this.objectiveUuid;
        copy.taskIndex = this.taskIndex;
        copy.task = ((this.task != null) ? this.task.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final UpdateObjectiveTask other) {
            return Objects.equals(this.objectiveUuid, other.objectiveUuid) && this.taskIndex == other.taskIndex && Objects.equals(this.task, other.task);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.objectiveUuid, this.taskIndex, this.task);
    }
}
