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

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

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 UpdateCategoryAction extends WindowAction
{
    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 category;
    @Nonnull
    public String itemCategory;
    
    public UpdateCategoryAction() {
        this.category = "";
        this.itemCategory = "";
    }
    
    public UpdateCategoryAction(@Nonnull final String category, @Nonnull final String itemCategory) {
        this.category = "";
        this.itemCategory = "";
        this.category = category;
        this.itemCategory = itemCategory;
    }
    
    public UpdateCategoryAction(@Nonnull final UpdateCategoryAction other) {
        this.category = "";
        this.itemCategory = "";
        this.category = other.category;
        this.itemCategory = other.itemCategory;
    }
    
    @Nonnull
    public static UpdateCategoryAction deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final UpdateCategoryAction obj = new UpdateCategoryAction();
        final int varPos0 = offset + 8 + buf.getIntLE(offset + 0);
        final int categoryLen = VarInt.peek(buf, varPos0);
        if (categoryLen < 0) {
            throw ProtocolException.negativeLength("Category", categoryLen);
        }
        if (categoryLen > 4096000) {
            throw ProtocolException.stringTooLong("Category", categoryLen, 4096000);
        }
        obj.category = PacketIO.readVarString(buf, varPos0, PacketIO.UTF8);
        final int varPos2 = offset + 8 + buf.getIntLE(offset + 4);
        final int itemCategoryLen = VarInt.peek(buf, varPos2);
        if (itemCategoryLen < 0) {
            throw ProtocolException.negativeLength("ItemCategory", itemCategoryLen);
        }
        if (itemCategoryLen > 4096000) {
            throw ProtocolException.stringTooLong("ItemCategory", itemCategoryLen, 4096000);
        }
        obj.itemCategory = 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;
    }
    
    @Override
    public int serialize(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        final int categoryOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int itemCategoryOffsetSlot = buf.writerIndex();
        buf.writeIntLE(0);
        final int varBlockStart = buf.writerIndex();
        buf.setIntLE(categoryOffsetSlot, buf.writerIndex() - varBlockStart);
        PacketIO.writeVarString(buf, this.category, 4096000);
        buf.setIntLE(itemCategoryOffsetSlot, buf.writerIndex() - varBlockStart);
        PacketIO.writeVarString(buf, this.itemCategory, 4096000);
        return buf.writerIndex() - startPos;
    }
    
    @Override
    public int computeSize() {
        int size = 8;
        size += PacketIO.stringSize(this.category);
        size += PacketIO.stringSize(this.itemCategory);
        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 categoryOffset = buffer.getIntLE(offset + 0);
        if (categoryOffset < 0) {
            return ValidationResult.error("Invalid offset for Category");
        }
        int pos = offset + 8 + categoryOffset;
        if (pos >= buffer.writerIndex()) {
            return ValidationResult.error("Offset out of bounds for Category");
        }
        final int categoryLen = VarInt.peek(buffer, pos);
        if (categoryLen < 0) {
            return ValidationResult.error("Invalid string length for Category");
        }
        if (categoryLen > 4096000) {
            return ValidationResult.error("Category exceeds max length 4096000");
        }
        pos += VarInt.length(buffer, pos);
        pos += categoryLen;
        if (pos > buffer.writerIndex()) {
            return ValidationResult.error("Buffer overflow reading Category");
        }
        final int itemCategoryOffset = buffer.getIntLE(offset + 4);
        if (itemCategoryOffset < 0) {
            return ValidationResult.error("Invalid offset for ItemCategory");
        }
        pos = offset + 8 + itemCategoryOffset;
        if (pos >= buffer.writerIndex()) {
            return ValidationResult.error("Offset out of bounds for ItemCategory");
        }
        final int itemCategoryLen = VarInt.peek(buffer, pos);
        if (itemCategoryLen < 0) {
            return ValidationResult.error("Invalid string length for ItemCategory");
        }
        if (itemCategoryLen > 4096000) {
            return ValidationResult.error("ItemCategory exceeds max length 4096000");
        }
        pos += VarInt.length(buffer, pos);
        pos += itemCategoryLen;
        if (pos > buffer.writerIndex()) {
            return ValidationResult.error("Buffer overflow reading ItemCategory");
        }
        return ValidationResult.OK;
    }
    
    public UpdateCategoryAction clone() {
        final UpdateCategoryAction copy = new UpdateCategoryAction();
        copy.category = this.category;
        copy.itemCategory = this.itemCategory;
        return copy;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final UpdateCategoryAction other) {
            return Objects.equals(this.category, other.category) && Objects.equals(this.itemCategory, other.itemCategory);
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(this.category, this.itemCategory);
    }
}
