package com.wassim.androidmvpbase.data;

import com.wassim.androidmvpbase.data.local.database.DatabaseHelper;
import com.wassim.androidmvpbase.data.local.preferences.PreferencesHelper;
import com.wassim.androidmvpbase.data.model.Movie;
import com.wassim.androidmvpbase.data.remote.ApiService;
import com.wassim.androidmvpbase.util.EventPosterHelper;

import java.util.List;

import javax.inject.Inject;

import rx.Observable;

public class DataManager {

    private final ApiService mApiService;
    private final DatabaseHelper mDatabaseHelper;
    private final PreferencesHelper mPreferencesHelper;
    private final EventPosterHelper mEventPoster;


    @Inject
    public DataManager(ApiService apiService, PreferencesHelper preferencesHelper,
                       DatabaseHelper databaseHelper, EventPosterHelper eventPosterHelper) {
        mApiService = apiService;
        mPreferencesHelper = preferencesHelper;
        mDatabaseHelper = databaseHelper;
        mEventPoster = eventPosterHelper;
    }

    public PreferencesHelper getPreferencesHelper() {
        return mPreferencesHelper;
    }

    public ApiService getApiService() {
        return mApiService;
    }

    public DatabaseHelper getDatabaseHelper() {
        return mDatabaseHelper;
    }

    public EventPosterHelper getEventPoster() {
        return mEventPoster;
    }

    public boolean verifyMovie(int id){
        return getDatabaseHelper().findMovie(id) == null ? false : true;
    }


    public void removeMovie(Movie movie){
         getDatabaseHelper().removeMovie(movie);
    }


    public void addMovie(Movie movie){
        getDatabaseHelper().addMovie(movie);
    }


    public Observable<List<Movie>> getMovies() {
        return getApiService().getMovies();
    }

}

@@ -418,16 +418,43 @@
 ervable;
+%0Aimport rx.functions.Func1;
 %0A%0Apublic
@@ -1682,32 +1682,395 @@
 movie);%0A    %7D%0A%0A%0A
+    public Observable%3CMovie%3E getAndSaveMovies() %7B%0A        return getApiService().getMovies().concatMap(new Func1%3CList%3CMovie%3E, Observable%3CMovie%3E%3E() %7B%0A            @Override%0A            public Observable%3CMovie%3E call(List%3CMovie%3E movies) %7B%0A                // save in database and return%0A                return Observable.from(movies);%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     public Obser
@@ -2091,33 +2091,32 @@
 ie%3E%3E getMovies()
- 
 %7B%0A        return
@@ -2143,16 +2143,27 @@
 Movies()
+.distinct()
 ;%0A    %7D%0A

add method getAndSaveMovies() for further use in sync process