package ch.pantas.billsplitter.ui;

import android.content.Intent;
import android.test.suitebuilder.annotation.LargeTest;

import org.mockito.Mock;

import java.util.LinkedList;
import java.util.List;

import ch.pantas.billsplitter.dataaccess.EventStore;
import ch.pantas.billsplitter.dataaccess.ParticipantStore;
import ch.pantas.billsplitter.dataaccess.UserStore;
import ch.pantas.billsplitter.framework.BaseEspressoTest;
import ch.pantas.billsplitter.model.Event;
import ch.pantas.billsplitter.model.User;
import ch.pantas.billsplitter.services.SharedPreferenceService;
import ch.yvu.myapplication.R;

import static ch.pantas.billsplitter.ui.EventDetails.ARGUMENT_EVENT_ID;
import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.isDisplayed;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withText;
import static org.mockito.Mockito.when;

public class AddParticipantsTest extends BaseEspressoTest<AddParticipants> {

    public AddParticipantsTest() {
        super(AddParticipants.class);
    }

    @Mock
    private EventStore eventStore;

    @Mock
    private UserStore userStore;

    @Mock
    private ParticipantStore participantStore;

    @Mock
    private ParticipantManager participantManager;

    @Mock
    private SharedPreferenceService sharedPreferenceService;

    private Event event;

    @Override
    public void setUp() throws Exception {
        super.setUp();

        event = new Event("abcd", "An Event");
        Intent intent = new Intent();
        intent.putExtra(ARGUMENT_EVENT_ID, event.getId());
        setActivityIntent(intent);
        when(eventStore.getById(event.getId())).thenReturn(event);
    }

    @LargeTest
    public void testCorrectTitleIsDisplayed() {
        // When
        getActivity();

        // Then
        onView(withText(R.string.add_event)).check(matches(isDisplayed()));
    }

    @LargeTest
    public void testSaveButtonIsDisplayed() {
        // When
        getActivity();

        // Then
        onView(withText(R.string.save)).check(matches(isDisplayed()));
    }

    @LargeTest
    public void testEditParticipantsExistingParticipantsAreShown() {
        // TODO: FIXME (NullPointerException)
        // Given
        List<User> participantsList = new LinkedList<User>();
        participantsList.add(new User("a", "Me"));
        participantsList.add(new User("b", "Hans"));
        participantsList.add(new User("c", "Fritz"));
        when(participantManager.getParticipants()).thenReturn(participantsList);
        when(sharedPreferenceService.getUserName()).thenReturn("Me");
        // When
        getActivity();

        // Then
        onView(withText("Me")).check(matches(isDisplayed()));
        onView(withText("Hans")).check(matches(isDisplayed()));
        onView(withText("Fritz")).check(matches(isDisplayed()));
    }

    // TODO: Participants UI tests
}

@@ -566,16 +566,70 @@
 ervice;%0A
+import ch.pantas.billsplitter.ui.adapter.UserAdapter;%0A
 import c
@@ -696,30 +696,24 @@
 .ui.
-EventDetails.ARGUMENT_
+AddParticipants.
 EVEN
@@ -1094,16 +1094,139 @@
 thText;%0A
+import static org.mockito.Matchers.any;%0Aimport static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify;%0A
 import s
@@ -1694,24 +1694,71 @@
 ceService;%0A%0A
+    @Mock%0A    UserAdapter participantAdapter;%0A%0A
     private 
@@ -1965,17 +1965,8 @@
 tra(
-ARGUMENT_
 EVEN
@@ -2084,24 +2084,92 @@
 turn(event);
+%0A%0A        when(participantAdapter.getViewTypeCount()).thenReturn(1);
 %0A    %7D%0A%0A    
@@ -2649,54 +2649,8 @@
 ) %7B%0A
-        // TODO: FIXME (NullPointerException)%0A
     
@@ -2877,24 +2877,242 @@
 , %22Fritz%22));
+%0A%0A        List%3CUser%3E otherParticipantsList = new LinkedList%3CUser%3E(participantsList);%0A        otherParticipantsList.remove(0);%0A%0A        when(participantStore.getParticipants(event.getId())).thenReturn(participantsList);
 %0A        whe
@@ -3263,36 +3263,104 @@
     
-// When%0A        getActivity(
+when(participantManager.filterOutParticipants(any(List.class))).thenReturn(otherParticipantsList
 );%0A%0A
@@ -3362,33 +3362,33 @@
 t);%0A%0A        // 
-T
+W
 hen%0A        onVi
@@ -3387,188 +3387,110 @@
     
-onView(withText(%22Me%22)).check(matches(isDisplayed()));%0A        onView(withText(%22Hans%22)).check(matches(isDisplayed()));%0A        onView(withText(%22Fritz%22)).check(matches(isDisplayed())
+getActivity();%0A%0A        // Then%0A        verify(participantAdapter, times(2)).setUsers(participantsList
 );%0A 

Edit participants test