package be.iminds.ilabt.jfed.highlevel.tasks;

import be.iminds.ilabt.jfed.highlevel.controller.Task;
import be.iminds.ilabt.jfed.highlevel.controller.TaskExecution;

import be.iminds.ilabt.jfed.lowlevel.*;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;
import be.iminds.ilabt.jfed.lowlevel.connection.HttpConnection;
import be.iminds.ilabt.jfed.lowlevel.connection.JFedConnectionProvider;
import be.iminds.ilabt.jfed.lowlevel.fedmon.Fedmon;
import be.iminds.ilabt.jfed.lowlevel.fedmon.TestResult;
import be.iminds.ilabt.jfed.preferences.JFedPreferences;
import be.iminds.ilabt.jfed.util.JavaFXLogger;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * User: twalcari
 * Date: 6/17/2015
 * Time: 11:30
 */
public class FedmonResultsTask extends Task {

    private static final String FEDMON_URN = "urn:publicid:IDN+flsmonitor.fed4fire.eu+authority+cm";
    //input parameters
    public final String testbedFilter;
    public final String testbedUrnFilter;
    public final String testDefinitionFilter;

    //results
    public List<TestResult> results = null;

    //environment parameters
    private final JavaFXLogger logger;
    private final GeniUserProvider geniUserProvider;
    private final JFedPreferences jFedPreferences;
    private final TestbedInfoSource testbedInfoSource;
    private final JFedConnectionProvider connectionProvider;


    public FedmonResultsTask(@Nullable String testbedFilter, @Nullable String testbedUrnFilter, @Nullable String testDefinitionFilter, JavaFXLogger logger,
                             GeniUserProvider geniUserProvider, JFedPreferences jFedPreferences,
                             TestbedInfoSource testbedInfoSource, JFedConnectionProvider connectionProvider) {
        super("Get Femon Results");
        this.testbedFilter = testbedFilter;
        this.testbedUrnFilter = testbedUrnFilter;
        this.testDefinitionFilter = testDefinitionFilter;
        this.logger = logger;
        this.geniUserProvider = geniUserProvider;
        this.jFedPreferences = jFedPreferences;
        this.testbedInfoSource = testbedInfoSource;
        this.connectionProvider = connectionProvider;
    }

    @Override
    protected void doTask(TaskExecution taskExecution) throws JFedException, InterruptedException {
        Fedmon fedmon = new Fedmon(logger, jFedPreferences);
        Server fedmonAuth = testbedInfoSource.getByUrnExact(FEDMON_URN);

        GeniUser user = geniUserProvider.getLoggedInGeniUser();
        HttpConnection con =
                (HttpConnection) connectionProvider.getConnectionByAuthority(user, fedmonAuth, new ApiInfo.Api(ApiInfo.ApiName.FED4FIRE_FEDMON, 1));
        Fedmon.FedmonReply<List<TestResult>> reply = fedmon.getTestResults(con, testbedFilter, testDefinitionFilter, testbedUrnFilter);
        if (reply.getGeniResponseCode().isSuccess())
            results = reply.getValue();
        else
            results = new ArrayList<>();
    }

    public List<TestResult> getResults() {
        return results;
    }

    public List<TestResult> getSuccessResults() {
        if (results != null) {
            return results.stream().filter(TestResult::isSuccessSummary).collect(Collectors.toList());
        } else
            return null;
    }
}
