package pl.consdata.ico.sqcompanion.hook.action;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import pl.consdata.ico.sqcompanion.history.GroupViolationsHistoryDiff;
import pl.consdata.ico.sqcompanion.history.ViolationsHistoryService;
import pl.consdata.ico.sqcompanion.repository.Group;
import pl.consdata.ico.sqcompanion.repository.RepositoryService;

import java.time.LocalDate;
import java.util.Collections;

@Slf4j
@Service
public class NoImprovementWebhookAction implements WebhookAction<NoImprovementWebhookActionData> {

    public static final String TYPE = "NO_IMPROVEMENT";
    private final RepositoryService repositoryService;
    private final ViolationsHistoryService violationsHistoryService;

    public NoImprovementWebhookAction(RepositoryService repositoryService, ViolationsHistoryService violationsHistoryService) {
        this.repositoryService = repositoryService;
        this.violationsHistoryService = violationsHistoryService;
    }

    @Override
    public ActionResponse call(String groupUUID, NoImprovementWebhookActionData actionData) {
        Group group = repositoryService.getGroup(groupUUID).orElse(null);
        if (group != null) {
            return checkImprovement(group, actionData);
        }
        return null;
    }

    private boolean isGroupClean(Group group, NoImprovementWebhookActionData actionData) {
        GroupViolationsHistoryDiff violationsHistoryDiff = violationsHistoryService.getGroupViolationsHistoryDiff(group, LocalDate.of(2018, 1, 20), LocalDate.of(2018, 1, 23));
        return countDiff(violationsHistoryDiff, actionData) == 0;
    }

    private ActionResponse checkImprovement(Group group, NoImprovementWebhookActionData actionData) {
        if (isGroupClean(group, actionData)) {
            return createResponse("clean", null);
        }

        GroupViolationsHistoryDiff violationsHistoryDiff = violationsHistoryService.getGroupViolationsHistoryDiff(group, LocalDate.of(2018, 1, 20), LocalDate.of(2018, 1, 23));
        if (groupWasImproved(violationsHistoryDiff, actionData)) {
            return createResponse("improvement", countDiff(violationsHistoryDiff, actionData));
        } else {
            return createResponse("no_improvement", countDiff(violationsHistoryDiff, actionData));
        }
    }

    private ActionResponse createResponse(String result, Integer diff) {
        if (diff == null) {
            return new ActionResponse(Collections.emptyMap(), result);
        } else {
            return new ActionResponse(Collections.singletonMap("diff", String.valueOf(diff)), result);
        }
    }

    private int countDiff(GroupViolationsHistoryDiff violationsHistoryDiff, NoImprovementWebhookActionData actionData) {
        int diff = 0;
        if (actionData.getSeverity().contains("blockers")) {
            diff += violationsHistoryDiff.getRemovedViolations().getBlockers() - violationsHistoryDiff.getAddedViolations().getBlockers();
        }
        if (actionData.getSeverity().contains("criticals")) {
            diff += violationsHistoryDiff.getRemovedViolations().getCriticals() - violationsHistoryDiff.getAddedViolations().getCriticals();
        }
        if (actionData.getSeverity().contains("majors")) {
            diff += violationsHistoryDiff.getRemovedViolations().getMajors() - violationsHistoryDiff.getAddedViolations().getMajors();
        }
        if (actionData.getSeverity().contains("minors")) {
            diff += violationsHistoryDiff.getRemovedViolations().getMinors() - violationsHistoryDiff.getAddedViolations().getMinors();
        }
        if (actionData.getSeverity().contains("infos")) {
            diff += violationsHistoryDiff.getRemovedViolations().getInfos() - violationsHistoryDiff.getAddedViolations().getInfos();
        }
        return diff;
    }

    private boolean groupWasImproved(GroupViolationsHistoryDiff violationsHistoryDiff, NoImprovementWebhookActionData actionData) {
        int diff = countDiff(violationsHistoryDiff, actionData);
        return diff > 0;
    }

}

@@ -1517,35 +1517,33 @@
 LocalDate.of
-(2018, 1, 2
+EpochDay(
 0), LocalDat
@@ -1540,38 +1540,28 @@
 , LocalDate.
-of(2018, 1, 23
+now(
 ));%0A        
@@ -1970,49 +1970,42 @@
 ate.
-of(2018, 1, 20), LocalDate.of(2018, 1, 23
+now().minusDays(1), LocalDate.now(
 ));%0A

fix zakresu dat