package fr.ydelouis.selfoss.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.BitmapAjaxCallback;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.ViewById;

import fr.ydelouis.selfoss.R;
import fr.ydelouis.selfoss.adapter.ArticlePagerAdapter;
import fr.ydelouis.selfoss.entity.Article;
import fr.ydelouis.selfoss.entity.ArticleType;
import fr.ydelouis.selfoss.entity.Tag;
import fr.ydelouis.selfoss.util.SelfossUtil;

@EActivity(R.layout.activity_article)
public class ArticleActivity extends Activity implements ViewPager.OnPageChangeListener {

	@Extra protected Article article;
	@Extra protected ArticleType type;
	@Extra protected Tag tag;

	@Bean protected SelfossUtil util;
	@Bean ArticlePagerAdapter adapter;

	@ViewById protected ViewPager pager;

	@AfterViews
	protected void initViews() {
		getActionBar().setDisplayHomeAsUpEnabled(true);
		adapter.setTypeAndTag(type, tag);
		adapter.setArticle(article);
		setArticle(article);
		pager.setAdapter(adapter);
		pager.setOnPageChangeListener(this);
		pager.setCurrentItem(adapter.getPosition(article));
	}

	private void setArticle(Article article) {
		setTitle(article.getSourceTitle());
		if (article.hasIcon()) {
			new AQuery(this).image(util.faviconUrl(article), true, true, 0, 0, new BitmapAjaxCallback() {
				@Override
				protected void callback(String url, ImageView iv, Bitmap bm, AjaxStatus status) {
					if (bm != null) {
						getActionBar().setIcon(new BitmapDrawable(getResources(), bm));
					}
				}
			});
		}
	}

	@Override
	@OptionsItem(android.R.id.home)
	public void finish() {
		super.finish();
	}

	@Override
	public void onPageSelected(int position) {
		setArticle(adapter.getArticle(position));
	}

	@Override
	public void onPageScrollStateChanged(int state) {}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

}

@@ -180,24 +180,56 @@
 .ViewPager;%0A
+import android.util.TypedValue;%0A
 import andro
@@ -1725,21 +1725,36 @@
  AQuery(
+new ImageView(
 this)
+)
 .image(u
@@ -1942,24 +1942,219 @@
  != null) %7B%0A
+%09%09%09%09%09%09int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 48, getResources().getDisplayMetrics());%0A%09%09%09%09%09%09Bitmap scaledBitmap = Bitmap.createScaledBitmap(bm, size, size, true);%0A
 %09%09%09%09%09%09getAct
@@ -2205,18 +2205,28 @@
 rces(), 
-bm
+scaledBitmap
 ));%0A%09%09%09%09
@@ -2241,16 +2241,77 @@
 %0A%09%09%09%7D);%0A
+%09%09%7D else %7B%0A%09%09%09getActionBar().setIcon(R.drawable.ic_selfoss);%0A
 %09%09%7D%0A%09%7D%0A%0A

Fix icon in actionbar in ArticleActivity