package com.novoda.simplechromecustomtabs.provider;

import android.support.annotation.NonNull;
import android.support.annotation.WorkerThread;
import android.text.TextUtils;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SimpleChromeCustomTabsAvailableAppProvider implements AvailableAppProvider {

    private final Observable<String> observable;
    private final BestPackageFinder bestPackageFinder;

    SimpleChromeCustomTabsAvailableAppProvider(Observable<String> observable, BestPackageFinder bestPackageFinder) {
        this.observable = observable;
        this.bestPackageFinder = bestPackageFinder;
    }

    public static SimpleChromeCustomTabsAvailableAppProvider newInstance() {
        BestPackageFinder bestPackageFinder = BestPackageFinder.newInstance();
        return new SimpleChromeCustomTabsAvailableAppProvider(createObservable(), bestPackageFinder);
    }

    private static Observable<String> createObservable() {
        return Observable.<String>empty()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io());
    }

    @Override
    @WorkerThread
    public void findBestPackage(@NonNull PackageFoundCallback packageFoundCallback) {
        observable.mergeWith(findBestPackageObservable())
                .subscribe(findBestPackageSubscriber(packageFoundCallback));
    }

    private Observable<String> findBestPackageObservable() {
        return Observable.create(
                new Observable.OnSubscribe<String>() {
                    @Override
                    public void call(Subscriber<? super String> subscriber) {
                        String packageName = bestPackageFinder.findBestPackage();
                        subscriber.onNext(packageName);
                        subscriber.onCompleted();
                    }
                }
        );
    }

    private Subscriber<String> findBestPackageSubscriber(final PackageFoundCallback packageFoundCallback) {
        return new Subscriber<String>() {
            @Override
            public void onCompleted() {
                //no-op
            }

            @Override
            public void onError(Throwable e) {
                packageFoundCallback.onPackageNotFound();
            }

            @Override
            public void onNext(String packageName) {
                if (TextUtils.isEmpty(packageName)) {
                    packageFoundCallback.onPackageNotFound();
                    return;
                }

                packageFoundCallback.onPackageFound(packageName);
            }
        };
    }

    public interface PackageFoundCallback {
        void onPackageFound(String packageName);

        void onPackageNotFound();
    }

}

@@ -2552,39 +2552,21 @@
     
-    return;%0A                %7D%0A%0A
+%7D else %7B%0A    
     
@@ -2619,32 +2619,50 @@
 d(packageName);%0A
+                %7D%0A
             %7D%0A  

Refactor SimpleChromeCustomTabsAvailableAppProvider.findBestPackageSubscriber to include if/else onNext()