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

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

import java.util.Arrays;
import com.hypixel.hytale.protocol.io.ValidationResult;
import com.hypixel.hytale.protocol.io.PacketIO;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import io.netty.buffer.ByteBuf;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.UUID;

public class SleepMultiplayer
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 9;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 65536014;
    public int sleepersCount;
    public int awakeCount;
    @Nullable
    public UUID[] awakeSample;
    
    public SleepMultiplayer() {
    }
    
    public SleepMultiplayer(final int sleepersCount, final int awakeCount, @Nullable final UUID[] awakeSample) {
        this.sleepersCount = sleepersCount;
        this.awakeCount = awakeCount;
        this.awakeSample = awakeSample;
    }
    
    public SleepMultiplayer(@Nonnull final SleepMultiplayer other) {
        this.sleepersCount = other.sleepersCount;
        this.awakeCount = other.awakeCount;
        this.awakeSample = other.awakeSample;
    }
    
    @Nonnull
    public static SleepMultiplayer deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final SleepMultiplayer obj = new SleepMultiplayer();
        final byte nullBits = buf.getByte(offset);
        obj.sleepersCount = buf.getIntLE(offset + 1);
        obj.awakeCount = buf.getIntLE(offset + 5);
        int pos = offset + 9;
        if ((nullBits & 0x1) != 0x0) {
            final int awakeSampleCount = VarInt.peek(buf, pos);
            if (awakeSampleCount < 0) {
                throw ProtocolException.negativeLength("AwakeSample", awakeSampleCount);
            }
            if (awakeSampleCount > 4096000) {
                throw ProtocolException.arrayTooLong("AwakeSample", awakeSampleCount, 4096000);
            }
            final int awakeSampleVarLen = VarInt.size(awakeSampleCount);
            if (pos + awakeSampleVarLen + awakeSampleCount * 16L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("AwakeSample", pos + awakeSampleVarLen + awakeSampleCount * 16, buf.readableBytes());
            }
            pos += awakeSampleVarLen;
            obj.awakeSample = new UUID[awakeSampleCount];
            for (int i = 0; i < awakeSampleCount; ++i) {
                obj.awakeSample[i] = PacketIO.readUUID(buf, pos + i * 16);
            }
            pos += awakeSampleCount * 16;
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int pos = offset + 9;
        if ((nullBits & 0x1) != 0x0) {
            final int arrLen = VarInt.peek(buf, pos);
            pos += VarInt.length(buf, pos) + arrLen * 16;
        }
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.awakeSample != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.sleepersCount);
        buf.writeIntLE(this.awakeCount);
        if (this.awakeSample != null) {
            if (this.awakeSample.length > 4096000) {
                throw ProtocolException.arrayTooLong("AwakeSample", this.awakeSample.length, 4096000);
            }
            VarInt.write(buf, this.awakeSample.length);
            for (final UUID item : this.awakeSample) {
                PacketIO.writeUUID(buf, item);
            }
        }
    }
    
    public int computeSize() {
        int size = 9;
        if (this.awakeSample != null) {
            size += VarInt.size(this.awakeSample.length) + this.awakeSample.length * 16;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 9) {
            return ValidationResult.error("Buffer too small: expected at least 9 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 9;
        if ((nullBits & 0x1) != 0x0) {
            final int awakeSampleCount = VarInt.peek(buffer, pos);
            if (awakeSampleCount < 0) {
                return ValidationResult.error("Invalid array count for AwakeSample");
            }
            if (awakeSampleCount > 4096000) {
                return ValidationResult.error("AwakeSample exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += awakeSampleCount * 16;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading AwakeSample");
            }
        }
        return ValidationResult.OK;
    }
    
    public SleepMultiplayer clone() {
        final SleepMultiplayer copy = new SleepMultiplayer();
        copy.sleepersCount = this.sleepersCount;
        copy.awakeCount = this.awakeCount;
        copy.awakeSample = (UUID[])((this.awakeSample != null) ? ((UUID[])Arrays.copyOf(this.awakeSample, this.awakeSample.length)) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final SleepMultiplayer other) {
            return this.sleepersCount == other.sleepersCount && this.awakeCount == other.awakeCount && Arrays.equals(this.awakeSample, other.awakeSample);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Integer.hashCode(this.sleepersCount);
        result = 31 * result + Integer.hashCode(this.awakeCount);
        result = 31 * result + Arrays.hashCode(this.awakeSample);
        return result;
    }
}
