package com.github.openwebnet.view;

import android.content.Intent;
import android.support.design.widget.NavigationView;

import com.getbase.floatingactionbutton.FloatingActionButton;
import com.github.openwebnet.BuildConfig;
import com.github.openwebnet.R;
import com.github.openwebnet.component.ApplicationComponentTest;
import com.github.openwebnet.component.DaggerApplicationComponentTest;
import com.github.openwebnet.component.Injector;
import com.github.openwebnet.component.module.ApplicationContextModuleTest;
import com.github.openwebnet.component.module.DomoticModuleTest;
import com.github.openwebnet.component.module.RepositoryModuleTest;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import butterknife.Bind;
import butterknife.ButterKnife;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.robolectric.Shadows.shadowOf;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@PrepareForTest({Injector.class})
public class MainActivityTest {

    @Rule
    public PowerMockRule rule = new PowerMockRule();

    @Bind(R.id.floatingActionButtonAddLight)
    FloatingActionButton floatingActionButtonAddLight;

    @Bind(R.id.nav_view)
    NavigationView navigationView;

    MainActivity activity;

    @Before
    public void setup() {
        ApplicationComponentTest applicationComponentTest = DaggerApplicationComponentTest.builder()
            .applicationContextModuleTest(new ApplicationContextModuleTest())
            .domoticModuleTest(new DomoticModuleTest())
            .repositoryModuleTest(new RepositoryModuleTest())
            .build();

        PowerMockito.mockStatic(Injector.class);
        PowerMockito.when(Injector.getApplicationComponent()).thenReturn(applicationComponentTest);

        ((ApplicationComponentTest) Injector.getApplicationComponent()).inject(this);
    }

    private void setupActivity() {
        activity = Robolectric.setupActivity(MainActivity.class);
        ButterKnife.bind(this, activity);
    }

    @Ignore
    @Test
    public void shouldInitNavigationDrawer() {
        setupActivity();
        // TODO
    }

    @Ignore
    @Test
    public void clickingAddLight_shouldStartLightActivity() {
        setupActivity();

        floatingActionButtonAddLight.performClick();
        Intent expectedIntent = new Intent(activity, MainActivity.class);
        assertThat(shadowOf(activity).getNextStartedActivity(), equalTo(expectedIntent));
    }

}

@@ -644,16 +644,239 @@
 uleTest;
+%0Aimport com.github.openwebnet.model.EnvironmentModel;%0Aimport com.github.openwebnet.service.EnvironmentService;%0Aimport com.github.openwebnet.view.device.DeviceActivity;%0Aimport com.github.openwebnet.view.device.LightActivity;
 %0A%0Aimport
@@ -898,33 +898,8 @@
 re;%0A
-import org.junit.Ignore;%0A
 impo
@@ -1340,16 +1340,69 @@
 onfig;%0A%0A
+import java.util.List;%0A%0Aimport javax.inject.Inject;%0A%0A
 import b
@@ -1449,16 +1449,38 @@
 erKnife;
+%0Aimport rx.Observable;
 %0A%0Aimport
@@ -1565,16 +1565,56 @@
 rtThat;%0A
+import static org.mockito.Mockito.when;%0A
 import s
@@ -1814,16 +1814,33 @@
 droid.*%22
+, %22com.getbase.*%22
 %7D)%0A@Prep
@@ -1961,24 +1961,182 @@
 ockRule();%0A%0A
+    @Inject%0A    EnvironmentService environmentService;%0A%0A    @Bind(R.id.floatingActionButtonAddDevice)%0A    FloatingActionButton floatingActionButtonAddDevice;%0A
     @Bind(R.
@@ -2223,17 +2223,16 @@
 dLight;%0A
-%0A
     @Bin
@@ -2940,32 +2940,159 @@
 tupActivity() %7B%0A
+        // mock reloadMenu%0A        when(environmentService.findAll()).thenReturn(Observable.%3CList%3CEnvironmentModel%3E%3Eempty());%0A%0A
         activity
@@ -3186,36 +3186,24 @@
 ty);%0A    %7D%0A%0A
-    @Ignore%0A
     @Test%0A  
@@ -3300,22 +3300,336 @@
 %7D%0A%0A    @
-Ignore
+Test%0A    public void clickingAddLight_shouldStartDeviceActivity() %7B%0A        setupActivity();%0A%0A        floatingActionButtonAddDevice.performClick();%0A        Intent expectedIntent = new Intent(activity, DeviceActivity.class);%0A        assertThat(shadowOf(activity).getNextStartedActivity(), equalTo(expectedIntent));%0A    %7D%0A
 %0A    @Te
@@ -3825,20 +3825,21 @@
 tivity, 
-Main
+Light
 Activity

fix exception mainActivity test