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

package com.hypixel.hytale.protocol;

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 ParamValue
{
    public static final int MAX_SIZE = 16384011;
    
    @Nonnull
    public static ParamValue 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 -> StringParamValue.deserialize(buf, offset + typeIdLen);
            case 1 -> BoolParamValue.deserialize(buf, offset + typeIdLen);
            case 2 -> DoubleParamValue.deserialize(buf, offset + typeIdLen);
            case 3 -> IntParamValue.deserialize(buf, offset + typeIdLen);
            case 4 -> LongParamValue.deserialize(buf, offset + typeIdLen);
            default -> throw ProtocolException.unknownPolymorphicType("ParamValue", 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 -> StringParamValue.computeBytesConsumed(buf, offset + typeIdLen);
            case 1 -> BoolParamValue.computeBytesConsumed(buf, offset + typeIdLen);
            case 2 -> DoubleParamValue.computeBytesConsumed(buf, offset + typeIdLen);
            case 3 -> IntParamValue.computeBytesConsumed(buf, offset + typeIdLen);
            case 4 -> LongParamValue.computeBytesConsumed(buf, offset + typeIdLen);
            default -> throw ProtocolException.unknownPolymorphicType("ParamValue", typeId);
        };
    }
    
    public int getTypeId() {
        if (this instanceof final StringParamValue sub) {
            return 0;
        }
        if (this instanceof final BoolParamValue sub2) {
            return 1;
        }
        if (this instanceof final DoubleParamValue sub3) {
            return 2;
        }
        if (this instanceof final IntParamValue sub4) {
            return 3;
        }
        if (this instanceof final LongParamValue sub5) {
            return 4;
        }
        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 -> StringParamValue.validateStructure(buffer, offset + typeIdLen);
            case 1 -> BoolParamValue.validateStructure(buffer, offset + typeIdLen);
            case 2 -> DoubleParamValue.validateStructure(buffer, offset + typeIdLen);
            case 3 -> IntParamValue.validateStructure(buffer, offset + typeIdLen);
            case 4 -> LongParamValue.validateStructure(buffer, offset + typeIdLen);
            default -> ValidationResult.error("Unknown polymorphic type ID " + typeId + " for ParamValue");
        };
    }
}
