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

package com.hypixel.hytale.protocol;

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;

public class HostAddress
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 0;
    public static final int FIXED_BLOCK_SIZE = 2;
    public static final int VARIABLE_FIELD_COUNT = 1;
    public static final int VARIABLE_BLOCK_START = 2;
    public static final int MAX_SIZE = 1031;
    @Nonnull
    public String host;
    public short port;
    
    public HostAddress() {
        this.host = "";
    }
    
    public HostAddress(@Nonnull final String host, final short port) {
        this.host = "";
        this.host = host;
        this.port = port;
    }
    
    public HostAddress(@Nonnull final HostAddress other) {
        this.host = "";
        this.host = other.host;
        this.port = other.port;
    }
    
    @Nonnull
    public static HostAddress deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final HostAddress obj = new HostAddress();
        obj.port = buf.getShortLE(offset + 0);
        int pos = offset + 2;
        final int hostLen = VarInt.peek(buf, pos);
        if (hostLen < 0) {
            throw ProtocolException.negativeLength("Host", hostLen);
        }
        if (hostLen > 256) {
            throw ProtocolException.stringTooLong("Host", hostLen, 256);
        }
        final int hostVarLen = VarInt.length(buf, pos);
        obj.host = PacketIO.readVarString(buf, pos, PacketIO.UTF8);
        pos += hostVarLen + hostLen;
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        int pos = offset + 2;
        final int sl = VarInt.peek(buf, pos);
        pos += VarInt.length(buf, pos) + sl;
        return pos - offset;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        buf.writeShortLE(this.port);
        PacketIO.writeVarString(buf, this.host, 256);
    }
    
    public int computeSize() {
        int size = 2;
        size += PacketIO.stringSize(this.host);
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 2) {
            return ValidationResult.error("Buffer too small: expected at least 2 bytes");
        }
        int pos = offset + 2;
        final int hostLen = VarInt.peek(buffer, pos);
        if (hostLen < 0) {
            return ValidationResult.error("Invalid string length for Host");
        }
        if (hostLen > 256) {
            return ValidationResult.error("Host exceeds max length 256");
        }
        pos += VarInt.length(buffer, pos);
        pos += hostLen;
        if (pos > buffer.writerIndex()) {
            return ValidationResult.error("Buffer overflow reading Host");
        }
        return ValidationResult.OK;
    }
    
    public HostAddress clone() {
        final HostAddress copy = new HostAddress();
        copy.host = this.host;
        copy.port = this.port;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final HostAddress other) {
            return Objects.equals(this.host, other.host) && this.port == other.port;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.host, this.port);
    }
}
