package net.heartshield.data;

import junit.framework.TestCase;

import net.heartshield.util.JsonUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.TimeZone;

public class MeasurementTest extends TestCase {
    public void testSerialize() {
        long time = 1491508656L;
        Measurement m = new Measurement("01234567", time, TimeZone.getTimeZone("Europe/London"));
        String js = m.serialize(JsonUtil.Target.LOCAL);
        String expected = "{\n" +
                "  \"startTime\": 1491508656,\n" +
                "  \"appId\": \"01234567\",\n" +
                "  \"meta\": {\n" +
                "    \"timeZone\": \"Europe/London\",\n" +
                "    \"lockTime\": 0.0,\n" +
                "    \"littleEndian\": true,\n" +
                "    \"test\": false,\n" +
                "    \"ppgMeanFps\": 0.0,\n" +
                "    \"audioFps\": 0.0,\n" +
                "    \"bcgFps\": 0.0\n" +
                "  }\n" +
                "}";
        assertEquals(expected, js);
    }

    public static Measurement measurement1() throws IOException {
        //throw new RuntimeException(new File("").getAbsolutePath());
        // working directories:
        // gradle: prevent-android/app
        // XX studio: prevent-android -> set $MODULE_DIR$ as working directory in Android Studio, see https://issuetracker.google.com/issues/37030346

        FileInputStream fis = new FileInputStream("src/androidTest/res/raw/measurement_1492009549_7f07f378.json");
        String js = FileUtil.readFileString(fis);
        Measurement m = Measurement.deserialize(js);
        return m;
    }

    public void testDeserialize() throws IOException {
        Measurement m = measurement1();
        assertEquals(1492002349, m.startTime);
        assertEquals("7F07F378", m.appId);
        assertEquals(null, m.result);
        assertNotNull(m.user);
        assertEquals(40, m.user.age);
        assertEquals(UserMeta.Gender.MALE, m.user.gender);
    }

    public void testInvariance() {
        long time = 1491508656L;
        Measurement m = new Measurement("01234567", time);
        Measurement d = Measurement.deserialize(m.serialize(JsonUtil.Target.LOCAL));
        assertEquals("Measurement must stay the same after chained serialization/deserialization", m, d);
    }

    private Measurement getDummyMeasurement() {

        Measurement measurement = new Measurement("01234567");

        measurement.meta.test = true;

        measurement.result = new MeasurementResult();
        //measurement.result.status = MeasurementResult.Status.UNCLASSIFIED;
        measurement.result.status = MeasurementResult.Status.OK;

        measurement.result.pred = 0.3;

        double[] filtered = new double[1800];
        for(int i = 0; i < filtered.length; i++)
            filtered[i] = Math.sin(2*Math.PI*1.0*i/30.0);
        measurement.result.filtered = filtered;

        int[] idx = new int[60];
        double[] ibis = new double[60];
        for(int i = 15, j = 0; i < filtered.length; i += 30, j++) {
            idx[j] = i;
            ibis[j] = 1.0;
        }
        measurement.result.idx = idx;
        measurement.result.ibis = ibis;

        MeasurementResult.HrvTimeDomain hrvTimeDomain = new MeasurementResult.HrvTimeDomain();
        MeasurementResult.HrvFrequencyDomain hrvFrequencyDomain = new MeasurementResult.HrvFrequencyDomain();

        hrvTimeDomain.sdnn = 93.123;
        hrvTimeDomain.rmssd = 104.1234;
        hrvTimeDomain.pnn50 = 74.71111;
        hrvTimeDomain.hrv_tri = 13.511;

        hrvFrequencyDomain.total_power = 5871.1234;
        hrvFrequencyDomain.lf = 2389.1234;
        hrvFrequencyDomain.hf = 3369.1234;

        measurement.result.hrvTimeDomain = hrvTimeDomain;
        measurement.result.hrvFrequencyDomain = hrvFrequencyDomain;

        return measurement;
    }
}
