package info.nightscout.androidaps.plugins.general.automation.triggers;

import com.google.common.base.Optional;
import com.squareup.otto.Bus;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.GregorianCalendar;

import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.utils.DateUtil;

import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, Bus.class, DateUtil.class, GregorianCalendar.class})
public class TriggerTimeRangeTest {

    int now = 754;
    String timeJson = "{\"data\":{\"lastRun\":0,\"start\":753,\"end\":784},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTimeRange\"}";

    @Test
    public void shouldRunTest() {

        // scheduled 1 min before
        TriggerTimeRange t;

        // scheduled 1 min in the future
        t = new TriggerTimeRange().period(now + 1, now + 30);
        Assert.assertFalse(t.shouldRun());

        // already run
        t = new TriggerTimeRange().period(now - 1, now + 30).lastRun(now - 1);
        Assert.assertFalse(t.shouldRun());

    }

    @Test
    public void toJSONTest() {
        TriggerTimeRange t = new TriggerTimeRange().period(now - 1, now + 30);
        Assert.assertEquals(timeJson, t.toJSON());
    }

    @Test
    public void fromJSONTest() throws JSONException {
        TriggerTimeRange t = new TriggerTimeRange().period(120 , 180);

        TriggerTimeRange t2 = (TriggerTimeRange) Trigger.instantiate(new JSONObject(t.toJSON()));
        Assert.assertEquals(now - 1, t2.period(753 , 360).getStart());
        Assert.assertEquals(360, t2.period(753 , 360).getEnd());
    }

    @Test
    public void copyConstructorTest() {
        TriggerTimeRange t = new TriggerTimeRange();
        t.period(now, now + 30);

        TriggerTimeRange t1 = (TriggerTimeRange) t.duplicate();
//        Assert.assertEquals(now, t1.getRunAt(), 0.01d);
    }

    @Test
    public void friendlyNameTest() {
        Assert.assertEquals(R.string.time_range, new TriggerTimeRange().friendlyName());
    }

    @Test
    public void friendlyDescriptionTest() {
        Assert.assertEquals(null, new TriggerTimeRange().friendlyDescription()); //not mocked    }
    }

    @Test
    public void iconTest() {
        Assert.assertEquals(Optional.of(R.drawable.ic_access_alarm_24dp), new TriggerTimeRange().icon());

    }

    @Before
    public void mock() {
        AAPSMocker.mockMainApp();
        AAPSMocker.mockBus();

        PowerMockito.mockStatic(DateUtil.class);
        when(DateUtil.now()).thenReturn((long) now*60*60*10000);

        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(now);
        when(DateUtil.gregorianCalendar()).thenReturn(calendar);
    }
}

@@ -1123,31 +1123,202 @@
     
-// scheduled 1 min befo
+TriggerTimeRange t;%0A//        when(System.currentTimeMillis()).thenReturn((long) now*60000);%0A        when(DateUtil.now()).thenReturn((long) now*60000);%0A%0A        // range starts 1 min in the futu
 re%0A 
@@ -1316,32 +1316,40 @@
  future%0A        
+t = new 
 TriggerTimeRange
@@ -1352,112 +1352,359 @@
 ange
- t;%0A%0A        // scheduled 1 min in the future%0A        t = new TriggerTimeRange().period(now + 1, now + 3
+().period(now + 1, now + 30);%0A        Assert.assertEquals(false, t.shouldRun());%0A%0A        // range starts 30 min back%0A        t = new TriggerTimeRange().period((int) (System.currentTimeMillis() - 30*60000), now + 30);%0A        Assert.assertEquals(true, t.shouldRun());%0A%0A        // Period is all day long%0A        t = new TriggerTimeRange().period(1, 144
 0);%0A
@@ -1716,38 +1716,45 @@
    Assert.assert
-False(
+Equals(true, 
 t.shouldRun());%0A

fix tests