package fr.titionfire.ffsaf.net2.packet;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import fr.titionfire.ffsaf.net2.Client_Thread;
import fr.titionfire.ffsaf.net2.Message;
import io.quarkus.hibernate.reactive.panache.common.WithSession;
import io.quarkus.vertx.SafeVertxContext;
import jakarta.transaction.Transactional;

import java.util.function.BiConsumer;

import static fr.titionfire.ffsaf.net2.Client_Thread.MAPPER;

public class CIA<T> implements IAction{
    private final Class<T> clazz;
    private final BiConsumer<Client_Thread, Message<T>> iAction;

    public CIA(Class<T> clazz, BiConsumer<Client_Thread, Message<T>> iAction){
        this.clazz = clazz;
        this.iAction = iAction;
    }

    @Override
    public void dataIn(Client_Thread client_Thread, Message<JsonNode> message) throws JsonProcessingException {
        iAction.accept(client_Thread, new Message<>(message.uuid(), message.code(), message.type(), MAPPER.treeToValue(message.data(), clazz)));
    }
}
