package info.nightscout.androidaps.plugins.pump.omnipod.defs.state;

import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeZone;
import org.joda.time.Duration;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mock;

import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.plugins.pump.omnipod.defs.FirmwareVersion;
import info.nightscout.androidaps.plugins.pump.omnipod.driver.comm.AapsPodStateManager;

import static org.junit.Assert.assertEquals;

public class AapsPodStateManagerTest {
    @Mock HasAndroidInjector hasAndroidInjector;

    @Test
    @Ignore("Not Dagger compliant") // FIXME
    public void times() {
        DateTimeZone timeZone = DateTimeZone.UTC;
        DateTimeZone.setDefault(timeZone);

        DateTime now = new DateTime(2020, 1, 1, 1, 2, 3, timeZone);

        DateTimeUtils.setCurrentMillisFixed(now.getMillis());

        AapsPodStateManager podStateManager = new AapsPodStateManager(hasAndroidInjector);
        podStateManager.initState(0x0);
        podStateManager.setPairingParameters(0, 0, new FirmwareVersion(1, 1, 1),
                new FirmwareVersion(2, 2, 2), timeZone);

        assertEquals(now, podStateManager.getTime());
        assertEquals(Duration.standardHours(1).plus(Duration.standardMinutes(2).plus(Duration.standardSeconds(3))), podStateManager.getScheduleOffset());
    }

    @Test
    @Ignore("Not Dagger compliant") // FIXME
    public void changeSystemTimeZoneWithoutChangingPodTimeZone() {
        DateTimeZone timeZone = DateTimeZone.UTC;
        DateTimeZone.setDefault(timeZone);

        DateTime now = new DateTime(2020, 1, 1, 1, 2, 3, timeZone);

        DateTimeUtils.setCurrentMillisFixed(now.getMillis());

        AapsPodStateManager podStateManager = new AapsPodStateManager(hasAndroidInjector);
        podStateManager.initState(0x0);
        podStateManager.setPairingParameters(0, 0, new FirmwareVersion(1, 1, 1),
                new FirmwareVersion(2, 2, 2), timeZone);

        DateTimeZone newTimeZone = DateTimeZone.forOffsetHours(2);
        DateTimeZone.setDefault(newTimeZone);

        // The system time zone has been updated, but the pod session state's time zone hasn't
        // So the pods time should not have been changed
        assertEquals(now, podStateManager.getTime());
        assertEquals(Duration.standardHours(1).plus(Duration.standardMinutes(2).plus(Duration.standardSeconds(3))), podStateManager.getScheduleOffset());
    }

    @Test
    @Ignore("Not Dagger compliant") // FIXME
    public void changeSystemTimeZoneAndChangePodTimeZone() {
        DateTimeZone timeZone = DateTimeZone.UTC;
        DateTimeZone.setDefault(timeZone);

        DateTime now = new DateTime(2020, 1, 1, 1, 2, 3, timeZone);

        DateTimeUtils.setCurrentMillisFixed(now.getMillis());

        AapsPodStateManager podStateManager = new AapsPodStateManager(hasAndroidInjector);
        podStateManager.initState(0x0);
        podStateManager.setPairingParameters(0, 0, new FirmwareVersion(1, 1, 1),
                new FirmwareVersion(2, 2, 2), timeZone);

        DateTimeZone newTimeZone = DateTimeZone.forOffsetHours(2);
        DateTimeZone.setDefault(newTimeZone);
        podStateManager.setTimeZone(newTimeZone);

        // Both the system time zone have been updated
        // So the pods time should have been changed (to +2 hours)
        assertEquals(now.withZone(newTimeZone), podStateManager.getTime());
        assertEquals(Duration.standardHours(3).plus(Duration.standardMinutes(2).plus(Duration.standardSeconds(3))), podStateManager.getScheduleOffset());
    }

    @After
    public void tearDown() {
        DateTimeUtils.setCurrentMillisSystem();
    }
}
@@ -236,22 +236,20 @@
 g.junit.
-Ignore
+Test
 ;%0Aimport
@@ -251,36 +251,46 @@
 mport org.junit.
-Test
+runner.RunWith
 ;%0Aimport org.moc
@@ -304,214 +304,548 @@
 ck;%0A
-%0A
 import 
-dagger.android.HasAndroidInjector;%0Aimport info.nightscout.androidaps.plugins.pump.omnipod.defs.FirmwareVersion;%0Aimport info.nightscout.androidaps.plugins.pump.omnipod.driver.comm.AapsPodStateManager
+org.powermock.modules.junit4.PowerMockRunner;%0A%0Aimport info.nightscout.androidaps.logging.AAPSLogger;%0Aimport info.nightscout.androidaps.plugins.bus.RxBusWrapper;%0Aimport info.nightscout.androidaps.plugins.pump.omnipod.defs.FirmwareVersion;%0Aimport info.nightscout.androidaps.plugins.pump.omnipod.driver.OmnipodPumpStatus;%0Aimport info.nightscout.androidaps.plugins.pump.omnipod.driver.comm.AapsPodStateManager;%0Aimport info.nightscout.androidaps.utils.resources.ResourceHelper;%0Aimport info.nightscout.androidaps.utils.sharedPreferences.SP
 ;%0A%0Ai
@@ -889,16 +889,48 @@
 quals;%0A%0A
+@RunWith(PowerMockRunner.class)%0A
 public c
@@ -974,102 +974,191 @@
 ock 
-HasAndroidInjector hasAndroidInjecto
+AAPSLogger aapsLogge
 r;%0A
-%0A
     @
-Test%0A    @Ignore(%22Not Dagger compliant%22) // FIXME
+Mock SP sp;%0A    @Mock OmnipodPumpStatus omnipodPumpStatus;%0A    RxBusWrapper rxBus = new RxBusWrapper();%0A    @Mock ResourceHelper resourceHelper;%0A%0A    @Test
 %0A   
@@ -1476,33 +1476,71 @@
 Manager(
-hasAndroidInjecto
+aapsLogger, sp, omnipodPumpStatus, rxBus, resourceHelpe
 r);%0A    
@@ -1943,53 +1943,8 @@
 est%0A
-    @Ignore(%22Not Dagger compliant%22) // FIXME%0A
     
@@ -2302,33 +2302,71 @@
 Manager(
-hasAndroidInjecto
+aapsLogger, sp, omnipodPumpStatus, rxBus, resourceHelpe
 r);%0A    
@@ -3035,53 +3035,8 @@
 est%0A
-    @Ignore(%22Not Dagger compliant%22) // FIXME%0A
     
@@ -3392,25 +3392,63 @@
 ger(
-hasAndroidInjecto
+aapsLogger, sp, omnipodPumpStatus, rxBus, resourceHelpe
 r);%0A

Fix AapsPodStateManagerTest