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

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

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 ContextMenuItem
{
    public static final int NULLABLE_BIT_FIELD_SIZE = 0;
    public static final int FIXED_BLOCK_SIZE = 0;
    public static final int VARIABLE_FIELD_COUNT = 2;
    public static final int VARIABLE_BLOCK_START = 8;
    public static final int MAX_SIZE = 32768018;
    @Nonnull
    public String name;
    @Nonnull
    public String command;
    
    public ContextMenuItem() {
        this.name = "";
        this.command = "";
    }
    
    public ContextMenuItem(@Nonnull final String name, @Nonnull final String command) {
        this.name = "";
        this.command = "";
        this.name = name;
        this.command = command;
    }
    
    public ContextMenuItem(@Nonnull final ContextMenuItem other) {
        this.name = "";
        this.command = "";
        this.name = other.name;
        this.command = other.command;
    }
    
    @Nonnull
    public static ContextMenuItem deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final ContextMenuItem obj = new ContextMenuItem();
        final int varPos0 = offset + 8 + buf.getIntLE(offset + 0);
        final int nameLen = VarInt.peek(buf, varPos0);
        if (nameLen < 0) {
            throw ProtocolException.negativeLength("Name", nameLen);
        }
        if (nameLen > 4096000) {
            throw ProtocolException.stringTooLong("Name", nameLen, 4096000);
        }
        obj.name = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        final int varPos2 = offset + 8 + buf.getIntLE(offset + 4);
        final int commandLen = VarInt.peek(buf, varPos2);
        if (commandLen < 0) {
            throw ProtocolException.negativeLength("Command", commandLen);
        }
        if (commandLen > 4096000) {
            throw ProtocolException.stringTooLong("Command", commandLen, 4096000);
        }
        obj.command = PacketIO.readVarString(buf, varPos2, PacketIO.UTF8);
        return obj;
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        int maxEnd = 8;
        final int fieldOffset0 = buf.getIntLE(offset + 0);
        int pos0 = offset + 8 + fieldOffset0;
        int sl = VarInt.peek(buf, pos0);
        pos0 += VarInt.length(buf, pos0) + sl;
        if (pos0 - offset > maxEnd) {
            maxEnd = pos0 - offset;
        }
        final int fieldOffset2 = buf.getIntLE(offset + 4);
        int pos2 = offset + 8 + fieldOffset2;
        sl = VarInt.peek(buf, pos2);
        pos2 += VarInt.length(buf, pos2) + sl;
        if (pos2 - offset > maxEnd) {
            maxEnd = pos2 - offset;
        }
        return maxEnd;
    }
    
    public void serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        final int nameOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int commandOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        buf.setIntLE(nameOffsetSlot, buf.writerIndex() - varBlockStart);
        PacketIO.writeVarString(buf, this.name, 4096000);
        buf.setIntLE(commandOffsetSlot, buf.writerIndex() - varBlockStart);
        PacketIO.writeVarString(buf, this.command, 4096000);
    }
    
    public int computeSize() {
        int size = 8;
        size += PacketIO.stringSize(this.name);
        size += PacketIO.stringSize(this.command);
        return size;
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        if (buffer.readableBytes() - offset < 8) {
            return ValidationResult.error("Buffer too small: expected at least 8 bytes");
        }
        final int nameOffset = buffer.getIntLE(offset + 0);
        if (nameOffset < 0) {
            return ValidationResult.error("Invalid offset for Name");
        }
        int pos = offset + 8 + nameOffset;
        if (pos >= buffer.writerIndex()) {
            return ValidationResult.error("Offset out of bounds for Name");
        }
        final int nameLen = VarInt.peek(buffer, pos);
        if (nameLen < 0) {
            return ValidationResult.error("Invalid string length for Name");
        }
        if (nameLen > 4096000) {
            return ValidationResult.error("Name exceeds max length 4096000");
        }
        pos += VarInt.length(buffer, pos);
        pos += nameLen;
        if (pos > buffer.writerIndex()) {
            return ValidationResult.error("Buffer overflow reading Name");
        }
        final int commandOffset = buffer.getIntLE(offset + 4);
        if (commandOffset < 0) {
            return ValidationResult.error("Invalid offset for Command");
        }
        pos = offset + 8 + commandOffset;
        if (pos >= buffer.writerIndex()) {
            return ValidationResult.error("Offset out of bounds for Command");
        }
        final int commandLen = VarInt.peek(buffer, pos);
        if (commandLen < 0) {
            return ValidationResult.error("Invalid string length for Command");
        }
        if (commandLen > 4096000) {
            return ValidationResult.error("Command exceeds max length 4096000");
        }
        pos += VarInt.length(buffer, pos);
        pos += commandLen;
        if (pos > buffer.writerIndex()) {
            return ValidationResult.error("Buffer overflow reading Command");
        }
        return ValidationResult.OK;
    }
    
    public ContextMenuItem clone() {
        final ContextMenuItem copy = new ContextMenuItem();
        copy.name = this.name;
        copy.command = this.command;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final ContextMenuItem other) {
            return Objects.equals(this.name, other.name) && Objects.equals(this.command, other.command);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.name, this.command);
    }
}
