package com.newsblur.view;

import android.content.Context;
import android.database.Cursor;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.newsblur.R;
import com.newsblur.activity.NewsBlurApplication;
import com.newsblur.database.DatabaseConstants;
import com.newsblur.domain.Story;
import com.newsblur.util.ImageLoader;
import com.newsblur.util.StoryUtils;

import java.util.Date;

public class SocialItemViewBinder implements ViewBinder {

    private final Context context;
	private ImageLoader imageLoader;
    private boolean ignoreIntel;

	public SocialItemViewBinder(final Context context, boolean ignoreIntel) {
        this.context = context;
		this.imageLoader = ((NewsBlurApplication) context.getApplicationContext()).getImageLoader();
        this.ignoreIntel = ignoreIntel;
	}

    public SocialItemViewBinder(final Context context) {
        this(context, false);
    }
	
	@Override
	public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
		final String columnName = cursor.getColumnName(columnIndex);
		final int hasBeenRead = cursor.getInt(cursor.getColumnIndex(DatabaseConstants.STORY_READ));
		if (TextUtils.equals(cursor.getColumnName(columnIndex), DatabaseConstants.FEED_FAVICON_URL)) {
			String faviconUrl = cursor.getString(columnIndex);
			imageLoader.displayImage(faviconUrl, ((ImageView) view), true);
			return true;
		} else if (TextUtils.equals(columnName, DatabaseConstants.STORY_INTELLIGENCE_AUTHORS)) {
            if (! this.ignoreIntel) {
                int authors = cursor.getInt(columnIndex);
                int tags = cursor.getInt(cursor.getColumnIndex(DatabaseConstants.STORY_INTELLIGENCE_TAGS));
                int feed = cursor.getInt(cursor.getColumnIndex(DatabaseConstants.STORY_INTELLIGENCE_FEED));
                int title = cursor.getInt(cursor.getColumnIndex(DatabaseConstants.STORY_INTELLIGENCE_TITLE));
                
                int score = Story.getIntelligenceTotal(title, authors, tags, feed);

                Drawable icon;
                if (score > 0) {
                    icon = view.getResources().getDrawable(R.drawable.g_icn_focus);
                } else if (score == 0) {
                    icon = view.getResources().getDrawable(R.drawable.g_icn_unread);
                } else {
                    icon = view.getResources().getDrawable(R.drawable.g_icn_hidden);
                }
                icon.mutate().setAlpha(hasBeenRead == 0 ? 255 : 127);
                view.setBackgroundDrawable(icon);
            } else {
                view.setBackgroundDrawable(null);
            }

			((TextView) view).setText("");
			return true;
		} else if (TextUtils.equals(columnName, DatabaseConstants.STORY_AUTHORS)) {
			String authors = cursor.getString(columnIndex);
			if (!TextUtils.isEmpty(authors)) {
				((TextView) view).setText(authors.toUpperCase());
			}
			return true;
		} else if (TextUtils.equals(columnName, DatabaseConstants.STORY_TITLE)) {
			((TextView) view).setText(Html.fromHtml(cursor.getString(columnIndex)));
			return true;
		} else if (TextUtils.equals(columnName, DatabaseConstants.STORY_TIMESTAMP)) {
            ((TextView) view).setText(StoryUtils.formatShortDate(context, new Date(cursor.getLong(columnIndex))));
            return true;
        }
		return false;
	}

}

@@ -2971,16 +2971,24 @@
 x);%0A
-%09%09%09
+            
 if (
-!
 Text
@@ -3017,65 +3017,204 @@
 ) %7B%0A
-%09%09%09%09((TextView) view).setText(authors.toUpperCase());%0A%09%09%09
+                view.setVisibility(View.GONE);%0A            %7D else %7B%0A                ((TextView) view).setText(authors.toUpperCase());%0A                view.setVisibility(View.VISIBLE);%0A            
 %7D%0A%09%09

Handle missing authors in story view. (#574)