package info.nightscout.androidaps.plugins.Insulin;

import org.junit.Before;
import org.junit.Test;

import info.nightscout.androidaps.data.Iob;
import info.nightscout.androidaps.db.Treatment;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Created by triplem on 07.01.18.
 */

public class InsulinOrefBasePluginTest extends InsulinOrefBasePlugin {

    private int peak;
    private double dia;

    private boolean shortDiaNotificationSend;

    @Before
    public void setUp() throws Exception {
        this.peak = 0;
        this.dia = InsulinOrefBasePlugin.MIN_DIA;
        this.shortDiaNotificationSend = false;
    }

    @Test
    public void testGetDia() throws Exception {
        assertEquals(InsulinOrefBasePlugin.MIN_DIA, this.getDia(), 0);

        this.dia = InsulinOrefBasePlugin.MIN_DIA + 1;
        assertEquals(InsulinOrefBasePlugin.MIN_DIA + 1, this.getDia(), 0);

        this.dia = InsulinOrefBasePlugin.MIN_DIA - 1;
        assertEquals(InsulinOrefBasePlugin.MIN_DIA, this.getDia(), 0);
        assertTrue(this.shortDiaNotificationSend);
    }

    @Test
    public void testIobCalcForTreatment() {
        Treatment treatment = new Treatment();
        Iob expected = new Iob();

        assertEquals(expected, this.iobCalcForTreatment(treatment, 0, 0d));

        this.peak = 30;
        long time = System.currentTimeMillis();
        treatment.date = time - 1 * 60 * 60 * 1000; // 1 hour
        treatment.insulin = 10d;

        assertEquals(3.92, this.iobCalcForTreatment(treatment, time).iobContrib, 0.1);
    }


    /**
     * this method is implemented to allow tests of the iobCalcForTreatment calculation
      * @return
     */
    @Override
    int getPeak() {
        return this.peak;
    }

    /**
     * Userdefined Dia is implemented to allow tests of the getDia method
     *
     * @return
     */
    public double getUserDefinedDia() {
        return this.dia;
    }

    void sendShortDiaNotification(double dia) {
        this.shortDiaNotificationSend = true;
    }


    // the following methods are implemented, but not needed...
    @Override
    String commentStandardText() {
        return null;
    }

    @Override
    public String getFragmentClass() {
        return null;
    }

    @Override
    public int getId() {
        return 0;
    }

    @Override
    public String getName() {
        return null;
    }

    @Override
    public String getFriendlyName() {
        return null;
    }

    @Override
    public boolean isEnabled(int type) {
        return false;
    }

    @Override
    public boolean isVisibleInTabs(int type) {
        return false;
    }

    @Override
    public void setFragmentEnabled(int type, boolean fragmentEnabled) {

    }

    @Override
    public void setFragmentVisible(int type, boolean fragmentVisible) {

    }

    @Override
    public int getPreferencesId() {
        return 0;
    }
}

@@ -1349,16 +1349,38 @@
 k = 30;%0A
+        this.dia = 4;%0A
         
@@ -1418,16 +1418,236 @@
 illis();
+%0A%0A        // check directly after bolus%0A        treatment.date = time;%0A        treatment.insulin = 10d;%0A        assertEquals(10d, this.iobCalcForTreatment(treatment, time).iobContrib, 0.1);%0A%0A        // check after 1 hour
 %0A       
@@ -1734,17 +1734,16 @@
  = 10d;%0A
-%0A
         
@@ -1820,16 +1820,640 @@
 b, 0.1);
+%0A%0A        // check after 2 hour%0A        treatment.date = time - 2 * 60 * 60 * 1000; // 1 hour%0A        treatment.insulin = 10d;%0A        assertEquals(0.77, this.iobCalcForTreatment(treatment, time).iobContrib, 0.1);%0A%0A        // check after 3 hour%0A        treatment.date = time - 3 * 60 * 60 * 1000; // 1 hour%0A        treatment.insulin = 10d;%0A        assertEquals(0.10, this.iobCalcForTreatment(treatment, time).iobContrib, 0.1);%0A%0A        // check after dia%0A        treatment.date = time - 4 * 60 * 60 * 1000;%0A        treatment.insulin = 10d;%0A        assertEquals(0d, this.iobCalcForTreatment(treatment, time).iobContrib, 0.1);
 %0A    %7D%0A%0A

Enhance insulin plugin test