package uk.co.ribot.androidboilerplate.data;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.IBinder;

import javax.inject.Inject;

import rx.Observer;
import rx.Subscription;
import rx.schedulers.Schedulers;
import timber.log.Timber;
import uk.co.ribot.androidboilerplate.BoilerplateApplication;
import uk.co.ribot.androidboilerplate.data.model.Ribot;
import uk.co.ribot.androidboilerplate.util.AndroidComponentUtil;
import uk.co.ribot.androidboilerplate.util.NetworkUtil;
import uk.co.ribot.androidboilerplate.util.RxUtil;

public class SyncService extends Service {

    @Inject DataManager mDataManager;
    private Subscription mSubscription;

    public static Intent getStartIntent(Context context) {
        return new Intent(context, SyncService.class);
    }

    public static boolean isRunning(Context context) {
        return AndroidComponentUtil.isServiceRunning(context, SyncService.class);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        BoilerplateApplication.get(this).getComponent().inject(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, final int startId) {
        Timber.i("Starting sync...");

        if (!NetworkUtil.isNetworkConnected(this)) {
            Timber.i("Sync canceled, connection not available");
            AndroidComponentUtil.toggleComponent(this, SyncOnConnectionAvailable.class, true);
            stopSelf(startId);
            return START_NOT_STICKY;
        }

        RxUtil.unsubscribe(mSubscription);
        mSubscription = mDataManager.syncRibots()
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Ribot>() {
                    @Override
                    public void onCompleted() {
                        Timber.i("Synced successfully!");
                        stopSelf(startId);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Timber.w(e, "Error syncing.");
                        stopSelf(startId);

                    }

                    @Override
                    public void onNext(Ribot ribot) {
                    }
                });

        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        if (mSubscription != null) mSubscription.unsubscribe();
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public static class SyncOnConnectionAvailable extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)
                    && NetworkUtil.isNetworkConnected(context)) {
                Timber.i("Connection is now available, triggering sync...");
                AndroidComponentUtil.toggleComponent(context, this.getClass(), false);
                context.startService(getStartIntent(context));
            }
        }
    }

}
@@ -276,17 +276,27 @@
 %0Aimport 
-r
+io.reactive
 x.Observ
@@ -310,33 +310,104 @@
 ort 
-rx.Subscription;%0Aimport r
+io.reactivex.annotations.NonNull;%0Aimport io.reactivex.disposables.Disposable;%0Aimport io.reactive
 x.sc
@@ -842,34 +842,30 @@
 ate 
-Subscription mSubscription
+Disposable mDisposable
 ;%0A%0A 
@@ -1715,59 +1715,37 @@
 til.
-unsubscribe(mSubscription);%0A        mSubscription =
+dispose(mDisposable);%0A       
  mDa
@@ -1932,122 +1932,199 @@
 d on
-Completed() %7B%0A                        Timber.i(%22Synced successfully!%22);%0A                        stopSelf(startId);
+Subscribe(@NonNull Disposable d) %7B%0A                        mDisposable = d;%0A                    %7D%0A%0A                    @Override%0A                    public void onNext(@NonNull Ribot ribot) %7B
 %0A   
@@ -2213,16 +2213,25 @@
 onError(
+@NonNull 
 Throwabl
@@ -2335,17 +2335,16 @@
 artId);%0A
-%0A
         
@@ -2426,27 +2426,121 @@
 d on
-Next(Ribot ribot) %7B
+Complete() %7B%0A                        Timber.i(%22Synced successfully!%22);%0A                        stopSelf(startId);
 %0A   
@@ -2676,54 +2676,46 @@
 f (m
-Subscription != null) mSubscription.unsubscrib
+Disposable != null) mDisposable.dispos
 e();
@@ -3403,8 +3403,9 @@
     %7D%0A%0A%7D
+%0A

Migrate SyncService to RxJava2.