package com.marverenic.music.data.store;

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

import com.jakewharton.rxrelay.BehaviorRelay;
import com.marverenic.music.R;
import com.marverenic.music.instances.AutoPlaylist;
import com.marverenic.music.instances.Playlist;
import com.marverenic.music.instances.Song;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import rx.Observable;

public class LocalPlaylistStore implements PlaylistStore {

    private Context mContext;
    private BehaviorRelay<List<Playlist>> mPlaylists;

    public LocalPlaylistStore(Context context) {
        mContext = context;
    }

    @Override
    public Observable<List<Playlist>> getPlaylists() {
        if (mPlaylists == null) {
            mPlaylists = BehaviorRelay.create();

            MediaStoreUtil.getPermission(mContext).map(granted -> {
                if (granted) {
                    return MediaStoreUtil.getAllPlaylists(mContext);
                } else {
                    return Collections.<Playlist>emptyList();
                }
            }).subscribe(mPlaylists);
        }
        return mPlaylists;
    }

    @Override
    public Observable<List<Song>> getSongs(Playlist playlist) {
        return Observable.just(MediaStoreUtil.getPlaylistSongs(mContext, playlist));
    }

    @Override
    public Observable<List<Playlist>> searchForPlaylists(String query) {
        return Observable.just(MediaStoreUtil.searchForPlaylists(mContext, query));
    }

    @Override
    public String verifyPlaylistName(String playlistName) {
        if (playlistName == null || playlistName.trim().isEmpty()) {
            return mContext.getString(R.string.error_hint_empty_playlist);
        }

        if (MediaStoreUtil.findPlaylistByName(mContext, playlistName) != null) {
            return mContext.getString(R.string.error_hint_duplicate_playlist);
        }

        return null;
    }

    @Override
    public Playlist makePlaylist(String name) {
        return makePlaylist(name, null);
    }

    @Override
    public AutoPlaylist makePlaylist(AutoPlaylist model) {
        // TODO implement AutoPlaylists
        return null;
    }

    @Override
    public Playlist makePlaylist(String name, @Nullable List<Song> songs) {
        Playlist created = MediaStoreUtil.createPlaylist(mContext, name, songs);

        if (mPlaylists != null && mPlaylists.getValue() != null) {
            List<Playlist> updated = new ArrayList<>(mPlaylists.getValue());
            updated.add(created);
            Collections.sort(updated);

            mPlaylists.call(updated);
        }

        return created;
    }

    @Override
    public void removePlaylist(Playlist playlist) {
        MediaStoreUtil.deletePlaylist(mContext, playlist);

        if (mPlaylists != null && mPlaylists.getValue() != null) {
            List<Playlist> updated = new ArrayList<>(mPlaylists.getValue());
            updated.remove(playlist);

            mPlaylists.call(updated);
        }
    }

    @Override
    public void editPlaylist(Playlist playlist, List<Song> newSongs) {
        MediaStoreUtil.editPlaylist(mContext, playlist, newSongs);
    }

    @Override
    public void editPlaylist(AutoPlaylist replacementModel) {
        // TODO implement AutoPlaylists
    }

    @Override
    public void addToPlaylist(Playlist playlist, Song song) {
        MediaStoreUtil.appendToPlaylist(mContext, playlist, song);
    }

    @Override
    public void addToPlaylist(Playlist playlist, List<Song> songs) {
        MediaStoreUtil.appendToPlaylist(mContext, playlist, songs);
    }
}

@@ -116,54 +116,8 @@
 e;%0A%0A
-import com.jakewharton.rxrelay.BehaviorRelay;%0A
 impo
@@ -391,16 +391,52 @@
 ervable;
+%0Aimport rx.subjects.BehaviorSubject;
 %0A%0Apublic
@@ -539,21 +539,23 @@
 Behavior
-Relay
+Subject
 %3CList%3CPl
@@ -800,13 +800,15 @@
 vior
-Relay
+Subject
 .cre
@@ -870,11 +870,17 @@
 xt).
-map
+subscribe
 (gra
@@ -944,30 +944,26 @@
     
-return MediaStoreUtil.
+mPlaylists.onNext(
 getA
@@ -974,24 +974,17 @@
 aylists(
-mContext
+)
 );%0A     
@@ -1023,23 +1023,34 @@
         
-return 
+mPlaylists.onNext(
 Collecti
@@ -1053,26 +1053,16 @@
 ections.
-%3CPlaylist%3E
 emptyLis
@@ -1064,16 +1064,17 @@
 tyList()
+)
 ;%0A      
@@ -1099,27 +1099,43 @@
       %7D)
-.subscribe(
+;%0A        %7D%0A        return 
 mPlaylis
@@ -1140,46 +1140,119 @@
 ists
-)
 ;%0A    
+%7D%0A%0A
     
-%7D%0A        return mPlaylists
+private List%3CPlaylist%3E getAllPlaylists() %7B%0A        return MediaStoreUtil.getAllPlaylists(mContext)
 ;%0A  
@@ -2688,36 +2688,38 @@
      mPlaylists.
-call
+onNext
 (updated);%0A     
@@ -3091,12 +3091,14 @@
 sts.
-call
+onNext
 (upd

Use a BehaviorSubject instead of a BehaviorRelay