package ru.kirillius.pf.sdn.api.flow;

import lombok.Getter;
import lombok.Setter;
import lombok.SneakyThrows;
import ru.kirillius.pf.sdn.api.Networking.NetworkScope;
import ru.kirillius.pf.sdn.entity.ActionConfig;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * Представляет собой вызов функции с параметрами
 */
public class FlowAction implements Function<List<NetworkScope>, CallContext> {

    private FlowFunction function;
    @Getter
    private final ActionConfig config;

    public FlowAction(Class<? extends FlowFunction> functionClass, ActionConfig config) {
        this.functionClass = functionClass;
        this.properties = config.getProperties();
        this.config = config;
        instantiateFunction();
    }

    @SneakyThrows
    private void instantiateFunction() {
        function = functionClass.getConstructor().newInstance();
    }

    private final Class<? extends FlowFunction> functionClass;
    @Getter
    @Setter
    private Map<String, String> properties;

    @Override
    public CallContext apply(List<NetworkScope> inputs) {
        var context = new CallContext(inputs, properties);
        function.apply(context);
        return context;
    }

}
