package io.mokshjn.cosmo.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.media.MediaMetadataCompat;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;

/**
 * Created by moksh on 31/1/17.
 */


public class StorageUtils {

    private final static String STORAGE = " io.mokshjn.cosmo.STORAGE";
    private SharedPreferences preferences;
    private String AUDIO_LIST = "io.mokshjn.cosmo.AUDIO_LIST";
    private Context context;

    public StorageUtils(Context context) {
        this.context = context;
    }

    public static void storeMediaID(Context context, String mediaID) {
        SharedPreferences preferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("mediaID", mediaID);
        editor.apply();
    }

    public static String getMediaID(Context context) {
        String mediaID;
        SharedPreferences preferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
        mediaID = preferences.getString("mediaID", "null");
        return mediaID;
    }

    public void storeSong(ArrayList<MediaMetadataCompat> arrayList) {
        preferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        Gson gson = new Gson();
        String json = gson.toJson(arrayList);
        editor.putString(AUDIO_LIST, json);
        editor.apply();
    }

    public ArrayList<MediaMetadataCompat> loadSongs() {
        preferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
        String json = preferences.getString("audioArrayList", null);
        Type type = new TypeToken<ArrayList<MediaMetadataCompat>>() {
        }.getType();
        Gson gson = new Gson();
        return gson.fromJson(json, type);
    }

    public void storePosition(int position) {
        preferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt("audioPosition", position);
        editor.apply();
    }

    public int loadPosition() {
        preferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
        return preferences.getInt("audioPosition", -1);//return -1 if no data found
    }

    public void clearCachedAudioPlaylist() {
        preferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.commit();
    }
}
@@ -100,16 +100,68 @@
 rences;%0A
+import android.support.v4.media.MediaBrowserCompat;%0A
 import a
@@ -205,16 +205,16 @@
 Compat;%0A
-
 %0Aimport 
@@ -586,16 +586,85 @@
 _LIST%22;%0A
+    private String CURRENT_QUEUE = %22io.mokshjn.cosmo.CURRENT_QUEUE%22;%0A
     priv
@@ -1905,24 +1905,18 @@
 ing(
-%22audioArrayList%22
+AUDIO_LIST
 , nu
@@ -2590,33 +2590,79 @@
 oid 
-clearCachedAudioPlaylist(
+storeCurrentPlayingQueue(ArrayList%3CMediaBrowserCompat.MediaItem%3E tracks
 ) %7B%0A
@@ -2818,46 +2818,542 @@
     
-editor.clear();%0A        editor.commit(
+Gson gson = new Gson();%0A        String json = gson.toJson(tracks);%0A        editor.putString(CURRENT_QUEUE, json);%0A        editor.apply();%0A    %7D%0A%0A    public ArrayList%3CMediaBrowserCompat.MediaItem%3E getCurrentPlayingQueue() %7B%0A        preferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);%0A        String json = preferences.getString(CURRENT_QUEUE, null);%0A        Type type = new TypeToken%3CArrayList%3CMediaMetadataCompat%3E%3E() %7B%0A        %7D.getType();%0A        Gson gson = new Gson();%0A        return gson.fromJson(json, type
 );%0A 

implement saving current queue