/*
 * Copyright 2020 The RoboZonky Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.robozonky.app.daemon;

import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicReference;

import com.github.robozonky.internal.Defaults;
import com.github.robozonky.internal.remote.Select;
import com.github.robozonky.internal.test.DateUtil;
import org.apache.logging.log4j.Logger;

/**
 * The purpose of this is that marketplace checks are coupled to information about latest updates to those marketplaces.
 * Each instance of an implementing class may decide to cache or otherwise alter the marketplace during
 * {@link #hasUpdates()} and only ever return that in {@link #getMarketplace()}.
 * @param <T> Type of the entity coming from the marketplace.
 */
abstract class AbstractMarketplaceAccessor<T> {

    private final AtomicReference<Instant> lastFullMarketplaceCheckReference = new AtomicReference<>(Instant.EPOCH);

    protected Select getIncrementalFilter() {
        Instant lastFullMarketplaceCheck = lastFullMarketplaceCheckReference.get();
        Instant now = DateUtil.now();
        if (lastFullMarketplaceCheck.plus(getForcedMarketplaceCheckInterval()).isBefore(now)) {
            lastFullMarketplaceCheckReference.set(now);
            getLogger().debug("Running full marketplace check with timestamp of {}, previous was {}.", now,
                              lastFullMarketplaceCheck);
            return getBaseFilter();
        } else {
            var filter = getBaseFilter()
                    .greaterThanOrEquals("datePublished",
                                         OffsetDateTime.ofInstant(lastFullMarketplaceCheck, Defaults.ZONE_ID));
            getLogger().debug("Running incremental marketplace check, starting from {}.", lastFullMarketplaceCheck);
            return filter;
        }
    }

    protected abstract Select getBaseFilter();

    public abstract Duration getForcedMarketplaceCheckInterval();

    public abstract Collection<T> getMarketplace();

    public abstract boolean hasUpdates();

    protected abstract Logger getLogger();

}

@@ -727,16 +727,54 @@
 teTime;%0A
+import java.time.temporal.ChronoUnit;%0A
 import j
@@ -1842,24 +1842,124 @@
 ore(now)) %7B%0A
+            Instant newTimestamp = now.truncatedTo(ChronoUnit.SECONDS); // Go a couple millis back.%0A
             
@@ -1997,18 +1997,27 @@
 ce.set(n
-ow
+ewTimestamp
 );%0A     
@@ -2115,18 +2115,27 @@
  %7B%7D.%22, n
-ow
+ewTimestamp
 ,%0A      

Add a couple milliseconds of safety to incremental marketplace check