package com.ragdroid.rxify.library;

import android.util.Log;

import com.ragdroid.rxify.core.BaseSchedulerProvider;
import com.ragdroid.rxify.core.data.BookDataSource;
import com.ragdroid.rxify.entity.Book;
import com.ragdroid.rxify.logic.mvp.AbstractPresenter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.ObservableSource;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.processors.ReplayProcessor;
import io.reactivex.subjects.ReplaySubject;

/**
 * Created by garimajain on 08/11/16.
 */

public class LibraryPresenter extends AbstractPresenter<LibraryContract.View> implements LibraryContract.Presenter {

    private static final String TAG = LibraryPresenter.class.getSimpleName();
    private final BookDataSource dataSource;

    ReplaySubject<String> subject = ReplaySubject.create();

    private Disposable disposable;
    private boolean isFixed = false;

    @Inject
    public LibraryPresenter(BookDataSource dataSource,
                            BaseSchedulerProvider provider) {
        super(provider);
        this.dataSource = dataSource;
    }

    @Override
    public void subscribe() {
        if (disposable == null) {
            initDisposable();
        }
    }

    private void initDisposable() {
        if (isFixed) {
            disposable = subject
                    .debounce(300, TimeUnit.MILLISECONDS)
                    .concatMap(new Function<String, ObservableSource<List<Book>>>() {
                        @Override
                        public ObservableSource<List<Book>> apply(String s) throws Exception {
                            Log.d(TAG, "getting books for " + s);
                            return dataSource.getBook(s);
                        }
                    })
                    .subscribeOn(provider.io())
                    .observeOn(provider.ui())
                    .subscribe(new Consumer<List<Book>>() {
                        @Override
                        public void accept(List<Book> books) throws Exception {
                            onBooksFetched(books);
                        }
                    });
        } else {
            disposable = subject
                    .debounce(300, TimeUnit.MILLISECONDS)
                    .flatMap(new Function<String, ObservableSource<List<Book>>>() {
                        @Override
                        public ObservableSource<List<Book>> apply(String s) throws Exception {
                            Log.d(TAG, "getting books for " + s);
                            return dataSource.getBook(s);
                        }
                    })
                    .subscribeOn(provider.io())
                    .observeOn(provider.ui())
                    .subscribe(new Consumer<List<Book>>() {
                        @Override
                        public void accept(List<Book> books) throws Exception {
                            onBooksFetched(books);
                        }
                    });
        }
    }

    private void onBooksFetched(List<Book> books) {
        if (getView() != null) {
            getView().showBooks(books);
        }
    }

    @Override
    public void unsubscribe() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
            disposable = null;
        }
    }

    @Override
    public void onQueryTextChange(final String newText) {
        if (newText == null || newText.length() == 0) {
            onBooksFetched(new ArrayList<Book>());
        } else {
            subject.onNext(newText);
        }

    }

    @Override
    public boolean getState() {
        return isFixed;
    }

    @Override
    public void onFixBugToggleClicked() {
        this.isFixed = !isFixed;
        unsubscribe();
        initDisposable();
    }
}

@@ -616,16 +616,61 @@
 cessor;%0A
+import io.reactivex.subjects.PublishSubject;%0A
 import i
@@ -999,22 +999,23 @@
 e;%0A%0A    
-Replay
+Publish
 Subject%3C
@@ -1032,22 +1032,23 @@
 bject = 
-Replay
+Publish
 Subject.

change to publish subject