package uk.co.ribot.androidboilerplate.data.local;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.squareup.sqlbrite.BriteDatabase;
import com.squareup.sqlbrite.SqlBrite;

import java.util.Collection;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import hu.akarnokd.rxjava.interop.RxJavaInterop;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import uk.co.ribot.androidboilerplate.data.model.Ribot;

@Singleton
public class DatabaseHelper {

    private final BriteDatabase mDb;

    @Inject
    public DatabaseHelper(DbOpenHelper dbOpenHelper) {
        SqlBrite.Builder briteBuilder = new SqlBrite.Builder();
        mDb = briteBuilder.build().wrapDatabaseHelper(dbOpenHelper, Schedulers.immediate());
    }

    public BriteDatabase getBriteDb() {
        return mDb;
    }

    public Observable<Ribot> setRibots(final Collection<Ribot> newRibots) {
        return Observable.create(new ObservableOnSubscribe<Ribot>() {
            @Override
            public void subscribe(ObservableEmitter<Ribot> emiter) throws Exception {
                if (emiter.isDisposed()) return;
                BriteDatabase.Transaction transaction = mDb.newTransaction();
                try {
                    mDb.delete(Db.RibotProfileTable.TABLE_NAME, null);
                    for (Ribot ribot : newRibots) {
                        long result = mDb.insert(Db.RibotProfileTable.TABLE_NAME,
                                Db.RibotProfileTable.toContentValues(ribot.profile()),
                                SQLiteDatabase.CONFLICT_REPLACE);
                        if (result >= 0) emiter.onNext(ribot);
                    }
                    transaction.markSuccessful();
                    emiter.onComplete();
                } finally {
                    transaction.end();
                }
            }
        });
    }

    public Observable<List<Ribot>> getRibots() {
        rx.Observable<List<Ribot>> v1Observable = mDb.createQuery(Db.RibotProfileTable.TABLE_NAME,
                "SELECT * FROM " + Db.RibotProfileTable.TABLE_NAME)
                .mapToList(new Func1<Cursor, Ribot>() {
                    @Override
                    public Ribot call(Cursor cursor) {
                        return Ribot.create(Db.RibotProfileTable.parseCursor(cursor));
                    }
                });

        return RxJavaInterop.toV2Observable(v1Observable);
    }

}

@@ -153,16 +153,17 @@
 sqlbrite
+2
 .BriteDa
@@ -198,16 +198,17 @@
 sqlbrite
+2
 .SqlBrit
@@ -328,57 +328,8 @@
 n;%0A%0A
-import hu.akarnokd.rxjava.interop.RxJavaInterop;%0A
 impo
@@ -445,17 +445,68 @@
 %0Aimport 
-r
+io.reactivex.annotations.NonNull;%0Aimport io.reactive
 x.functi
@@ -513,17 +513,20 @@
 ons.Func
-1
+tion
 ;%0Aimport
@@ -526,17 +526,27 @@
 %0Aimport 
-r
+io.reactive
 x.schedu
@@ -914,16 +914,9 @@
 rs.i
-mmediate
+o
 ());
@@ -2107,48 +2107,13 @@
    r
-x.Observable%3CList%3CRibot%3E%3E v1Observable =
+eturn
  mDb
@@ -2261,17 +2261,20 @@
 new Func
-1
+tion
 %3CCursor,
@@ -2352,27 +2352,54 @@
 bot 
-call(Cursor cursor)
+apply(@NonNull Cursor cursor) throws Exception
  %7B%0A 
@@ -2529,68 +2529,8 @@
  %7D);
-%0A%0A        return RxJavaInterop.toV2Observable(v1Observable);
 %0A   

Migrate DatabaseHelper to RxJava2.