// Path: app/src/main/java/com/calgen/prodek/fadflicks/model/MovieBundle.java
// public class MovieBundle implements Serializable {
// 
//     public Movie movie;
//     public VideoResponse videoResponse;
//     public ReviewResponse reviewResponse;
//     public Credits credits;
//     public MovieDetails movieDetails;
// }


import java.util.List;
import java.util.TimeZone;
import static android.preference.PreferenceManager.getDefaultSharedPreferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.calgen.prodek.fadflicks.R;
import com.calgen.prodek.fadflicks.model.MovieBundle;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
    public static List<MovieBundle> bulkReadMovieData(Context context, List<Integer> movieIds) {
        List<MovieBundle> movieBundleList = new ArrayList<>();
        SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
        String baseKey = context.getResources().getString(R.string.fav_movie_base_key);
        for (Integer id : movieIds) {
            String key = baseKey + id;
            if (sharedPreferences.contains(key)) {
                String jsonStr = sharedPreferences.getString(key, null);
                movieBundleList.add(new Gson().fromJson(jsonStr, MovieBundle.class));
            }
        }
        return movieBundleList;
    }

    /**
     * @param context {@link Context} needed to fetch DefaultSharedPreferences.
     * @param movieId id of movie whose favourite needs to be cached.
     * @param isFavourite favourite value.
     */
    public static void setFavouriteMovie(Context context, int movieId, boolean isFavourite) {
        SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
        Editor sharedPreferencesEditor = sharedPreferences.edit();
        HashMap<Integer, Boolean> map = getFavouriteMovies(context);
        if (map == null) {
            map = new HashMap<>();
        }
        map.put(movieId, isFavourite);
        Gson gson = new Gson();
        String data = gson.toJson(map);
        sharedPreferencesEditor.putString(context.getResources().getString(R.string.fav_movies_prefs_key), data);
// Path: app/src/main/java/com/calgen/prodek/fadflicks/model/MovieBundle.java
// public class MovieBundle implements Serializable {
// 
//     public Movie movie;
//     public VideoResponse videoResponse;
//     public ReviewResponse reviewResponse;
//     public Credits credits;
//     public MovieDetails movieDetails;
// }


// Path: app/src/main/java/com/calgen/prodek/fadflicks/utils/Cache.java
import java.util.List;
import java.util.TimeZone;
import static android.preference.PreferenceManager.getDefaultSharedPreferences;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import com.calgen.prodek.fadflicks.R;
import com.calgen.prodek.fadflicks.model.MovieBundle;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

    public static List<MovieBundle> bulkReadMovieData(Context context, List<Integer> movieIds) {
        List<MovieBundle> movieBundleList = new ArrayList<>();
        SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
        String baseKey = context.getResources().getString(R.string.fav_movie_base_key);
        for (Integer id : movieIds) {
            String key = baseKey + id;
            if (sharedPreferences.contains(key)) {
                String jsonStr = sharedPreferences.getString(key, null);
                movieBundleList.add(new Gson().fromJson(jsonStr, MovieBundle.class));
            }
        }
        return movieBundleList;
    }

    /**
     * @param context {@link Context} needed to fetch DefaultSharedPreferences.
     * @param movieId id of movie whose favourite needs to be cached.
     * @param isFavourite favourite value.
     */
    public static void setFavouriteMovie(Context context, int movieId, boolean isFavourite) {
        SharedPreferences sharedPreferences = getDefaultSharedPreferences(context);
        Editor sharedPreferencesEditor = sharedPreferences.edit();
        HashMap<Integer, Boolean> map = getFavouriteMovies(context);
        if (map == null) {
            map = new HashMap<>();
        }
        map.put(movieId, isFavourite);
        Gson gson = new Gson();
        String data = gson.toJson(map);
        sharedPreferencesEditor.putString(context.getResources().getString(R.string.fav_movies_prefs_key), data);
        sharedPreferencesEditor.commit();