package com.episode6.hackit.typed.preferences;

import android.content.Context;
import com.episode6.hackit.mockspresso.Mockspresso;
import com.episode6.hackit.typed.core.util.Supplier;
import com.episode6.hackit.typed.testing.Rules;
import com.google.gson.Gson;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.MockPolicy;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

/**
 * Tests {@link TypedPrefs.wrap}
 */
@MockPolicy(SharedTestResources.MockPolicy.class)
public class TypedPrefsWrapTest {

  final SharedTestResources t = new SharedTestResources();
  @Rule public final Mockspresso.Rule mockspresso = Rules.mockspressoBuilder()
      .testResources(t)
      .buildRule();

  private static final PrefKey<Integer> INT_PREF = PrefNamespace.ROOT.key(Integer.class)
      .named("testInt")
      .buildWithDefault(3);
  private static final OptPrefKey<TestClass> CLASS_PREF = PrefNamespace.ROOT
      .key(TestClass.class)
      .named("simpleNullTestPref")
      .buildOptional();

  @Mock Context mContext;
  @Mock Supplier<Gson> mGsonSupplier;

  @Before
  public void setup() {
    when(mContext.getSharedPreferences(anyString(), anyInt())).thenReturn(t.mSharedPreferences);
    when(mGsonSupplier.get()).thenReturn(t.mGson);
  }

  @Test
  public void testSimpleWrap() {
    TypedPrefs prefs = TypedPrefs.wrap.sharedPrefs(t.mSharedPreferences);
    int value = prefs.get(INT_PREF);

    t.verifyPrefDidntExist(INT_PREF);
    assertThat(value).isEqualTo(3);
  }

  @Test
  public void testWrapWithGson() {
    TestClass expected = new TestClass();
    t.setupPrefExists(CLASS_PREF, expected);

    TypedPrefs prefs = TypedPrefs.wrap.sharedPrefs(t.mSharedPreferences, mGsonSupplier);
    TestClass result = prefs.get(CLASS_PREF);

    t.verifyPrefExisted(CLASS_PREF);
    assertThat(result).isEqualTo(expected);
  }

  static class TestClass {}
}

@@ -73,16 +73,61 @@
 ontext;%0A
+import android.preference.PreferenceManager;%0A
 import c
@@ -457,16 +457,82 @@
 kPolicy;
+%0Aimport org.powermock.core.classloader.annotations.PrepareForTest;
 %0A%0Aimport
@@ -616,85 +616,85 @@
 ito.
-ArgumentMatchers.anyInt;%0Aimport static org.mockito.ArgumentMatchers.anyString
+Mockito.when;%0Aimport static org.powermock.api.mockito.PowerMockito.mockStatic
 ;%0Aim
@@ -705,32 +705,46 @@
  static org.
+powermock.api.
 mockito.
 Mockito.when
@@ -727,32 +727,37 @@
 api.mockito.
+Power
 Mockito.
 when;%0A%0A/**%0A 
@@ -744,20 +744,28 @@
 Mockito.
-when
+verifyStatic
 ;%0A%0A/**%0A 
@@ -850,16 +850,57 @@
 .class)%0A
+@PrepareForTest(PreferenceManager.class)%0A
 public c
@@ -1522,149 +1522,504 @@
     
-when(mContext.getSharedPreferences(anyString(), anyInt())).thenReturn(t.mSharedPreferences);%0A    when(mGsonSupplier.get()).thenReturn(t.mGson
+mockStatic(PreferenceManager.class);%0A    when(PreferenceManager.getDefaultSharedPreferences(mContext)).thenReturn(t.mSharedPreferences);%0A    when(mGsonSupplier.get()).thenReturn(t.mGson);%0A  %7D%0A%0A  @Test%0A  public void testDefaultWrap() %7B%0A    TypedPrefs prefs = TypedPrefs.wrap.defaultSharedPrefs(mContext);%0A    int value = prefs.get(INT_PREF);%0A%0A%0A    verifyStatic();%0A    PreferenceManager.getDefaultSharedPreferences(mContext);%0A%0A    t.verifyPrefDidntExist(INT_PREF);%0A    assertThat(value).isEqualTo(3
 );%0A 

add test for TypedPrefs.wrap.defaultSharedPrefs()