package net.buycraft.plugin.shared.tasks;

import com.google.common.collect.ImmutableList;
import lombok.RequiredArgsConstructor;
import net.buycraft.plugin.IBuycraftPlatform;
import net.buycraft.plugin.client.ApiException;
import net.buycraft.plugin.data.Category;
import net.buycraft.plugin.data.Coupon;
import net.buycraft.plugin.data.Package;
import net.buycraft.plugin.data.responses.Listing;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;

@RequiredArgsConstructor
public class CouponUpdateTask implements Runnable {
    private final IBuycraftPlatform platform;
    private final AtomicReference<List<Coupon>> listing = new AtomicReference<>();
    private final AtomicReference<Date> lastUpdate = new AtomicReference<>();
    private final Runnable updateTask;

    @Override
    public void run() {

        platform.log(Level.INFO, "Updating coupon list...");

        if (platform.getApiClient() == null) {
            // no API client
            return;
        }

        try {
            listing.set(platform.getApiClient().getAllCoupons());
        } catch (IOException | ApiException e) {
            platform.log(Level.SEVERE, "Error whilst retrieving coupon listing", e);
            return;
        }

        lastUpdate.set(new Date());

        if (updateTask != null) {
            updateTask.run();
        }
    }

    public List<Coupon> getListing() {
        List<Coupon> coupons = listing.get();
        return coupons == null ? ImmutableList.<Coupon>of() : coupons;
    }

    public Date getLastUpdate() {
        return lastUpdate.get();
    }

    public Coupon getCouponByCode(String code) {
        List<Coupon> listing = getListing();
        for (Coupon coupon : listing) {
            if (coupon.getCode().equalsIgnoreCase(code)) {
                return coupon;
            }
        }
        return null;
    }


}

@@ -873,16 +873,51 @@
 ateTask;
+%0A    private final boolean verbose;
 %0A%0A    @O
@@ -949,16 +949,43 @@
 un() %7B%0A%0A
+        if (verbose) %7B%0A    
         
@@ -1036,16 +1036,26 @@
 st...%22);
+%0A        %7D
 %0A%0A      
@@ -1553,24 +1553,65 @@
 Listing() %7B%0A
+        //Update the list%0A        run();%0A
         List

Update coupons on list, respect verbose config