package org.helioviewer.jhv.plugins.eve.lines;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Future;

import org.helioviewer.jhv.base.interval.Interval;
import org.helioviewer.jhv.plugins.eve.EVEPlugin;
import org.helioviewer.jhv.time.TimeUtils;
import org.helioviewer.jhv.timelines.Timelines;
import org.helioviewer.jhv.timelines.band.Band;
import org.helioviewer.jhv.timelines.band.BandDataProvider;

public class EVEDataProvider implements BandDataProvider {

    private static final int DOWNLOADER_MAX_DAYS_PER_BLOCK = 21;

    private static final HashMap<Band, List<Interval>> downloadMap = new HashMap<>();
    private static final HashMap<Band, List<Future<?>>> futureJobs = new HashMap<>();

    @Override
    public void loadBand(URI uri) {
        EVEPlugin.executorService.submit(new LoadThread(uri));
    }

    @Override
    public void updateBand(Band band, long start, long end) {
        List<Interval> missingIntervalsNoExtend = band.getMissingDaysInInterval(start, end);
        if (!missingIntervalsNoExtend.isEmpty()) {
            // extend
            start -= 7 * TimeUtils.DAY_IN_MILLIS;
            end += 7 * TimeUtils.DAY_IN_MILLIS;

            ArrayList<Interval> intervals = getIntervals(band, start, end);
            if (intervals == null) {
                return;
            }

            int n = intervals.size();
            if (n == 0) {
                Timelines.getModel().downloadStarted(band);
                return;
            }

            addDownloads(band, intervals);
        }
    }

    private static ArrayList<Interval> getIntervals(Band band, long start, long end) {
        List<Interval> missingIntervals = band.addRequest(start, end);
        if (missingIntervals.isEmpty())
            return null;

        ArrayList<Interval> intervals = new ArrayList<>();
        for (Interval i : missingIntervals) {
            intervals.addAll(Interval.splitInterval(i, DOWNLOADER_MAX_DAYS_PER_BLOCK));
        }
        return intervals;
    }

    private static void addFutureJobs(Band band, List<Future<?>> newFutureJobs) {
        List<Future<?>> fl = futureJobs.computeIfAbsent(band, k -> new ArrayList<>(newFutureJobs.size()));
        fl.addAll(newFutureJobs);
    }

    private static void addDownloads(Band band, List<Interval> intervals) {
        int size = intervals.size();
        List<Interval> dl = downloadMap.computeIfAbsent(band, k -> new ArrayList<>(size));
        List<Future<?>> fl = new ArrayList<>(size);
        for (Interval interval : intervals) {
            dl.add(interval);
            fl.add(EVEPlugin.executorService.submit(new DownloadThread(band, interval)));
        }
        addFutureJobs(band, fl);
    }

    static void downloadFinished(Band band, Interval interval) {
        List<Interval> list = downloadMap.get(band);
        if (list != null) {
            list.remove(interval);
            if (list.isEmpty())
                downloadMap.remove(band);
        }
        Timelines.getModel().downloadFinished(band);
    }

    @Override
    public void stopDownloads(Band band) {
        List<Interval> list = downloadMap.get(band);
        if (list == null)
            return;
        if (list.isEmpty())
            downloadMap.remove(band);

        List<Future<?>> fjs = futureJobs.get(band);
        for (Future<?> fj : fjs) {
            fj.cancel(true);
        }
        futureJobs.remove(band);
        Timelines.getModel().downloadFinished(band);
    }

    @Override
    public boolean isDownloadActive(Band band) {
        List<Interval> list = downloadMap.get(band);
        return list != null && !list.isEmpty();
    }

}

@@ -2076,238 +2076,8 @@
  %7D%0A%0A
-    private static void addFutureJobs(Band band, List%3CFuture%3C?%3E%3E newFutureJobs) %7B%0A        List%3CFuture%3C?%3E%3E fl = futureJobs.computeIfAbsent(band, k -%3E new ArrayList%3C%3E(newFutureJobs.size()));%0A        fl.addAll(newFutureJobs);%0A    %7D%0A%0A
     
@@ -2140,32 +2140,32 @@
 l%3E intervals) %7B%0A
+
         int size
@@ -2304,16 +2304,54 @@
 ?%3E%3E fl =
+ futureJobs.computeIfAbsent(band, k -%3E
  new Arr
@@ -2364,16 +2364,17 @@
 %3C%3E(size)
+)
 ;%0A      
@@ -2529,24 +2529,24 @@
 nterval)));%0A
+
         %7D%0A  
@@ -2547,41 +2547,8 @@
   %7D%0A
-        addFutureJobs(band, fl);%0A
     

Remove pointless list