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

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

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;
import com.hypixel.hytale.protocol.Packet;

public class Pong implements Packet
{
    public static final int PACKET_ID = 3;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 20;
    public static final int VARIABLE_FIELD_COUNT = 0;
    public static final int VARIABLE_BLOCK_START = 20;
    public static final int MAX_SIZE = 20;
    public int id;
    @Nullable
    public InstantData time;
    @Nonnull
    public PongType type;
    public short packetQueueSize;
    
    @Override
    public int getId() {
        return 3;
    }
    
    public Pong() {
        this.type = PongType.Raw;
    }
    
    public Pong(final int id, @Nullable final InstantData time, @Nonnull final PongType type, final short packetQueueSize) {
        this.type = PongType.Raw;
        this.id = id;
        this.time = time;
        this.type = type;
        this.packetQueueSize = packetQueueSize;
    }
    
    public Pong(@Nonnull final Pong other) {
        this.type = PongType.Raw;
        this.id = other.id;
        this.time = other.time;
        this.type = other.type;
        this.packetQueueSize = other.packetQueueSize;
    }
    
    @Nonnull
    public static Pong deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final Pong obj = new Pong();
        final byte nullBits = buf.getByte(offset);
        obj.id = buf.getIntLE(offset + 1);
        if ((nullBits & 0x1) != 0x0) {
            obj.time = InstantData.deserialize(buf, offset + 5);
        }
        obj.type = PongType.fromValue(buf.getByte(offset + 17));
        obj.packetQueueSize = buf.getShortLE(offset + 18);
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        return 20;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        byte nullBits = 0;
        if (this.time != null) {
            nullBits |= 0x1;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.id);
        if (this.time != null) {
            this.time.serialize(buf);
        }
        else {
            buf.writeZero(12);
        }
        buf.writeByte(this.type.getValue());
        buf.writeShortLE(this.packetQueueSize);
    }
    
    @Override
    public int computeSize() {
        return 20;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 20) {
            return ValidationResult.error("Buffer too small: expected at least 20 bytes");
        }
        return ValidationResult.OK;
    }
    
    public Pong clone() {
        final Pong copy = new Pong();
        copy.id = this.id;
        copy.time = ((this.time != null) ? this.time.clone() : null);
        copy.type = this.type;
        copy.packetQueueSize = this.packetQueueSize;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final Pong other) {
            return this.id == other.id && Objects.equals(this.time, other.time) && Objects.equals(this.type, other.type) && this.packetQueueSize == other.packetQueueSize;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.id, this.time, this.type, this.packetQueueSize);
    }
}
