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

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

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

public class ClientReferral implements Packet
{
    public static final int PACKET_ID = 18;
    public static final boolean IS_COMPRESSED = false;
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 1;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 9;
    public static final int MAX_SIZE = 5141;
    @Nullable
    public HostAddress hostTo;
    @Nullable
    public byte[] data;
    
    @Override
    public int getId() {
        return 18;
    }
    
    public ClientReferral() {
    }
    
    public ClientReferral(@Nullable final HostAddress hostTo, @Nullable final byte[] data) {
        this.hostTo = hostTo;
        this.data = data;
    }
    
    public ClientReferral(@Nonnull final ClientReferral other) {
        this.hostTo = other.hostTo;
        this.data = other.data;
    }
    
    @Nonnull
    public static ClientReferral deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ClientReferral obj = new ClientReferral();
        final byte nullBits = buf.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 9 + buf.getIntLE(offset + 1);
            obj.hostTo = HostAddress.deserialize(buf, varPos0);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 9 + buf.getIntLE(offset + 5);
            final int dataCount = VarInt.peek(buf, varPos2);
            if (dataCount < 0) {
                throw ProtocolException.negativeLength("Data", dataCount);
            }
            if (dataCount > 4096) {
                throw ProtocolException.arrayTooLong("Data", dataCount, 4096);
            }
            final int varIntLen = VarInt.length(buf, varPos2);
            if (varPos2 + varIntLen + dataCount * 1L > buf.readableBytes()) {
                throw ProtocolException.bufferTooSmall("Data", varPos2 + varIntLen + dataCount * 1, buf.readableBytes());
            }
            obj.data = new byte[dataCount];
            for (int i = 0; i < dataCount; ++i) {
                obj.data[i] = buf.getByte(varPos2 + varIntLen + i * 1);
            }
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 9;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 1);
            int pos0 = offset + 9 + fieldOffset0;
            pos0 += HostAddress.computeBytesConsumed(buf, pos0);
            if (pos0 - offset > maxEnd) {
                maxEnd = pos0 - offset;
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int fieldOffset2 = buf.getIntLE(offset + 5);
            int pos2 = offset + 9 + fieldOffset2;
            final int arrLen = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + arrLen * 1;
            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.hostTo != null) {
            nullBits |= 0x1;
        }
        if (this.data != null) {
            nullBits |= 0x2;
        }
        buf.writeByte(nullBits);
        final int hostToOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int dataOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.hostTo != null) {
            buf.setIntLE(hostToOffsetSlot, buf.writerIndex() - varBlockStart);
            this.hostTo.serialize(buf);
        }
        else {
            buf.setIntLE(hostToOffsetSlot, -1);
        }
        if (this.data != null) {
            buf.setIntLE(dataOffsetSlot, buf.writerIndex() - varBlockStart);
            if (this.data.length > 4096) {
                throw ProtocolException.arrayTooLong("Data", this.data.length, 4096);
            }
            VarInt.write(buf, this.data.length);
            for (final byte item : this.data) {
                buf.writeByte(item);
            }
        }
        else {
            buf.setIntLE(dataOffsetSlot, -1);
        }
    }
    
    @Override
    public int computeSize() {
        int size = 9;
        if (this.hostTo != null) {
            size += this.hostTo.computeSize();
        }
        if (this.data != null) {
            size += VarInt.size(this.data.length) + this.data.length * 1;
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 9) {
            return ValidationResult.error("Buffer too small: expected at least 9 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int hostToOffset = buffer.getIntLE(offset + 1);
            if (hostToOffset < 0) {
                return ValidationResult.error("Invalid offset for HostTo");
            }
            int pos = offset + 9 + hostToOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for HostTo");
            }
            final ValidationResult hostToResult = HostAddress.validateStructure(buffer, pos);
            if (!hostToResult.isValid()) {
                return ValidationResult.error("Invalid HostTo: " + hostToResult.error());
            }
            pos += HostAddress.computeBytesConsumed(buffer, pos);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int dataOffset = buffer.getIntLE(offset + 5);
            if (dataOffset < 0) {
                return ValidationResult.error("Invalid offset for Data");
            }
            int pos = offset + 9 + dataOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Data");
            }
            final int dataCount = VarInt.peek(buffer, pos);
            if (dataCount < 0) {
                return ValidationResult.error("Invalid array count for Data");
            }
            if (dataCount > 4096) {
                return ValidationResult.error("Data exceeds max length 4096");
            }
            pos += VarInt.length(buffer, pos);
            pos += dataCount * 1;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Data");
            }
        }
        return ValidationResult.OK;
    }
    
    public ClientReferral clone() {
        final ClientReferral copy = new ClientReferral();
        copy.hostTo = ((this.hostTo != null) ? this.hostTo.clone() : null);
        copy.data = (byte[])((this.data != null) ? Arrays.copyOf(this.data, this.data.length) : null);
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ClientReferral other) {
            return Objects.equals(this.hostTo, other.hostTo) && Arrays.equals(this.data, other.data);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + Objects.hashCode(this.hostTo);
        result = 31 * result + Arrays.hashCode(this.data);
        return result;
    }
}
