package com.kickstarter.viewmodels;

import com.kickstarter.KSRobolectricTestCase;
import com.kickstarter.factories.UserFactory;
import com.kickstarter.libs.CurrentUserType;
import com.kickstarter.libs.Environment;
import com.kickstarter.libs.MockCurrentUser;
import com.kickstarter.models.User;

import org.junit.Test;

import rx.observers.TestSubscriber;

public final class SettingsViewModelTest extends KSRobolectricTestCase {

  @Test
  public void testSettingsViewModel_sendHappeningNewsletter() {
    final User user = UserFactory.user().toBuilder().happeningNewsletter(false).build();
    final CurrentUserType currentUser = new MockCurrentUser(user);
    final Environment environment = environment().toBuilder()
      .currentUser(currentUser)
      .build();

    final SettingsViewModel vm = new SettingsViewModel(environment);

    final TestSubscriber<User> currentUserTest = new TestSubscriber<>();
    currentUser.observable().subscribe(currentUserTest);

    currentUserTest.assertValues(user);
    koalaTest.assertValues("Settings View");

    vm.inputs.sendHappeningNewsletter(true);
    koalaTest.assertValues("Settings View", "Newsletter Subscribe");
    currentUserTest.assertValues(user, user.toBuilder().happeningNewsletter(true).build());

    vm.inputs.sendHappeningNewsletter(false);
    koalaTest.assertValues("Settings View", "Newsletter Subscribe", "Newsletter Unsubscribe");
    currentUserTest.assertValues(user, user.toBuilder().happeningNewsletter(true).build(), user);
  }

  @Test
  public void testSettingsViewModel_sendPromoNewsletter() {
    final User user = UserFactory.user().toBuilder().promoNewsletter(false).build();
    final CurrentUserType currentUser = new MockCurrentUser(user);
    final Environment environment = environment().toBuilder()
      .currentUser(currentUser)
      .build();

    final SettingsViewModel vm = new SettingsViewModel(environment);

    final TestSubscriber<User> currentUserTest = new TestSubscriber<>();
    currentUser.observable().subscribe(currentUserTest);

    currentUserTest.assertValues(user);
    koalaTest.assertValues("Settings View");

    vm.inputs.sendPromoNewsletter(true);
    koalaTest.assertValues("Settings View", "Newsletter Subscribe");
    currentUserTest.assertValues(user, user.toBuilder().promoNewsletter(true).build());

    vm.inputs.sendPromoNewsletter(false);
    koalaTest.assertValues("Settings View", "Newsletter Subscribe", "Newsletter Unsubscribe");
    currentUserTest.assertValues(user, user.toBuilder().promoNewsletter(true).build(), user);
  }

  @Test
  public void testSettingsViewModel_sendWeeklyNewsletter() {
    final User user = UserFactory.user().toBuilder().weeklyNewsletter(false).build();
    final CurrentUserType currentUser = new MockCurrentUser(user);
    final Environment environment = environment().toBuilder()
      .currentUser(currentUser)
      .build();

    final SettingsViewModel vm = new SettingsViewModel(environment);

    final TestSubscriber<User> currentUserTest = new TestSubscriber<>();
    currentUser.observable().subscribe(currentUserTest);

    currentUserTest.assertValues(user);
    koalaTest.assertValues("Settings View");

    vm.inputs.sendWeeklyNewsletter(true);
    koalaTest.assertValues("Settings View", "Newsletter Subscribe");
    currentUserTest.assertValues(user, user.toBuilder().weeklyNewsletter(true).build());

    vm.inputs.sendWeeklyNewsletter(false);
    koalaTest.assertValues("Settings View", "Newsletter Subscribe", "Newsletter Unsubscribe");
    currentUserTest.assertValues(user, user.toBuilder().weeklyNewsletter(true).build(), user);
  }
}

@@ -288,16 +288,59 @@
 ls.User;
+%0Aimport com.kickstarter.ui.data.Newsletter;
 %0A%0Aimport
@@ -1000,32 +1000,206 @@
 rentUserTest);%0A%0A
+    final TestSubscriber%3CNewsletter%3E sendNewsletterConfirmationTest = new TestSubscriber%3C%3E();%0A    vm.sendNewsletterConfirmation().subscribe(sendNewsletterConfirmationTest);%0A%0A
     currentUserT
@@ -1705,32 +1705,86 @@
 .build(), user);
+%0A%0A    sendNewsletterConfirmationTest.assertNoValues();
 %0A  %7D%0A%0A  @Test%0A  
@@ -2297,32 +2297,206 @@
 rentUserTest);%0A%0A
+    final TestSubscriber%3CNewsletter%3E sendNewsletterConfirmationTest = new TestSubscriber%3C%3E();%0A    vm.sendNewsletterConfirmation().subscribe(sendNewsletterConfirmationTest);%0A%0A
     currentUserT
@@ -2990,24 +2990,78 @@
 ld(), user);
+%0A%0A    sendNewsletterConfirmationTest.assertNoValues();
 %0A  %7D%0A%0A  @Tes
@@ -3580,32 +3580,206 @@
 rentUserTest);%0A%0A
+    final TestSubscriber%3CNewsletter%3E sendNewsletterConfirmationTest = new TestSubscriber%3C%3E();%0A    vm.sendNewsletterConfirmation().subscribe(sendNewsletterConfirmationTest);%0A%0A
     currentUserT
@@ -4179,32 +4179,32 @@
  Unsubscribe%22);%0A
-
     currentUserT
@@ -4281,15 +4281,69 @@
 , user);
+%0A%0A    sendNewsletterConfirmationTest.assertNoValues();
 %0A  %7D%0A%7D%0A

Test that confirmation doesn't emit with no German locale