package be.cegeka.batchers.taxcalculator.batch.config.listeners;

import be.cegeka.batchers.taxcalculator.application.service.EmployeeService;
import be.cegeka.batchers.taxcalculator.batch.api.events.JobProgressEvent;
import be.cegeka.batchers.taxcalculator.batch.domain.JobStartParams;
import be.cegeka.batchers.taxcalculator.batch.mapping.JobStartParamsMapper;
import com.google.common.eventbus.EventBus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Used on single JVM
 */
@Component
@Qualifier("singleJVM")
@StepScope
@Profile(value = {"default", "test", "singleJVM", "remotePartitioningMaster", "remotePartitioningSlave"})
public class SingleJVMJobProgressListener implements JobProgressListener {

    private AtomicInteger lastPercentageComplete;
    private AtomicLong currentItemCount;
    private int totalItemCount;
    private JobStartParams jobStartParams;
    private String stepName;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private EventBus eventBus;

    @Override
    public void beforeStep(StepExecution stepExecution) {
        totalItemCount = employeeService.getEmployeeCount().intValue();
        jobStartParams = new JobStartParamsMapper().map(stepExecution.getJobParameters());
        stepName = stepExecution.getStepName();
        currentItemCount = new AtomicLong();
        lastPercentageComplete = new AtomicInteger();
        eventBus.post(new JobProgressEvent(jobStartParams, stepName, 0));
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return null;
    }

    @Override
    public void beforeWrite(List items) {
    }

    @Override
    public void afterWrite(List items) {
        currentItemCount.addAndGet(items.size());
        int percentageComplete = currentItemCount.intValue() * 100 / totalItemCount;

        sentUpdateIfNeeded(percentageComplete);
    }

    private synchronized void sentUpdateIfNeeded(int percentageComplete) {
        if (percentageComplete > lastPercentageComplete.get()) {
            lastPercentageComplete = new AtomicInteger(percentageComplete);
            eventBus.post(new JobProgressEvent(jobStartParams, stepName, lastPercentageComplete.intValue()));
        }
    }

    @Override
    public void onWriteError(Exception exception, List items) {

    }
}

@@ -503,16 +503,198 @@
 cution;%0A
+import org.springframework.batch.core.annotation.AfterStep;%0Aimport org.springframework.batch.core.annotation.AfterWrite;%0Aimport org.springframework.batch.core.annotation.BeforeStep;%0A
 import o
@@ -1679,32 +1679,48 @@
 %0A%0A    @Override%0A
+    @BeforeStep%0A
     public void 
@@ -2158,32 +2158,47 @@
 %0A%0A    @Override%0A
+    @AfterStep%0A
     public ExitS
@@ -2341,32 +2341,48 @@
 %0A%0A    @Override%0A
+    @AfterWrite%0A
     public void 

fix singleJVM progress for taxcalculationstep