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

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

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

@Builder
public class FlowAction implements Function<NetworkScope, CallContext> {

    private FlowFunction function;

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

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

    public void setFunctionClass(Class<? extends FlowFunction> functionClass) {
        this.functionClass = functionClass;
        instantiateFunction();
    }

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

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

}
