package org.owntracks.android.data.repos;

import android.util.EventLog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.EventBusBuilder;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.owntracks.android.App;
import org.owntracks.android.injection.components.AppComponent;
import org.owntracks.android.injection.components.DaggerAppComponent;
import org.owntracks.android.injection.modules.AppModule;
import org.owntracks.android.messages.MessageLocation;
import org.owntracks.android.model.FusedContact;
import org.owntracks.android.support.ContactImageProvider;
import org.owntracks.android.support.Events;
import org.owntracks.android.support.Preferences;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest({App.class})
public class MemoryContactsRepoTest {
    @Mock
        App app;

    private MessageLocation messageLocation;
    private ContactsRepo contactsRepo;
    private final static String CONTACT_ID = "abcd1234";
    private EventBus eventBus;
    private ContactImageProvider contactImageProvider;


    @Before
    public void setup() {
        eventBus = EventBus.getDefault();
        contactImageProvider = new ContactImageProvider();

        mockStatic(App.class);
        when(App.getDisplayDensity()).thenReturn(160); // Should return DENSITY_MEDIUM;

        messageLocation = new MessageLocation();
        messageLocation.setAcc(10);
        messageLocation.setAlt(20);
        messageLocation.setBatt(30);
        messageLocation.setConn("TestConn");
        messageLocation.setLat(50.1);
        messageLocation.setLon(60.2);
        messageLocation.setTst(123456789);

        contactsRepo = new MemoryContactsRepo(eventBus, contactImageProvider);

    }

    @Test
    public void repoCorrectlyRegistersToEventBus() {
        assertTrue(eventBus.isRegistered(contactsRepo));
    }

    @Test
    public void repoCorrectlyUpdatesContactWithMessageLocation() {
        assertEquals(0, contactsRepo.getRevision());
        contactsRepo.update(CONTACT_ID, messageLocation);
        assertEquals(1, contactsRepo.getRevision());

        FusedContact c = contactsRepo.getById(CONTACT_ID);
        assertEquals(messageLocation, c.getMessageLocation());
        assertEquals(messageLocation.getTst(), c.getTst());
        assertEquals(CONTACT_ID, c.getId());
    }

    @Test
    public void repoCorrectlyRemovesContactById() {
        assertEquals(0, contactsRepo.getRevision());
        contactsRepo.update(CONTACT_ID, messageLocation);
        assertEquals(1, contactsRepo.getRevision());

        FusedContact c = contactsRepo.getById(CONTACT_ID);
        assertFalse(c.isDeleted());

        contactsRepo.remove(CONTACT_ID);
        assertEquals(-MemoryContactsRepo.MAJOR_STEP, contactsRepo.getRevision());
        assertTrue(c.isDeleted());
        assertNull(contactsRepo.getById(CONTACT_ID));
    }


    @Test
    public void repoCorrectlyHandlesEventModeChanged() {
        contactsRepo.update(CONTACT_ID, messageLocation);
        MemoryContactsRepo.class.cast(contactsRepo).onEventMainThread(new Events.ModeChanged(0,1));
        assertEquals(0, contactsRepo.getAll().size());

    }

    @Test
    public void repoCorrectlyHandlesEventEndpointChanged() {
        contactsRepo.update(CONTACT_ID, messageLocation);
        MemoryContactsRepo.class.cast(contactsRepo).onEventMainThread(new Events.EndpointChanged());
        assertEquals(0, contactsRepo.getAll().size());

    }


}
@@ -36,16 +36,90 @@
 repos;%0A%0A
+import android.content.res.Resources;%0Aimport android.util.DisplayMetrics;%0A
 import a
@@ -1751,82 +1751,149 @@
     
-contactImageProvider = new ContactImageProvider();%0A%0A        mockStatic(App
+DisplayMetrics displayMetrics = new DisplayMetrics();%0A        displayMetrics.densityDpi = 160;%0A        Resources myResources = mock(Resources
 .cla
@@ -1910,19 +1910,27 @@
    when(
-App
+myResources
 .getDisp
@@ -1936,15 +1936,15 @@
 play
-Density
+Metrics
 ()).
@@ -1958,45 +1958,175 @@
 urn(
-160); // Should return DENSITY_MEDIUM
+displayMetrics);%0A        when(app.getResources()).thenReturn(myResources);%0A        contactImageProvider = new ContactImageProvider(app);%0A%0A        mockStatic(App.class)
 ;%0A%0A 

Fix MemoryContactsRepoTest