package uk.gov.indexer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.gov.indexer.dao.DestinationDBUpdateDAO;
import uk.gov.indexer.dao.Entry;
import uk.gov.indexer.dao.SourceDBQueryDAO;
import uk.gov.indexer.monitoring.CloudwatchRecordsProcessedUpdater;

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

public class IndexerTask implements Runnable {
    private final Logger LOGGER = LoggerFactory.getLogger(IndexerTask.class);

    private final String register;
    private final DestinationDBUpdateDAO destinationDBUpdateDAO;
    private final SourceDBQueryDAO sourceDBQueryDAO;
    private final Optional<CloudwatchRecordsProcessedUpdater> cloudwatchUpdater;

    public IndexerTask(Optional<CloudwatchRecordsProcessedUpdater> cloudwatchUpdater, String register, SourceDBQueryDAO sourceDBQueryDAO, DestinationDBUpdateDAO destinationDBUpdateDAO) {
        this.register = register;
        this.sourceDBQueryDAO = sourceDBQueryDAO;
        this.destinationDBUpdateDAO = destinationDBUpdateDAO;
        this.cloudwatchUpdater = cloudwatchUpdater;
    }

    @Override
    public void run() {
        try {
            LOGGER.info("Starting update for: " + register);
            update();
            LOGGER.info("Finished for register: " + register);
        } catch (Throwable e) {
            LOGGER.error(ExceptionFormatter.formatExceptionAsString(e));
            throw e;
        }
    }

    protected void update() {
        List<Entry> entries = fetchNewEntries();

        if (entries.isEmpty()) {
            updateCloudWatch(0);
        } else {
            do {
                int totalNewEntries = entries.size();

                LOGGER.info(String.format("Register '%s': Found '%s' new entries.", register, totalNewEntries));

                destinationDBUpdateDAO.writeEntriesInBatch(register, entries);

                LOGGER.info(String.format("Register '%s': Copied '%s' entries in database from index '%s'.", register, totalNewEntries, entries.get(0).serial_number));

                updateCloudWatch(totalNewEntries);

                LOGGER.info(String.format("Register '%s': Notified Cloudwatch about '%s' entries processed.", register, totalNewEntries));
            } while (!(entries = fetchNewEntries()).isEmpty());
        }

    }

    private void updateCloudWatch(final int totalEntriesWritten) {
        cloudwatchUpdater.ifPresent(cwu -> cwu.update(totalEntriesWritten));
    }

    private List<Entry> fetchNewEntries() {
        return sourceDBQueryDAO.read(destinationDBUpdateDAO.lastReadSerialNumber());
    }

}

@@ -1717,17 +1717,17 @@
 Found '%25
-s
+d
 ' new en
@@ -1912,17 +1912,17 @@
 opied '%25
-s
+d
 ' entrie
@@ -1948,17 +1948,17 @@
 index '%25
-s
+d
 '.%22, reg
@@ -2152,17 +2152,17 @@
 about '%25
-s
+d
 ' entrie

Correct String.format format specifiers. For numbers, switched from %s to %d.