package fr.ydelouis.selfoss.adapter;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.support.v13.app.FragmentPagerAdapter;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.UiThread;

import java.util.ArrayList;
import java.util.List;

import fr.ydelouis.selfoss.entity.Article;
import fr.ydelouis.selfoss.entity.Filter;
import fr.ydelouis.selfoss.fragment.ArticleFragment_;
import fr.ydelouis.selfoss.model.ArticleProvider;

@EBean
public class ArticlePagerAdapter extends FragmentPagerAdapter implements ArticleProvider.Listener {

	@Bean
	protected ArticleProvider provider;

	private List<Article> articles = new ArrayList<Article>();

	public ArticlePagerAdapter(Context context) {
		super(((Activity) context).getFragmentManager());
	}

	@AfterInject
	protected void init() {
		provider.setListener(this);
	}

	public void setFilter(Filter filter) {
		provider.setFilter(filter);
	}

	public void setArticle(Article article) {
		articles.clear();
		articles.add(article);
		provider.loadNew(article);
	}

	@Override
	public void onNewLoaded(List<Article> newArticles) {
		articles.addAll(0, newArticles);
	}

	public int getPosition(Article article) {
		return articles.indexOf(article);
	}

	public Article getArticle(int position) {
		return articles.get(position);
	}

	@Override
	public Fragment getItem(int position) {
		if (isNewPageNeeded(position)) {
			loadNextArticles();
		}
		return ArticleFragment_.builder().article(getArticle(position)).build();
	}

	private boolean isNewPageNeeded(int position) {
		return position == getCount()-1;
	}

	@Background
	protected void loadNextArticles() {
		int count = getCount();
		Article lastArticle = null;
		if (count > 0) {
			lastArticle = getArticle(count - 1);
		}
		provider.loadNext(count, lastArticle);
	}

	@Override
	public int getCount() {
		return articles.size();
	}

	@Override
	@UiThread
	public void onNextLoaded(List<Article> nextArticles) {
		for(Article article : nextArticles) {
			if(!articles.contains(article))
				articles.add(article);
		}
		if (!nextArticles.isEmpty()) {
			notifyDataSetChanged();
		}
	}
}

@@ -1322,24 +1322,54 @@
 Articles) %7B%0A
+%09%09if (newArticles != null) %7B%0A%09
 %09%09articles.a
@@ -1387,24 +1387,28 @@
 wArticles);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09public 
@@ -2205,13 +2205,45 @@
 ) %7B%0A
+%09%09if (nextArticles != null) %7B%0A%09
 %09%09for
+ 
 (Art
@@ -2277,13 +2277,15 @@
 ) %7B%0A
+%09
 %09%09%09if
+ 
 (!ar
@@ -2309,21 +2309,24 @@
 rticle))
+ %7B
 %0A%09%09%09%09
+%09
 articles
@@ -2340,20 +2340,28 @@
 ticle);%0A
+%09%09
 %09%09%7D%0A
+%09%09%09%7D%0A%09
 %09%09if (!n
@@ -2388,16 +2388,17 @@
 )) %7B%0A%09%09%09
+%09
 notifyDa
@@ -2409,21 +2409,26 @@
 tChanged();%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%7D%0A

Fix NPE in ArticlePagerAdapter