// Path: app/src/main/java/de/dorianscholz/openlibre/OpenLibre.java
// public static RealmConfiguration realmConfigProcessedData;
// 
// Path: app/src/main/java/de/dorianscholz/openlibre/model/SensorData.java
// public static final long maxSensorAgeInMinutes = TimeUnit.DAYS.toMinutes(14); // data generated by the sensor after 14 days also has faults
// 
// Path: app/src/main/java/de/dorianscholz/openlibre/model/SensorData.java
// public static final long minSensorAgeInMinutes = 60; // data generated by the sensor in the first 60 minutes is not correct


import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.RealmResults;
import io.realm.Sort;
import io.realm.annotations.PrimaryKey;
import static de.dorianscholz.openlibre.OpenLibre.realmConfigProcessedData;
import static de.dorianscholz.openlibre.model.SensorData.maxSensorAgeInMinutes;
import static de.dorianscholz.openlibre.model.SensorData.minSensorAgeInMinutes;
import static java.lang.Math.min;
    public static final int historyIntervalInMinutes = 15;
    public static final int numTrendValues = 16;

    @PrimaryKey
    private String id;
    private SensorData sensor;
    private int sensorAgeInMinutes = -1;
    private long date = -1;
    private int timezoneOffsetInMinutes;
    private RealmList<GlucoseData> trend = new RealmList<>();
    private RealmList<GlucoseData> history = new RealmList<>();

    public ReadingData() {}
    public ReadingData(RawTagData rawTagData) {
        id = rawTagData.getId();
        date = rawTagData.getDate();
        timezoneOffsetInMinutes = rawTagData.getTimezoneOffsetInMinutes();
        sensorAgeInMinutes = rawTagData.getSensorAgeInMinutes();

        Realm realmProcessedData = Realm.getInstance(realmConfigProcessedData);

        // find or create entry for this sensor
        RealmResults<SensorData> sensors = realmProcessedData.where(SensorData.class).contains(SensorData.ID, rawTagData.getTagId()).findAll();
        if (sensors.size() > 0) {
            sensor = sensors.first();
        } else {
            sensor = new SensorData(rawTagData);
        }

        // check if sensor is of valid age
// Path: app/src/main/java/de/dorianscholz/openlibre/OpenLibre.java
// public static RealmConfiguration realmConfigProcessedData;
// 
// Path: app/src/main/java/de/dorianscholz/openlibre/model/SensorData.java
// public static final long maxSensorAgeInMinutes = TimeUnit.DAYS.toMinutes(14); // data generated by the sensor after 14 days also has faults
// 
// Path: app/src/main/java/de/dorianscholz/openlibre/model/SensorData.java
// public static final long minSensorAgeInMinutes = 60; // data generated by the sensor in the first 60 minutes is not correct


// Path: app/src/main/java/de/dorianscholz/openlibre/model/ReadingData.java
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.RealmResults;
import io.realm.Sort;
import io.realm.annotations.PrimaryKey;
import static de.dorianscholz.openlibre.OpenLibre.realmConfigProcessedData;
import static de.dorianscholz.openlibre.model.SensorData.maxSensorAgeInMinutes;
import static de.dorianscholz.openlibre.model.SensorData.minSensorAgeInMinutes;
import static java.lang.Math.min;

    public static final int historyIntervalInMinutes = 15;
    public static final int numTrendValues = 16;

    @PrimaryKey
    private String id;
    private SensorData sensor;
    private int sensorAgeInMinutes = -1;
    private long date = -1;
    private int timezoneOffsetInMinutes;
    private RealmList<GlucoseData> trend = new RealmList<>();
    private RealmList<GlucoseData> history = new RealmList<>();

    public ReadingData() {}
    public ReadingData(RawTagData rawTagData) {
        id = rawTagData.getId();
        date = rawTagData.getDate();
        timezoneOffsetInMinutes = rawTagData.getTimezoneOffsetInMinutes();
        sensorAgeInMinutes = rawTagData.getSensorAgeInMinutes();

        Realm realmProcessedData = Realm.getInstance(realmConfigProcessedData);

        // find or create entry for this sensor
        RealmResults<SensorData> sensors = realmProcessedData.where(SensorData.class).contains(SensorData.ID, rawTagData.getTagId()).findAll();
        if (sensors.size() > 0) {
            sensor = sensors.first();
        } else {
            sensor = new SensorData(rawTagData);
        }

        // check if sensor is of valid age
        if (sensorAgeInMinutes <= minSensorAgeInMinutes || sensorAgeInMinutes > maxSensorAgeInMinutes) {