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

import be.iminds.ilabt.jfed.CorePreferencesModule;
import be.iminds.ilabt.jfed.MockAutomaticWrappersTestCoreModule;
import be.iminds.ilabt.jfed.TestAuthoritiesModule;
import be.iminds.ilabt.jfed.TestUserModule;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.Server;
import be.iminds.ilabt.jfed.highlevel.HighLevelModule;
import be.iminds.ilabt.jfed.highlevel.SfaOnlyExperimentModule;
import be.iminds.ilabt.jfed.highlevel.tasks.HighLevelTaskFactory;
import be.iminds.ilabt.jfed.lowlevel.JFedException;
import be.iminds.ilabt.jfed.lowlevel.TestCertHelper;
import be.iminds.ilabt.jfed.lowlevel.TestbedInfoSource;
import be.iminds.ilabt.jfed.lowlevel.api_wrapper.impl.AutomaticAggregateManagerWrapper;
import javafx.embed.swing.JFXPanel;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.inject.Inject;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * HighLevelControllerTest
 */
@Test
@org.testng.annotations.Guice(modules = {MockAutomaticWrappersTestCoreModule.class,
                                         TestUserModule.class,
                                         CorePreferencesModule.class,
                                         TestAuthoritiesModule.class,
                                         HighLevelModule.class,
                                         SfaOnlyExperimentModule.class})
public class HighLevelControllerMockAutomaticWrappersTest {
    private Server testAuth;

    @Inject
    private TestbedInfoSource testbedInfoSource;

    @Inject
    private AutomaticAggregateManagerWrapper.AutomaticAggregateManagerWrapperFactory
            automaticAggregateManagerWrapperFactory;


    //    private AuthorityList authorityList;
//    private JFedConnectionProvider connectionProvider;
//    private GeniUserProvider geniUserProvider;
//    private SfaModel sfaModel;
//    private Logger logger;
    @Inject
    private TaskThread tt;
    @Inject
    private HighLevelTaskFactory hltf;

    private AutomaticAggregateManagerWrapper automaticAggregateManagerWrapperMock;



    {
        //initialize JavaFX
        new JFXPanel();
    }

    @BeforeMethod
    public void setUp() throws Exception {
        testAuth = testbedInfoSource.getByUrnExact(TestCertHelper.am1Urn);
        assert testAuth != null;
        automaticAggregateManagerWrapperMock = automaticAggregateManagerWrapperFactory.create(testAuth);
        //this is a mocked object
        when(automaticAggregateManagerWrapperMock.getVersion()).thenReturn(Collections.emptyMap());
    }

    @Test
    public void basicHighLevelControllerTest() throws InterruptedException, JFedException {
        CountDownLatch countdownLatch = new CountDownLatch(2);

        Task t = hltf.getVersion(testAuth);
        TaskExecutionFinishedCallback callback = (task, taskExecution, state) -> {
            System.out.println("GetVersion completed");
            countdownLatch.countDown();
        };
        tt.addTask(t, callback);

        countdownLatch.countDown();
        countdownLatch.await(1, TimeUnit.SECONDS);

        verify(automaticAggregateManagerWrapperMock).getVersion();
    }
}
