package ca.six.unittestapp.todo.tasks;

import android.app.Activity;

import com.google.common.collect.Lists;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

import java.util.List;

import ca.six.unittestapp.todo.addedittask.AddEditTaskActivity;
import ca.six.unittestapp.todo.data.Task;
import ca.six.unittestapp.todo.data.source.TasksDataSource;
import ca.six.unittestapp.todo.data.source.TasksRepository;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

/**
 * @copyright six.ca
 * Created by Xiaolin on 2017-04-27.
 */

public class TasksPresenterTest {
    private static TasksRepository mMockTasksRepository;
    private static TasksContract.View mMockTasksView;
    private static TasksPresenter mTasksPresenter;
    private static Task mTestTask;
    private static final List<Task> TASKS
            = Lists.newArrayList(new Task("Title1", "Description1"),
            new Task("Title2", "Description2", true), new Task("Title3", "Description3", true));

    @BeforeClass
    public static void setUp(){
        mMockTasksRepository = mock(TasksRepository.class);
        mMockTasksView = mock(TasksContract.View.class);
        mTasksPresenter = new TasksPresenter(mMockTasksRepository, mMockTasksView);
        mTestTask = new Task("test", "test for sure");
    }

    @Test
    public void cfTest(){
        verify(mMockTasksView).setPresenter(mTasksPresenter);
    }

    @Test
    public void resultTest(){
        mTasksPresenter.result(AddEditTaskActivity.REQUEST_ADD_TASK, Activity.RESULT_OK);
        verify(mMockTasksView).showSuccessfullySavedMessage();
    }

    @Test
    public void loadTasksOnDataNotAvailableTest(){
        ArgumentCaptor<TasksDataSource.LoadTasksCallback> loadTasksCbCaptor
                = ArgumentCaptor.forClass(TasksDataSource.LoadTasksCallback.class);

        mTasksPresenter.loadTasks(true);

        verify(mMockTasksView).setLoadingIndicator(true);
        verify(mMockTasksRepository).refreshTasks();
        verify(mMockTasksRepository).getTasks(loadTasksCbCaptor.capture());
        loadTasksCbCaptor.getValue().onDataNotAvailable();
        if(mMockTasksView.isActive()){
            verify(mMockTasksView).showLoadingTasksError();
        }
    }

    @Test
    public void loadAllTasksTest(){
        ArgumentCaptor<TasksDataSource.LoadTasksCallback> loadCaptor
                = ArgumentCaptor.forClass(TasksDataSource.LoadTasksCallback.class);
        ArgumentCaptor<List> taskListCaptor = ArgumentCaptor.forClass(List.class);

        mTasksPresenter.setFiltering(TasksFilterType.ALL_TASKS);
        mTasksPresenter.loadTasks(true);

//        verify(mMockTasksRepository).refreshTasks();

        verify(mMockTasksView).setLoadingIndicator(true);
        verify(mMockTasksRepository).getTasks(loadCaptor.capture());
        loadCaptor.getValue().onTasksLoaded(TASKS);

        verify(mMockTasksView).setLoadingIndicator(false);

        verify(mMockTasksView).showTasks(taskListCaptor.capture());
        Assert.assertTrue(TASKS.size() == taskListCaptor.getValue().size());
    }

    @Test
    public void addNewTaskTest(){
        mTasksPresenter.addNewTask();
        verify(mMockTasksView).showAddTask();
    }

    @Test
    public void openTaskDetailsTest(){
        mTasksPresenter.openTaskDetails(mTestTask);
        Assert.assertNotNull(mTestTask);
        verify(mMockTasksView).showTaskDetailsUi(mTestTask.getId());
    }

    @Test
    public void completeTaskTest(){
        mTasksPresenter.completeTask(mTestTask);
        Assert.assertNotNull(mTestTask);
        verify(mMockTasksRepository).completeTask(mTestTask);
        verify(mMockTasksView).showTaskMarkedComplete();
    }

    @Test
    public void activateTaskTest(){
        mTasksPresenter.activateTask(mTestTask);
        Assert.assertNotNull(mTestTask);
        verify(mMockTasksRepository).activateTask(mTestTask);
        verify(mMockTasksView).showTaskMarkedActive();
    }

    @Test
    public void clearCompletedTasks(){
        mTasksPresenter.clearCompletedTasks();
        verify(mMockTasksRepository).clearCompletedTasks();
        verify(mMockTasksView).showCompletedTasksCleared();
    }

}

@@ -156,15 +156,10 @@
 fore
-Class
 ;%0A
+
 impo
@@ -545,16 +545,56 @@
 .verify;
+%0Aimport static org.mockito.Mockito.when;
 %0A%0A/**%0A *
@@ -696,31 +696,24 @@
     private 
-static 
 TasksReposit
@@ -746,31 +746,24 @@
     private 
-static 
 TasksContrac
@@ -793,31 +793,24 @@
     private 
-static 
 TasksPresent
@@ -841,23 +841,16 @@
 private 
-static 
 Task mTe
@@ -869,23 +869,16 @@
 private 
-static 
 final Li
@@ -1070,21 +1070,16 @@
  @Before
-Class
 %0A    pub
@@ -1082,23 +1082,16 @@
  public 
-static 
 void set
@@ -1352,16 +1352,74 @@
 sure%22);%0A
+        when(mMockTasksView.isActive()).thenReturn(true);%0A
     %7D%0A%0A 
@@ -2742,64 +2742,8 @@
 );%0A%0A
-//        verify(mMockTasksRepository).refreshTasks();%0A%0A
     
@@ -2788,32 +2788,85 @@
 ndicator(true);%0A
+        verify(mMockTasksRepository).refreshTasks();%0A
         verify(m
@@ -2967,24 +2967,24 @@
 ed(TASKS);%0A%0A
+
         veri
@@ -3026,25 +3026,24 @@
 tor(false);%0A
-%0A
         veri

load all tasks unit test stopped at return.