// 
// 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.Nullable;
import javax.annotation.Nonnull;

public class CustomUICommand
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 1;
    public static final int FIXED_BLOCK_SIZE = 2;
    public static final int VARIABLE_FIELD_COUNT = 3;
    public static final int VARIABLE_BLOCK_START = 14;
    public static final int MAX_SIZE = 49152029;
    @Nonnull
    public CustomUICommandType type;
    @Nullable
    public String selector;
    @Nullable
    public String data;
    @Nullable
    public String text;
    
    public CustomUICommand() {
        this.type = CustomUICommandType.Append;
    }
    
    public CustomUICommand(@Nonnull final CustomUICommandType type, @Nullable final String selector, @Nullable final String data, @Nullable final String text) {
        this.type = CustomUICommandType.Append;
        this.type = type;
        this.selector = selector;
        this.data = data;
        this.text = text;
    }
    
    public CustomUICommand(@Nonnull final CustomUICommand other) {
        this.type = CustomUICommandType.Append;
        this.type = other.type;
        this.selector = other.selector;
        this.data = other.data;
        this.text = other.text;
    }
    
    @Nonnull
    public static CustomUICommand deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final CustomUICommand obj = new CustomUICommand();
        final byte nullBits = buf.getByte(offset);
        obj.type = CustomUICommandType.fromValue(buf.getByte(offset + 1));
        if ((nullBits & 0x1) != 0x0) {
            final int varPos0 = offset + 14 + buf.getIntLE(offset + 2);
            final int selectorLen = VarInt.peek(buf, varPos0);
            if (selectorLen < 0) {
                throw ProtocolException.negativeLength("Selector", selectorLen);
            }
            if (selectorLen > 4096000) {
                throw ProtocolException.stringTooLong("Selector", selectorLen, 4096000);
            }
            obj.selector = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        }
        if ((nullBits & 0x2) != 0x0) {
            final int varPos2 = offset + 14 + buf.getIntLE(offset + 6);
            final int dataLen = VarInt.peek(buf, varPos2);
            if (dataLen < 0) {
                throw ProtocolException.negativeLength("Data", dataLen);
            }
            if (dataLen > 4096000) {
                throw ProtocolException.stringTooLong("Data", dataLen, 4096000);
            }
            obj.data = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        }
        if ((nullBits & 0x4) != 0x0) {
            final int varPos3 = offset + 14 + buf.getIntLE(offset + 10);
            final int textLen = VarInt.peek(buf, varPos3);
            if (textLen < 0) {
                throw ProtocolException.negativeLength("Text", textLen);
            }
            if (textLen > 4096000) {
                throw ProtocolException.stringTooLong("Text", textLen, 4096000);
            }
            obj.text = PacketIO.readVarString(buf, varPos3, PacketIO.UTF8);
        }
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final byte nullBits = buf.getByte(offset);
        int maxEnd = 14;
        if ((nullBits & 0x1) != 0x0) {
            final int fieldOffset0 = buf.getIntLE(offset + 2);
            int pos0 = offset + 14 + 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 + 6);
            int pos2 = offset + 14 + fieldOffset2;
            final int sl = VarInt.peek(buf, pos2);
            pos2 += VarInt.length(buf, pos2) + sl;
            if (pos2 - offset > maxEnd) {
                maxEnd = pos2 - offset;
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int fieldOffset3 = buf.getIntLE(offset + 10);
            int pos3 = offset + 14 + fieldOffset3;
            final int sl = VarInt.peek(buf, pos3);
            pos3 += VarInt.length(buf, pos3) + sl;
            if (pos3 - offset > maxEnd) {
                maxEnd = pos3 - offset;
            }
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        byte nullBits = 0;
        if (this.selector != null) {
            nullBits |= 0x1;
        }
        if (this.data != null) {
            nullBits |= 0x2;
        }
        if (this.text != null) {
            nullBits |= 0x4;
        }
        buf.writeByte(nullBits);
        buf.writeByte(this.type.getValue());
        final int selectorOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int dataOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int textOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        if (this.selector != null) {
            buf.setIntLE(selectorOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.selector, 4096000);
        }
        else {
            buf.setIntLE(selectorOffsetSlot, -1);
        }
        if (this.data != null) {
            buf.setIntLE(dataOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.data, 4096000);
        }
        else {
            buf.setIntLE(dataOffsetSlot, -1);
        }
        if (this.text != null) {
            buf.setIntLE(textOffsetSlot, buf.writerIndex() - varBlockStart);
            PacketIO.writeVarString(buf, this.text, 4096000);
        }
        else {
            buf.setIntLE(textOffsetSlot, -1);
        }
    }
    
    public int computeSize() {
        int size = 14;
        if (this.selector != null) {
            size += PacketIO.stringSize(this.selector);
        }
        if (this.data != null) {
            size += PacketIO.stringSize(this.data);
        }
        if (this.text != null) {
            size += PacketIO.stringSize(this.text);
        }
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 14) {
            return ValidationResult.error("Buffer too small: expected at least 14 bytes");
        }
        final byte nullBits = buffer.getByte(offset);
        if ((nullBits & 0x1) != 0x0) {
            final int selectorOffset = buffer.getIntLE(offset + 2);
            if (selectorOffset < 0) {
                return ValidationResult.error("Invalid offset for Selector");
            }
            int pos = offset + 14 + selectorOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Selector");
            }
            final int selectorLen = VarInt.peek(buffer, pos);
            if (selectorLen < 0) {
                return ValidationResult.error("Invalid string length for Selector");
            }
            if (selectorLen > 4096000) {
                return ValidationResult.error("Selector exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += selectorLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Selector");
            }
        }
        if ((nullBits & 0x2) != 0x0) {
            final int dataOffset = buffer.getIntLE(offset + 6);
            if (dataOffset < 0) {
                return ValidationResult.error("Invalid offset for Data");
            }
            int pos = offset + 14 + dataOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Data");
            }
            final int dataLen = VarInt.peek(buffer, pos);
            if (dataLen < 0) {
                return ValidationResult.error("Invalid string length for Data");
            }
            if (dataLen > 4096000) {
                return ValidationResult.error("Data exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += dataLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Data");
            }
        }
        if ((nullBits & 0x4) != 0x0) {
            final int textOffset = buffer.getIntLE(offset + 10);
            if (textOffset < 0) {
                return ValidationResult.error("Invalid offset for Text");
            }
            int pos = offset + 14 + textOffset;
            if (pos >= buffer.writerIndex()) {
                return ValidationResult.error("Offset out of bounds for Text");
            }
            final int textLen = VarInt.peek(buffer, pos);
            if (textLen < 0) {
                return ValidationResult.error("Invalid string length for Text");
            }
            if (textLen > 4096000) {
                return ValidationResult.error("Text exceeds max length 4096000");
            }
            pos += VarInt.length(buffer, pos);
            pos += textLen;
            if (pos > buffer.writerIndex()) {
                return ValidationResult.error("Buffer overflow reading Text");
            }
        }
        return ValidationResult.OK;
    }
    
    public CustomUICommand clone() {
        final CustomUICommand copy = new CustomUICommand();
        copy.type = this.type;
        copy.selector = this.selector;
        copy.data = this.data;
        copy.text = this.text;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final CustomUICommand other) {
            return Objects.equals(this.type, other.type) && Objects.equals(this.selector, other.selector) && Objects.equals(this.data, other.data) && Objects.equals(this.text, other.text);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.type, this.selector, this.data, this.text);
    }
}
