package com.example.profbola.bakingtime.services;

import android.content.Context;
import android.support.annotation.NonNull;

import com.example.profbola.bakingtime.models.Ingredient;
import com.example.profbola.bakingtime.models.Recipe;
import com.example.profbola.bakingtime.models.Step;
import com.example.profbola.bakingtime.provider.RecipeContract;
import com.example.profbola.bakingtime.utils.RecipeNetworkUtil;
import com.example.profbola.bakingtime.utils.RecipeParser;

import org.json.JSONArray;

import java.util.List;

import static com.example.profbola.bakingtime.provider.RecipeContract.RecipeEntry.CONTENT_URI;

/**
 * Created by prof.BOLA on 7/5/2017.
 */

public class RecipeTasks {

    synchronized static void writeRecipes(@NonNull Context context) {

        JSONArray jsonArray = RecipeNetworkUtil.getRecipeFromApi();
        List<Recipe> recipeParser = RecipeParser.parse(jsonArray);

        for (Recipe recipe: recipeParser) {
            insertRecipe(context, recipe);
            insertIngredients(context, recipe);
            insertSteps(context, recipe);
        }

        RecipeService.startActionUpdateRecipeWidget(context);

    }

    private static void insertSteps(Context context, Recipe recipe) {
        int id = recipe.id;
        for (Step step: recipe.steps) {
            context.getContentResolver().insert(
                    RecipeContract.StepEntry.CONTENT_URI(id),
                    step.toContentValues(id)
            );
        }
    }

    private static void insertIngredients(Context context, Recipe recipe) {
        int id = recipe.id;
        for (Ingredient ingredient: recipe.ingredients) {
            context.getContentResolver().insert(
                    RecipeContract.IngredientEntry.CONTENT_URI(id),
                    ingredient.toContentValues(id)
            );
        }
    }

    private static void insertRecipe(Context context, Recipe recipe) {
        context.getContentResolver().insert(
                CONTENT_URI,
                recipe.toContentValues()
        );
    }
}

@@ -44,16 +44,54 @@
 vices;%0A%0A
+import android.content.ContentValues;%0A
 import a
@@ -538,16 +538,44 @@
 Array;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -965,16 +965,97 @@
 nArray);
+%0A        ContentValues%5B%5D recipesValues;%0A        ContentValues%5B%5D ingredientValues;
 %0A%0A      
@@ -1139,98 +1139,8 @@
 e);%0A
-            insertIngredients(context, recipe);%0A            insertSteps(context, recipe);%0A
     
@@ -1142,24 +1142,26 @@
 %0A        %7D%0A%0A
+//
         Reci
@@ -2099,20 +2099,102 @@
 %0A        );%0A
+        insertIngredients(context, recipe);%0A        insertSteps(context, recipe);%0A
     %7D%0A%7D%0A

modify handleActionGetAssociatedData to capture RecipeDetailActivity logic