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

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

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 UpdateSleepState implements Packet
{
    public static final int PACKET_ID = 157;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 36;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 36;
    public static final int MAX_SIZE = 65536050;
    public boolean grayFade;
    public boolean sleepUi;
    @Nullable
    public SleepClock clock;
    @Nullable
    public SleepMultiplayer multiplayer;
    
    @Override
    public int getId() {
        return 157;
    }
    
    public UpdateSleepState() {
    }
    
    public UpdateSleepState(final boolean grayFade, final boolean sleepUi, @Nullable final SleepClock clock, @Nullable final SleepMultiplayer multiplayer) {
        this.grayFade = grayFade;
        this.sleepUi = sleepUi;
        this.clock = clock;
        this.multiplayer = multiplayer;
    }
    
    public UpdateSleepState(@Nonnull final UpdateSleepState other) {
        this.grayFade = other.grayFade;
        this.sleepUi = other.sleepUi;
        this.clock = other.clock;
        this.multiplayer = other.multiplayer;
    }
    
    @Nonnull
    public static UpdateSleepState deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final UpdateSleepState obj = new UpdateSleepState();
        final byte nullBits = buf.getByte(offset);
        obj.grayFade = (buf.getByte(offset + 1) != 0);
        obj.sleepUi = (buf.getByte(offset + 2) != 0);
        if ((nullBits & 0x1) != 0x0) {
            obj.clock = SleepClock.deserialize(buf, offset + 3);
        }
        int pos = offset + 36;
        if ((nullBits & 0x2) != 0x0) {
            obj.multiplayer = SleepMultiplayer.deserialize(buf, pos);
            pos += SleepMultiplayer.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 + 36;
        if ((nullBits & 0x2) != 0x0) {
            pos += SleepMultiplayer.computeBytesConsumed(buf, pos);
        }
        return pos - offset;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.clock != null) {
            nullBits |= 0x1;
        }
        if (this.multiplayer != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.grayFade ? 1 : 0);
        buf.writeByte(this.sleepUi ? 1 : 0);
        if (this.clock != null) {
            this.clock.serialize(buf);
        }
        else {
            buf.writeZero(33);
        }
        if (this.multiplayer != null) {
            this.multiplayer.serialize(buf);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 36;
        if (this.multiplayer != null) {
            size += this.multiplayer.computeSize();
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 36) {
            return ValidationResult.error("Buffer too small: expected at least 36 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        int pos = offset + 36;
        if ((nullBits & 0x2) != 0x0) {
            final ValidationResult multiplayerResult = SleepMultiplayer.validateStructure(buffer, pos);
            if (!multiplayerResult.isValid()) {
                return ValidationResult.error("Invalid Multiplayer: " + multiplayerResult.error());
            }
            pos += SleepMultiplayer.computeBytesConsumed(buffer, pos);
        }
        return ValidationResult.OK;
    }
    
    public UpdateSleepState clone() {
        final UpdateSleepState copy = new UpdateSleepState();
        copy.grayFade = this.grayFade;
        copy.sleepUi = this.sleepUi;
        copy.clock = ((this.clock != null) ? this.clock.clone() : null);
        copy.multiplayer = ((this.multiplayer != null) ? this.multiplayer.clone() : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final UpdateSleepState other) {
            return this.grayFade == other.grayFade && this.sleepUi == other.sleepUi && Objects.equals(this.clock, other.clock) && Objects.equals(this.multiplayer, other.multiplayer);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.grayFade, this.sleepUi, this.clock, this.multiplayer);
    }
}
