package it.near.sdk.Recipes;

import android.content.SharedPreferences;

import com.google.common.collect.Maps;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import it.near.sdk.GlobalConfig;
import it.near.sdk.Recipes.Models.Recipe;

import static it.near.sdk.Recipes.RecipesManager.PULSE_ACTION_ID_KEY;
import static it.near.sdk.Recipes.RecipesManager.PULSE_BUNDLE_ID_KEY;
import static it.near.sdk.Recipes.RecipesManager.PULSE_PLUGIN_ID_KEY;
import static junit.framework.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.ArgumentMatchers.notNull;
import static org.mockito.Mockito.when;

/**
 * Created by cattaneostefano on 01/03/2017.
 */
@RunWith(MockitoJUnitRunner.class)
public class RecipesManagerTest {

    @Mock
    GlobalConfig mockGlobalConfig;
    @Mock
    RecipeCooler recipeCooler;

    @Before
    public void setUp() {
        when(mockGlobalConfig.getAppId()).thenReturn("app_id");
        when(mockGlobalConfig.getInstallationId()).thenReturn("installation_id");
        when(mockGlobalConfig.getProfileId()).thenReturn("profile_id");
    }

    @Test
    public void buildCorrectEvaluationBody_noCooldownNoPulse() throws JSONException {
        String actual = RecipesManager.buildEvaluateBody(mockGlobalConfig, recipeCooler, null, null, null);
        JSONObject actualObj = new JSONObject(actual);
        assertThat(actualObj = actualObj.getJSONObject("data"), is(notNullValue()));
        assertThat(actualObj = actualObj.getJSONObject("attributes"), is(notNullValue()));
        assertThat(actualObj.has(PULSE_PLUGIN_ID_KEY), is(false));
        assertThat(actualObj.has(PULSE_ACTION_ID_KEY), is(false));
        assertThat(actualObj.has(PULSE_BUNDLE_ID_KEY), is(false));
        assertThat(actualObj = actualObj.getJSONObject("core"), is(notNullValue()));
        assertThat((String) actualObj.get("profile_id"), is("profile_id"));
        assertThat((String) actualObj.get("installation_id"), is("installation_id"));
        assertThat((String) actualObj.get("app_id"), is("app_id"));
        assertThat(actualObj = actualObj.getJSONObject("cooldown"), is(notNullValue()));
        assertThat(Long.valueOf((Integer)actualObj.get("last_notified_at")), is(0L));
        assertThat(actualObj.getJSONObject("recipes_notified_at").length(), is(0));
    }

    @Test
    public void buildCorrectEvaluationBody_withPulse() throws JSONException {
        String actual = RecipesManager.buildEvaluateBody(mockGlobalConfig, recipeCooler, "plugin", "action", "bundle");
        JSONObject actualObj = new JSONObject(actual);
        assertThat(actualObj = actualObj.getJSONObject("data"), is(notNullValue()));
        assertThat(actualObj = actualObj.getJSONObject("attributes"), is(notNullValue()));
        assertThat((String) actualObj.get(PULSE_PLUGIN_ID_KEY), is("plugin"));
        assertThat((String) actualObj.get(PULSE_ACTION_ID_KEY), is("action"));
        assertThat((String) actualObj.get(PULSE_BUNDLE_ID_KEY), is("bundle"));
    }

    @Test
    public void buildCorrectEvaluationBody_withCooldown() {
        // TODO
    }


}

@@ -27,51 +27,8 @@
 s;%0A%0A
-import android.content.SharedPreferences;%0A%0A
 impo
@@ -62,16 +62,16 @@
 t.Maps;%0A
+
 %0Aimport 
@@ -274,16 +274,39 @@
 unner;%0A%0A
+import java.util.Map;%0A%0A
 import i
@@ -333,50 +333,8 @@
 fig;
-%0Aimport it.near.sdk.Recipes.Models.Recipe;
 %0A%0Aim
@@ -545,48 +545,8 @@
 EY;%0A
-import static junit.framework.Assert.*;%0A
 impo
@@ -735,62 +735,8 @@
 ue;%0A
-import static org.mockito.ArgumentMatchers.anyString;%0A
 impo
@@ -1070,17 +1070,21 @@
 eCooler 
-r
+mockR
 ecipeCoo
@@ -1515,33 +1515,37 @@
 ckGlobalConfig, 
-r
+mockR
 ecipeCooler, nul
@@ -2736,17 +2736,21 @@
 Config, 
-r
+mockR
 ecipeCoo
@@ -3332,32 +3332,1081 @@
 n() 
-%7B%0A        // TODO%0A    %7D%0A
+throws JSONException %7B%0A        Map%3CString, Long%3E logMap = Maps.newHashMap();%0A        logMap.put(%22recipe_id_1%22, 0L);%0A        logMap.put(%22recipe_id_2%22, 1000L);%0A        when(mockRecipeCooler.getRecipeLogMap()).thenReturn(logMap);%0A        String actual = RecipesManager.buildEvaluateBody(mockGlobalConfig, mockRecipeCooler, %22plugin%22, %22action%22, %22bundle%22);%0A        JSONObject actualObj = new JSONObject(actual);%0A        assertThat(actualObj = actualObj.getJSONObject(%22data%22), is(notNullValue()));%0A        assertThat(actualObj = actualObj.getJSONObject(%22attributes%22), is(notNullValue()));%0A        assertThat(actualObj = actualObj.getJSONObject(%22core%22), is(notNullValue()));%0A        assertThat(actualObj = actualObj.getJSONObject(%22cooldown%22), is(notNullValue()));%0A        assertThat(actualObj = actualObj.getJSONObject(%22recipes_notified_at%22), is(notNullValue()));%0A        assertThat(actualObj.length(), is(2));%0A        assertThat(Long.valueOf((Integer)actualObj.get(%22recipe_id_1%22)), is(0L));%0A        assertThat(Long.valueOf((Integer)actualObj.get(%22recipe_id_2%22)), is(1000L));%0A    %7D
 %0A%0A%7D%0A

add test for building evaluation body with cool down information