package fr.ydelouis.selfoss.rest;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;

import com.squareup.picasso.Picasso;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.OrmLiteDao;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.rest.RestService;

import java.io.IOException;
import java.util.List;

import fr.ydelouis.selfoss.entity.Article;
import fr.ydelouis.selfoss.entity.ArticleType;
import fr.ydelouis.selfoss.entity.Tag;
import fr.ydelouis.selfoss.model.ArticleSyncActionDao;
import fr.ydelouis.selfoss.model.DatabaseHelper;
import fr.ydelouis.selfoss.sync.ArticleSyncAction;
import fr.ydelouis.selfoss.util.ArticleContentParser;

@EBean
public class SelfossRestWrapper {

    @RestService
    protected SelfossRest rest;
    @OrmLiteDao(helper = DatabaseHelper.class)
    protected ArticleSyncActionDao articleSyncActionDao;
    @RootContext
    protected Context context;
    private Picasso picasso;


    @AfterInject
    protected void init() {
        picasso = Picasso.with(context);
    }

    public List<Article> listArticles(int offset, int count) {
        return preProcess(rest.listArticles(offset, count));
    }

    public List<Article> listArticles(Tag tag, int offset, int count) {
        return preProcess(rest.listArticles(tag, offset, count));
    }

    public List<Article> listArticles(int sourceId, int offset, int count) {
        return preProcess(rest.listArticles(sourceId, offset, count));
    }

    public List<Article> listArticles(ArticleType type, Tag tag, int offset, int count) {
        return preProcess(rest.listArticles(type, tag, offset, count));
    }

    public List<Article> listArticles(ArticleType type, int sourceId, int offset, int count) {
        return preProcess(rest.listArticles(type, sourceId, offset, count));
    }

    public List<Article> listArticles(ArticleType type, int offset, int count) {
        return preProcess(rest.listArticles(type, offset, count));
    }

    public List<Article> listUnreadArticles(int offset, int count) {
        return preProcess(rest.listUnreadArticles(offset, count));
    }

    public List<Article> listStarredArticles(int offset, int count) {
        return preProcess(rest.listStarredArticles(offset, count));
    }

    private List<Article> preProcess(List<Article> articles) {
        if (articles != null) {
            for (Article article : articles) {
                preProcess(article);
            }
        }
        return articles;
    }

    private void preProcess(Article article) {
        applySyncAction(article);
        extractImage(article);
    }

    private void applySyncAction(Article article) {
        ArticleSyncAction syncAction = articleSyncActionDao.queryForArticle(article);
        if (syncAction != null) {
            syncAction.getAction().execute(article);
        }
    }

    private void extractImage(Article article) {
        ArticleContentParser parser = new ArticleContentParser(article);
        List<String> imageUrls = parser.extractImageUrls();
        for (String imageUrl : imageUrls) {
            try {
                Bitmap bitmap = picasso.load(imageUrl).get();
                if (bitmap != null && !bitmapIsEmpty(bitmap)) {
                    article.setImageUrl(imageUrl);
                    return;
                }
            } catch (IOException ignored) {
            }
        }
    }

    private boolean bitmapIsEmpty(Bitmap bitmap) {
        for (int x = 0; x < bitmap.getWidth(); x++) {
            for (int y = 0; y < bitmap.getHeight(); y++) {
                if (bitmap.getPixel(x, y) != Color.WHITE) {
                    return false;
                }
            }
        }
        return true;
    }

}

@@ -32,40 +32,8 @@
 t;%0A%0A
-import android.content.Context;%0A
 impo
@@ -88,108 +88,54 @@
 ics.
-Color;%0A%0Aimport com.squareup.picasso.Picasso
+BitmapFactory
 ;%0A
-%0A
 import 
-org.
 android
-annotations.annotations.AfterInject;
+.graphics.Color;%0A
 %0Aimp
@@ -238,63 +238,8 @@
 ao;%0A
-import org.androidannotations.annotations.RootContext;%0A
 impo
@@ -319,24 +319,76 @@
 OException;%0A
+import java.net.URL;%0Aimport java.net.URLConnection;%0A
 import java.
@@ -936,179 +936,8 @@
 Dao;
-%0A    @RootContext%0A    protected Context context;%0A    private Picasso picasso;%0A%0A%0A    @AfterInject%0A    protected void init() %7B%0A        picasso = Picasso.with(context);%0A    %7D
 %0A%0A  
@@ -2994,80 +2994,46 @@
     
-try %7B%0A                Bitmap bitmap = picasso.load(imageUrl).get();%0A    
+Bitmap bitmap = loadBitmap(imageUrl);%0A
     
@@ -3104,20 +3104,16 @@
         
-    
 article.
@@ -3155,42 +3155,288 @@
     
-    return;%0A                %7D%0A    
+return;%0A            %7D%0A        %7D%0A    %7D%0A%0A    private Bitmap loadBitmap(String imageUrl) %7B%0A        try %7B%0A            URL url = new URL(imageUrl);%0A            URLConnection connection = url.openConnection();%0A            return BitmapFactory.decodeStream(connection.getInputStream());%0A
     
@@ -3464,15 +3464,9 @@
 ion 
-ignored
+e
 ) %7B%0A
@@ -3469,33 +3469,44 @@
 ) %7B%0A            
-%7D
+return null;
 %0A        %7D%0A    %7D
@@ -3554,24 +3554,124 @@
 p bitmap) %7B%0A
+        if (bitmap.getWidth() %3C 50 %7C%7C bitmap.getHeight() %3C 50) %7B%0A            return true;%0A        %7D%0A
         for 

Fix crash due to Picasso