package org.jakubczyk.demo.flickrdemo.screens.search;


import org.jakubczyk.demo.flickrdemo.data.api.json.Photo;
import org.jakubczyk.demo.flickrdemo.data.api.json.SearchResponse;
import org.jakubczyk.demo.flickrdemo.data.repository.FlickrRepository;

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

import rx.Observable;
import rx.Scheduler;
import rx.functions.Action1;

public class SearchActivityPresenter implements SearchActivityContract.Presenter {

    // We don't know what is going to be size of the list
    private List<Photo> photos = new LinkedList<>();

    private SearchActivityContract.View view;
    private FlickrRepository flickrRepository;
    private Scheduler mainScheduler;

    public SearchActivityPresenter(
            FlickrRepository flickrRepository,
            Scheduler mainScheduler
    ) {
        this.flickrRepository = flickrRepository;
        this.mainScheduler = mainScheduler;
    }

    @Override
    public void create(SearchActivityContract.View view) {
        this.view = view;

        this.view.showEmpty();
    }

    @Override
    public void destroy() {
        view = null;
    }

    @Override
    public void observeSearch(Observable<CharSequence> charSequenceObservable) {
        charSequenceObservable
                // don't flood with requests
                .debounce(1, TimeUnit.SECONDS)
                .observeOn(mainScheduler)
                .doOnNext(new Action1<CharSequence>() {
                    @Override
                    public void call(CharSequence charSequence) {
                        photos.clear();
                        shouldShowList();
                    }
                })
                .filter(textToSearch -> textToSearch.length() > 0)
                .flatMap(textToSearch -> flickrRepository.searchFlickr(textToSearch.toString()))
                .map(this::processResponse)
                .subscribe(this::handleNewData);
    }

    void shouldShowList() {
        if (photos.size() == 0) {
            view.showEmpty();
        } else {
            view.showList();
            view.refreshList();
        }
    }

    void handleNewData(List<Photo> photoList) {
        photos.addAll(photoList);

        shouldShowList();
    }

    // Adapter
    @Override
    public Photo getItemAtPosition(int position) {
        return photos.get(position);
    }

    @Override
    public int getItemsCount() {
        return photos.size();
    }

    // Pagination

    boolean isLoadingNextPage = false;

    boolean hasLoadedAllItems = true;

    @Override
    public void loadNextPage() {
        isLoadingNextPage = true;

        flickrRepository
                .loadNext()
                .map(this::processResponse)
                .subscribe(this::handleNewData);
    }

    private List<Photo> processResponse(SearchResponse searchResponse) {
        hasLoadedAllItems = searchResponse.photos.totalPages == flickrRepository.getCurrentPage();
        return searchResponse.photos.photoList;
    }

    @Override
    public boolean isLoadingNextPage() {
        return isLoadingNextPage;
    }

    @Override
    public boolean hasLoadedAllItems() {
        return hasLoadedAllItems;
    }
}

@@ -1463,138 +1463,26 @@
 ext(
-new Action1%3CCharSequence%3E() %7B%0A                    @Override%0A                    public void call(CharSequence charSequence) %7B%0A    
+charSequence -%3E %7B%0A
     
@@ -1529,28 +1529,24 @@
             
-    
 shouldShowLi
@@ -1555,30 +1555,8 @@
 ();%0A
-                    %7D%0A
     

wrap to lambda