// 
// 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.InstantData;

public class SleepClock
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 33;
    public static final int VARIABLE_FIELD_COUNT = 0;
    public static final int VARIABLE_BLOCK_START = 33;
    public static final int MAX_SIZE = 33;
    @Nullable
    public InstantData startGametime;
    @Nullable
    public InstantData targetGametime;
    public float progress;
    public float durationSeconds;
    
    public SleepClock() {
    }
    
    public SleepClock(@Nullable final InstantData startGametime, @Nullable final InstantData targetGametime, final float progress, final float durationSeconds) {
        this.startGametime = startGametime;
        this.targetGametime = targetGametime;
        this.progress = progress;
        this.durationSeconds = durationSeconds;
    }
    
    public SleepClock(@Nonnull final SleepClock other) {
        this.startGametime = other.startGametime;
        this.targetGametime = other.targetGametime;
        this.progress = other.progress;
        this.durationSeconds = other.durationSeconds;
    }
    
    @Nonnull
    public static SleepClock deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final SleepClock obj = new SleepClock();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            obj.startGametime = InstantData.deserialize(buf, offset + 1);
        }
        if ((nullBits & 0x2) != 0x0) {
            obj.targetGametime = InstantData.deserialize(buf, offset + 13);
        }
        obj.progress = buf.getFloatLE(offset + 25);
        obj.durationSeconds = buf.getFloatLE(offset + 29);
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        return 33;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.startGametime != null) {
            nullBits |= 0x1;
        }
        if (this.targetGametime != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        if (this.startGametime != null) {
            this.startGametime.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        if (this.targetGametime != null) {
            this.targetGametime.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        buf.writeFloatLE(this.progress);
        buf.writeFloatLE(this.durationSeconds);
    }
    
    public int computeSize() {
        return 33;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 33) {
            return ValidationResult.error("Buffer too small: expected at least 33 bytes");
        }
        return ValidationResult.OK;
    }
    
    public SleepClock clone() {
        final SleepClock copy = new SleepClock();
        copy.startGametime = ((this.startGametime != null) ? this.startGametime.clone() : null);
        copy.targetGametime = ((this.targetGametime != null) ? this.targetGametime.clone() : null);
        copy.progress = this.progress;
        copy.durationSeconds = this.durationSeconds;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final SleepClock other) {
            return Objects.equals(this.startGametime, other.startGametime) && Objects.equals(this.targetGametime, other.targetGametime) && this.progress == other.progress && this.durationSeconds == other.durationSeconds;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.startGametime, this.targetGametime, this.progress, this.durationSeconds);
    }
}
