package be.iminds.ilabt.jfed.experimenter_gui.bugreporting;

import be.iminds.ilabt.jfed.CorePreferencesModule;
import be.iminds.ilabt.jfed.bugreport.BugReport;
import be.iminds.ilabt.jfed.experimenter_gui.JFedVersionInfo;
import be.iminds.ilabt.jfed.experimenter_gui.connectivity_tester.ConnectivityTester;
import be.iminds.ilabt.jfed.experimenter_gui.util.BugReportExperimentControllerListener;
import be.iminds.ilabt.jfed.highlevel.HighLevelModule;
import be.iminds.ilabt.jfed.highlevel.LowLevelModule;
import be.iminds.ilabt.jfed.highlevel.MockGeniUserModule;
import be.iminds.ilabt.jfed.highlevel.SfaOnlyExperimentModule;
import be.iminds.ilabt.jfed.highlevel.controller.JavaFXTaskThread;
import be.iminds.ilabt.jfed.highlevel.model.SfaModel;
import be.iminds.ilabt.jfed.log.cache.AllApiCallsList;
import be.iminds.ilabt.jfed.log.cache.ApiCallDetailsCache;
import be.iminds.ilabt.jfed.logging.JFedLoggerManager;
import be.iminds.ilabt.jfed.lowlevel.GeniUser;
import be.iminds.ilabt.jfed.lowlevel.GeniUserProvider;
import be.iminds.ilabt.jfed.preferences.JFedGuiPreferences;
import be.iminds.ilabt.jfed.util.GeniUrn;
import javafx.collections.FXCollections;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import javax.inject.Inject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.StringReader;
import java.util.Collections;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


@Test

@Guice(modules = {HighLevelModule.class, LowLevelModule.class, MockGeniUserModule.class, CorePreferencesModule.class, SfaOnlyExperimentModule.class})
public class BugReportComposerTest {

    @Inject
    private BugReportReaderFactory bugReportReaderFactory;
    @Inject
    private ApiCallDetailsCache cache;
    @Inject
    private JFedVersionInfo jFedVersionInfo;

    //TODO: make a reliable test for StitchingJobReport
    @Test(enabled = false)
    public void testStitchingReports() throws Exception {
        JavaFXTaskThread javaFXTaskThread = mock(JavaFXTaskThread.class);
        GeniUserProvider userLoginModelManager = mock(GeniUserProvider.class);
        SfaModel sfaModel = mock(SfaModel.class);
        ConnectivityTester connectivityTester = mock(ConnectivityTester.class);
        JFedGuiPreferences jFedPreferences = mock(JFedGuiPreferences.class);
        JFedLoggerManager jFedLoggerManager = mock(JFedLoggerManager.class);
        BugReportExperimentControllerListener bugReportExperimentControllerListener =
                mock(BugReportExperimentControllerListener.class);
        AllApiCallsList allApiCallsList = mock(AllApiCallsList.class);

        GeniUser geniUser = mock(GeniUser.class);
        when(geniUser.getUserUrnString()).thenReturn("testuserurn");

        when(userLoginModelManager.getLoggedInGeniUser()).thenReturn(geniUser);

        when(connectivityTester.getTestsFinished()).thenReturn(true);

        when(jFedLoggerManager.getBugReportMemoryAppender()).thenReturn(null);
        when(javaFXTaskThread.getCallsCount()).thenReturn(0);
        when(connectivityTester.getTestTasks()).thenReturn(FXCollections.emptyObservableList());
        when(jFedPreferences.getPropertiesFile()).thenReturn(File.createTempFile("empty", ".properties"));
        when(javaFXTaskThread.getAllTasks()).thenReturn(FXCollections.emptyObservableList());
        when(sfaModel.getSlices()).thenReturn(FXCollections.emptyObservableList());

        when(bugReportExperimentControllerListener.getStitchingJobReports()).thenReturn(Collections.EMPTY_LIST);

        when(allApiCallsList.getCalls()).thenReturn(Collections.emptyList());

        BugReportComposer bugReportComposer = new BugReportComposer(
                javaFXTaskThread,
                userLoginModelManager,
                sfaModel,
                connectivityTester,
                jFedPreferences,
                jFedLoggerManager,
                bugReportExperimentControllerListener,
                jFedVersionInfo,
                allApiCallsList);
        bugReportComposer.setDescription("Test Description");
        bugReportComposer.setEmail("Test Email");

        bugReportComposer.setSubject("subject1");
        bugReportComposer.setReportTarget(BugReport.ReportTarget.OTHER);
        bugReportComposer.setReportType(BugReport.ReportType.OTHER);
        bugReportComposer.setRelatedTestbeds(Collections.singletonList(GeniUrn.createGeniUrnFromEncodedParts("example.com", "auth", "am")));
        bugReportComposer.setPostOnPublicList(false);

        BugReport actualBugReport = bugReportComposer.createBugReport();
        System.out.println("Created BugReport xml: " + actualBugReport);

        String json = actualBugReport.toString();

        //just a quick check to see if XML looks OK
        assertThat(json, containsString("stitchingreports"));
        assertThat(json, containsString("Still OK 3"));

        //TODO
//        //now reconstruct
//        BugReportReader reader = bugReportReaderFactory.createBugReportReader(new StringReader(xml));
//
//        BugReport bugReport = reader.call();
//
//        assertThat(bugReport, is(notNullValue()));
    }
}
