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

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

import com.hypixel.hytale.protocol.io.ValidationResult;
import com.hypixel.hytale.protocol.io.ProtocolException;
import com.hypixel.hytale.protocol.io.VarInt;
import javax.annotation.Nonnull;
import io.netty.buffer.ByteBuf;

public abstract class WindowAction
{
    public static final int MAX_SIZE = 32768023;
    
    @Nonnull
    public static WindowAction deserialize(@Nonnull final ByteBuf buf, final int offset) {
        final int typeId = VarInt.peek(buf, offset);
        final int typeIdLen = VarInt.length(buf, offset);
        return switch (typeId) {
            case 0 -> CraftRecipeAction.deserialize(buf, offset + typeIdLen);
            case 1 -> TierUpgradeAction.deserialize(buf, offset + typeIdLen);
            case 2 -> SelectSlotAction.deserialize(buf, offset + typeIdLen);
            case 3 -> ChangeBlockAction.deserialize(buf, offset + typeIdLen);
            case 4 -> SetActiveAction.deserialize(buf, offset + typeIdLen);
            case 5 -> CraftItemAction.deserialize(buf, offset + typeIdLen);
            case 6 -> UpdateCategoryAction.deserialize(buf, offset + typeIdLen);
            case 7 -> CancelCraftingAction.deserialize(buf, offset + typeIdLen);
            case 8 -> SortItemsAction.deserialize(buf, offset + typeIdLen);
            default -> throw ProtocolException.unknownPolymorphicType("WindowAction", typeId);
        };
    }
    
    public static int computeBytesConsumed(@Nonnull final ByteBuf buf, final int offset) {
        final int typeId = VarInt.peek(buf, offset);
        final int length;
        final int typeIdLen = length = VarInt.length(buf, offset);
        return length + switch (typeId) {
            case 0 -> CraftRecipeAction.computeBytesConsumed(buf, offset + typeIdLen);
            case 1 -> TierUpgradeAction.computeBytesConsumed(buf, offset + typeIdLen);
            case 2 -> SelectSlotAction.computeBytesConsumed(buf, offset + typeIdLen);
            case 3 -> ChangeBlockAction.computeBytesConsumed(buf, offset + typeIdLen);
            case 4 -> SetActiveAction.computeBytesConsumed(buf, offset + typeIdLen);
            case 5 -> CraftItemAction.computeBytesConsumed(buf, offset + typeIdLen);
            case 6 -> UpdateCategoryAction.computeBytesConsumed(buf, offset + typeIdLen);
            case 7 -> CancelCraftingAction.computeBytesConsumed(buf, offset + typeIdLen);
            case 8 -> SortItemsAction.computeBytesConsumed(buf, offset + typeIdLen);
            default -> throw ProtocolException.unknownPolymorphicType("WindowAction", typeId);
        };
    }
    
    public int getTypeId() {
        if (this instanceof final CraftRecipeAction sub) {
            return 0;
        }
        if (this instanceof final TierUpgradeAction sub2) {
            return 1;
        }
        if (this instanceof final SelectSlotAction sub3) {
            return 2;
        }
        if (this instanceof final ChangeBlockAction sub4) {
            return 3;
        }
        if (this instanceof final SetActiveAction sub5) {
            return 4;
        }
        if (this instanceof final CraftItemAction sub6) {
            return 5;
        }
        if (this instanceof final UpdateCategoryAction sub7) {
            return 6;
        }
        if (this instanceof final CancelCraftingAction sub8) {
            return 7;
        }
        if (this instanceof final SortItemsAction sub9) {
            return 8;
        }
        throw new IllegalStateException("Unknown subtype: " + this.getClass().getName());
    }
    
    public abstract int serialize(@Nonnull final ByteBuf p0);
    
    public abstract int computeSize();
    
    public int serializeWithTypeId(@Nonnull final ByteBuf buf) {
        final int startPos = buf.writerIndex();
        VarInt.write(buf, this.getTypeId());
        this.serialize(buf);
        return buf.writerIndex() - startPos;
    }
    
    public int computeSizeWithTypeId() {
        return VarInt.size(this.getTypeId()) + this.computeSize();
    }
    
    public static ValidationResult validateStructure(@Nonnull final ByteBuf buffer, final int offset) {
        final int typeId = VarInt.peek(buffer, offset);
        final int typeIdLen = VarInt.length(buffer, offset);
        return switch (typeId) {
            case 0 -> CraftRecipeAction.validateStructure(buffer, offset + typeIdLen);
            case 1 -> TierUpgradeAction.validateStructure(buffer, offset + typeIdLen);
            case 2 -> SelectSlotAction.validateStructure(buffer, offset + typeIdLen);
            case 3 -> ChangeBlockAction.validateStructure(buffer, offset + typeIdLen);
            case 4 -> SetActiveAction.validateStructure(buffer, offset + typeIdLen);
            case 5 -> CraftItemAction.validateStructure(buffer, offset + typeIdLen);
            case 6 -> UpdateCategoryAction.validateStructure(buffer, offset + typeIdLen);
            case 7 -> CancelCraftingAction.validateStructure(buffer, offset + typeIdLen);
            case 8 -> SortItemsAction.validateStructure(buffer, offset + typeIdLen);
            default -> ValidationResult.error("Unknown polymorphic type ID " + typeId + " for WindowAction");
        };
    }
}
