package com.piticlistudio.playednext;

import android.app.Application;
import android.util.Log;

import com.piticlistudio.playednext.collection.CollectionModule;
import com.piticlistudio.playednext.company.model.CompanyModule;
import com.piticlistudio.playednext.di.module.NetModule;
import com.piticlistudio.playednext.game.GameComponent;
import com.piticlistudio.playednext.game.GameModule;
import com.piticlistudio.playednext.game.model.DaggerGamedataComponent;
import com.piticlistudio.playednext.game.model.GamedataComponent;
import com.piticlistudio.playednext.game.model.GamedataModule;
import com.piticlistudio.playednext.game.model.entity.Game;
import com.piticlistudio.playednext.game.model.repository.GameRepository;
import com.piticlistudio.playednext.image.ImageModule;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import io.realm.Realm;
import io.realm.RealmConfiguration;


public class AndroidApplication extends Application {

    public GamedataComponent gamedataComponent;
    public GameComponent gameComponent;

    private static final String TAG = "AndroidApplication";

    @Override
    public void onCreate() {
        super.onCreate();

        RealmConfiguration config = new RealmConfiguration.Builder(this)
                .deleteRealmIfMigrationNeeded()
                .build();
        Realm.setDefaultConfiguration(config);

        initializeComponents();
    }

    private void initializeComponents() {

        gamedataComponent = DaggerGamedataComponent.builder()
                .gamedataModule(new GamedataModule())
                .netModule(new NetModule())
                .build();

        gameComponent = gamedataComponent.plus(new GameModule(), new CollectionModule(), new CompanyModule());

        // TODO: 10/02/2017 remove
        final GameRepository repository = gameComponent.repository();
        repository.load(1000)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<Game>() {
                    @Override
                    public void accept(Game game) throws Exception {
                        Log.d(TAG, "accept() returned: " + game);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Log.e(TAG, "accept: "  + throwable.getLocalizedMessage() );
                    }
                });
    }
}

@@ -724,63 +724,8 @@
 ory;
-%0Aimport com.piticlistudio.playednext.image.ImageModule;
 %0A%0Aim
@@ -975,24 +975,84 @@
 lication %7B%0A%0A
+    private static final String TAG = %22AndroidApplication%22;%0A
     public G
@@ -1132,69 +1132,8 @@
 t;%0A%0A
-    private static final String TAG = %22AndroidApplication%22;%0A%0A
     
@@ -1909,16 +1909,59 @@
 d(1000)%0A
+                .flatMap(repository::save)%0A
         
@@ -2495,17 +2495,16 @@
 ccept: %22
- 
  + throw
@@ -2529,17 +2529,16 @@
 essage()
- 
 );%0A     

Save method is now called when fetching data on AndroidApplication