package io.syndesis.qe.bdd.validation;

import org.assertj.core.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import cucumber.api.java.en.Then;
import io.fabric8.kubernetes.api.model.Pod;
import io.syndesis.common.model.integration.Integration;
import io.syndesis.common.model.metrics.IntegrationMetricsSummary;
import io.syndesis.qe.endpoints.ActivityIntegrationsEndpoint;
import io.syndesis.qe.endpoints.IntegrationsEndpoint;
import io.syndesis.qe.endpoints.IntegrationsMetricsEndpoint;
import io.syndesis.qe.utils.OpenShiftUtils;
import io.syndesis.server.endpoint.v1.handler.activity.Activity;
import lombok.extern.slf4j.Slf4j;

/**
 * For monitoring of metrics and logs related to integrations.
 */

@Slf4j
public class MonitoringValidationSteps {

    @Autowired
    private IntegrationsEndpoint integrationsEndpoint;
    @Autowired
    private IntegrationsMetricsEndpoint integrationsMetricsEndpoint;
    @Autowired
    private ActivityIntegrationsEndpoint activityIntegrationsEndpoint;

    public MonitoringValidationSteps() {
    }

    @Then("^validate that number of all messages through integration \"([^\"]*)\" is greater than \"([^\"]*)\", period in ms: \"([^\"]*)\"$")
    public void validateThatNumberOfAllMessagesOfIntegrationIsGreaterThanPeriodInMs(String integrationName, Integer nr, Integer ms) throws InterruptedException {
        Thread.sleep(ms + 1000);
//        0.  get integration id.
        String integrationId = this.getIdByIntegrationName(integrationName);
        Assertions.assertThat(integrationId).isNotNull();

//        1. get metrics:
        IntegrationMetricsSummary summary = integrationsMetricsEndpoint.get(integrationId);
//        2. get metrics info:
        Long nrOfMessages = summary.getMessages();
        log.info("MESSAGES SUMMARY: *{}*", nrOfMessages);
        Assertions.assertThat(nrOfMessages).isGreaterThan(nr);

        Date lastProceded = summary.getLastProcessed().get();
        log.info("LAST MESSAGE WAS PROCEEDED ON: *{}*", lastProceded);
    }

    @Then("^validate that log of integration \"([^\"]*)\" has been created, period in ms: \"([^\"]*)\"$")
    public void validateThatLogOfIntegrationHasBeenCreatedPeriodInMs(String integrationName, Integer ms) throws InterruptedException {
        Thread.sleep(ms + 1000);
        String integrationId = this.getIdByIntegrationName(integrationName);
        Assertions.assertThat(integrationId).isNotNull();

        //1.
        List<Activity> activityIntegrationLogs = activityIntegrationsEndpoint.list(integrationId);
        activityIntegrationLogs.stream().forEach(activity -> log.info("LOGS ACTIVITY: *{}*", activity.getPod()));
        //I suppose there is the same pod for every activity
        String podName = activityIntegrationLogs.get(0).getPod();

        Optional<Pod> buildPod = OpenShiftUtils.getInstance().getPods().stream().filter(p -> p.getMetadata().getName().equals(podName)).findFirst();
        if (buildPod.isPresent()) {
            String logText = OpenShiftUtils.getInstance().getPodLog(buildPod.get());
            Assertions.assertThat(logText).isNotEmpty();
        } else {
            Assertions.fail("No pod found for pod name: " + podName);
        }
        //TODO other functionality for logs to be tested.
    }

    //    AUXILIARIES
    private String getIdByIntegrationName(String integrationName) {
        List<Integration> integrations = integrationsEndpoint.list();
        Integration integr = integrations.stream().filter(integration -> integrationName.equals(integration.getName())).findAny().orElse(null);
        return integr.getId().get();
    }

}

@@ -33,16 +33,61 @@
 ation;%0A%0A
+import io.syndesis.qe.utils.LogCheckerUtils;%0A
 import o
@@ -254,16 +254,48 @@
 ptional;
+%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -2777,20 +2777,21 @@
  -%3E log.
-info
+debug
 (%22LOGS A
@@ -2977,21 +2977,27 @@
 al%3CPod%3E 
-build
+integration
 Pod = Op
@@ -3122,21 +3122,27 @@
     if (
-build
+integration
 Pod.isPr
@@ -3224,13 +3224,19 @@
 Log(
-build
+integration
 Pod.
@@ -3289,22 +3289,270 @@
 ext)
-.isNotEmpty();
+%0A                    .isNotEmpty()%0A                    .containsPattern(Pattern.compile(%22%5C%5C%7B%5C%22exchange%5C%22:%5C%22i-.*$%5C%22,%5C%22status%5C%22:%5C%22begin%5C%22%7D%22))%0A                    .containsPattern(Pattern.compile(%22%5C%5C%7B%5C%22exchange%5C%22:%5C%22i-.*$%5C%22,%5C%22status%5C%22:%5C%22done%5C%22,%5C%22failed%5C%22:false%7D%22));%0A
 %0A   

Extend verification of activity log