package org.helioviewer.jhv.timelines.band;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.helioviewer.jhv.timelines.draw.YAxis;
import org.json.JSONArray;
import org.json.JSONObject;

class BandCacheAll implements BandCache {

    private static final int MAX_SIZE = 86400;

    private final List<DateValue> datevals = new ArrayList<>();
    private boolean hasData;

    @Override
    public boolean hasData() {
        return hasData;
    }

    @Override
    public void addToCache(YAxis yAxis, float[] values, long[] dates) {
        int len = values.length;
        if (len > 0) {
            hasData = true;
        }
        if (datevals.size() >= MAX_SIZE) {
            return;
        }

        for (int i = 0; i < len; i++) {
            if (datevals.size() >= MAX_SIZE)
                break;
            datevals.add(new DateValue(dates[i], yAxis.clip(values[i])));
        }
        Collections.sort(datevals);
    }

    @Override
    public float[] getBounds(long start, long end) {
        float min = Float.POSITIVE_INFINITY;
        float max = Float.NEGATIVE_INFINITY;

        for (DateValue dv : datevals) {
            if (dv.value != YAxis.BLANK && start <= dv.milli && dv.milli <= end) {
                min = Math.min(dv.value, min);
                max = Math.max(dv.value, max);
            }
        }
        return new float[]{min, max};
    }

    @Override
    public List<List<DateValue>> getValues(double graphWidth, long start, long end) {
        List<List<DateValue>> ret = new ArrayList<>();
        List<DateValue> list = new ArrayList<>();
        for (DateValue dv : datevals) {
            if (dv.value == YAxis.BLANK) {
                ret.add(list);
                list = new ArrayList<>();
            } else if (start <= dv.milli && dv.milli <= end) {
                list.add(dv);
            }
        }
        ret.add(list);
        return ret;
    }

    @Override
    public float getValue(long ts) {
        return YAxis.BLANK;
    }

    @Override
    public void serialize(JSONObject jo, double f) {
        JSONArray ja = new JSONArray();
        datevals.forEach(dv -> dv.serialize(ja, f));
        jo.put("data", ja);
    }

}

@@ -354,17 +354,17 @@
 ue%3E date
-v
+V
 als = ne
@@ -408,16 +408,73 @@
 hasData;
+%0A    private DateValue first;%0A    private DateValue last;
 %0A%0A    @O
@@ -731,33 +731,33 @@
         if (date
-v
+V
 als.size() %3E= MA
@@ -853,25 +853,25 @@
     if (date
-v
+V
 als.size() %3E
@@ -917,25 +917,25 @@
         date
-v
+V
 als.add(new 
@@ -1018,20 +1018,104 @@
 ort(date
-vals
+Vals);%0A%0A        first = dateVals.get(0);%0A        last = dateVals.get(dateVals.size() - 1
 );%0A    %7D
@@ -1298,33 +1298,33 @@
 eValue dv : date
-v
+V
 als) %7B%0A         
@@ -1797,17 +1797,17 @@
 v : date
-v
+V
 als) %7B%0A 
@@ -2135,24 +2135,248 @@
 (long ts) %7B%0A
+        int len = dateVals.size();%0A        int idx = (int) ((len - 1) * 1. * (ts - first.milli) / (last.milli - first.milli) + 0.5);%0A        if (idx %3E= 0 && idx %3C len) %7B%0A            return dateVals.get(idx).value;%0A        %7D%0A
         retu
@@ -2517,17 +2517,17 @@
     date
-v
+V
 als.forE

Read values from BandCacheAll