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

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

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

public class ServerInfo implements Packet
{
    public static final int PACKET_ID = 223;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 5;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 13;
    public static final int MAX_SIZE = 32768023;
    @Nullable
    public String serverName;
    @Nullable
    public String motd;
    public int maxPlayers;
    
    @Override
    public int getId() {
        return 223;
    }
    
    public ServerInfo() {
    }
    
    public ServerInfo(@Nullable final String serverName, @Nullable final String motd, final int maxPlayers) {
        this.serverName = serverName;
        this.motd = motd;
        this.maxPlayers = maxPlayers;
    }
    
    public ServerInfo(@Nonnull final ServerInfo other) {
        this.serverName = other.serverName;
        this.motd = other.motd;
        this.maxPlayers = other.maxPlayers;
    }
    
    @Nonnull
    public static ServerInfo deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ServerInfo obj = new ServerInfo();
        final byte nullBits = buf.getByte(offset);
        obj.maxPlayers = buf.getIntLE(offset + 1);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 13 + buf.getIntLE(offset + 5);
            final int serverNameLen = VarInt.peek(buf, varPos0);
            if (serverNameLen < 0) {
                throw ProtocolException.negativeLength("ServerName", serverNameLen);
            }
            if (serverNameLen > 4096000) {
                throw ProtocolException.stringTooLong("ServerName", serverNameLen, 4096000);
            }
            obj.serverName = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 13 + buf.getIntLE(offset + 9);
            final int motdLen = VarInt.peek(buf, varPos2);
            if (motdLen < 0) {
                throw ProtocolException.negativeLength("Motd", motdLen);
            }
            if (motdLen > 4096000) {
                throw ProtocolException.stringTooLong("Motd", motdLen, 4096000);
            }
            obj.motd = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 13;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 5);
            int pos0 = offset + 13 + fieldOffset0;
            final int sl = VarInt.peek(buf, pos0);
            pos0 += VarInt.length(buf, pos0) + sl;
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 9);
            int pos2 = offset + 13 + fieldOffset2;
            final int sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        return maxEnd;
    }
    
    @Override
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.serverName != null) {
            nullBits |= 0x1;
        }
        if (this.motd != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        buf.writeIntLE(this.maxPlayers);
        final int serverNameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int motdOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.serverName != null) {
            buf.setIntLE(serverNameOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.serverName, 4096000);
        }
        else {
            buf.setIntLE(serverNameOffsetSlot, -1);
        }
        if (this.motd != null) {
            buf.setIntLE(motdOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.motd, 4096000);
        }
        else {
            buf.setIntLE(motdOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 13;
        if (this.serverName != null) {
            size += PacketIO.stringSize(this.serverName);
        }
        if (this.motd != null) {
            size += PacketIO.stringSize(this.motd);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 13) {
            return ValidationResult.error("Buffer too small: expected at least 13 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int serverNameOffset = buffer.getIntLE(offset + 5);
            if (serverNameOffset < 0) {
                return ValidationResult.error("Invalid offset for ServerName");
            }
            int pos = offset + 13 + serverNameOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for ServerName");
            }
            final int serverNameLen = VarInt.peek(buffer, pos);
            if (serverNameLen < 0) {
                return ValidationResult.error("Invalid string length for ServerName");
            }
            if (serverNameLen > 4096000) {
                return ValidationResult.error("ServerName exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += serverNameLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading ServerName");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int motdOffset = buffer.getIntLE(offset + 9);
            if (motdOffset < 0) {
                return ValidationResult.error("Invalid offset for Motd");
            }
            int pos = offset + 13 + motdOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Motd");
            }
            final int motdLen = VarInt.peek(buffer, pos);
            if (motdLen < 0) {
                return ValidationResult.error("Invalid string length for Motd");
            }
            if (motdLen > 4096000) {
                return ValidationResult.error("Motd exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += motdLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Motd");
            }
        }
        return ValidationResult.OK;
    }
    
    public ServerInfo clone() {
        final ServerInfo copy = new ServerInfo();
        copy.serverName = this.serverName;
        copy.motd = this.motd;
        copy.maxPlayers = this.maxPlayers;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ServerInfo other) {
            return Objects.equals(this.serverName, other.serverName) && Objects.equals(this.motd, other.motd) && this.maxPlayers == other.maxPlayers;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.serverName, this.motd, this.maxPlayers);
    }
}
