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

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 Action implements Function<NetworkScope, NetworkScope> {

    private PipelineFunction function;

    public Action(Class<? extends PipelineFunction> 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 PipelineFunction> functionClass) {
        this.functionClass = functionClass;
        instantiateFunction();
    }

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

    @Override
    public NetworkScope apply(NetworkScope source) {
        return function.apply(source, properties);
    }
}
