/*
 * Copyright 2011 Qubling Software, LLC.
 *
 * This software may be distributed under the terms of the Artistic License 2.0.
 */
package com.qubling.sidekick.widget;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.QuickContactBadge;
import android.widget.RatingBar;
import android.widget.TextView;

import com.qubling.sidekick.R;
import com.qubling.sidekick.cpan.result.Module;

/**
 * Tools for filling in module data into views.
 * 
 * @author sterling
 *
 */
public final class ModuleHelper {
    private ModuleHelper() { }

    public static void updateItem(View row, Module item) {

        // Set the module name and abstract
        TextView moduleName = (TextView) row.findViewById(R.id.module_name);
        SpannableStringBuilder formattedString = new SpannableStringBuilder(item.getName());
        if (item.getAbstract() != null) {
            formattedString.append(" - " + item.getAbstract());

            ForegroundColorSpan color = new ForegroundColorSpan(Color.GRAY);
            formattedString.setSpan(
                    color,
                    formattedString.length() - item.getAbstract().length() - 3,
                    formattedString.length(),
                    0);
        }
        moduleName.setText(formattedString);

        // Set the distribution author, name, and version
        TextView distributionName = (TextView) row.findViewById(R.id.module_author_distribution);
        distributionName.setText(
                item.getAuthor().getPauseId()
                + "/" + item.getDistribution().getName()
                + "-" + item.getDistribution().getVersion());

        // Set the rating bar
        RatingBar distRating = (RatingBar) row.findViewById(R.id.module_release_rating);
        distRating.setRating((float) item.getDistribution().getRating());

        // Set the rating count
        TextView distRatingCount = (TextView) row.findViewById(R.id.module_release_rating_count);
        distRatingCount.setText(String.valueOf(item.getDistribution().getRatingCount()));

        // Set the favorite count
        Button favoriteCount = (Button) row.findViewById(R.id.module_release_favorite);
        if (item.getDistribution().getFavoriteCount() > 0) {
            favoriteCount.setText(item.getDistribution().getFavoriteCount() + "++ ");
            favoriteCount.setBackgroundResource(R.drawable.btn_favorite_others);
            favoriteCount.setShadowLayer(1.5f, 1f, 1f, R.color.favorite_text_shadow_color);
        }

        // Not favorited yet, set it to a blank
        else {
            favoriteCount.setText("++ ");
            favoriteCount.setBackgroundResource(R.drawable.btn_favorite_default);
            favoriteCount.setShadowLayer(0, 0, 0, Color.TRANSPARENT);
        }

        // Mark this as our favorite
        if (item.getDistribution().isMyFavorite()) {
            favoriteCount.setBackgroundResource(R.drawable.btn_favorite_mine);
        }

        // Set the quick contact badge to the author's picture
        QuickContactBadge badge = (QuickContactBadge) row.findViewById(R.id.module_author_avatar);
        if (item.getAuthor().getGravatarBitmap() != null) {
            badge.setImageBitmap(item.getAuthor().getGravatarBitmap());
        }

        // No user picture, set to default
        else {
            badge.setImageResource(R.drawable.ic_contact_picture);
        }
    }
}

@@ -479,16 +479,118 @@
 kick.R;%0A
+import com.qubling.sidekick.cpan.result.Author;%0Aimport com.qubling.sidekick.cpan.result.Distribution;%0A
 import c
@@ -1505,16 +1505,273 @@
 String);
+%0A        %0A        Author author = item.getAuthor();%0A        if (author == null) author = new Author(%22...%22);%0A        %0A        Distribution distribution = item.getDistribution();%0A        if (distribution == null) distribution = new Distribution(%22...%22, %22...%22);
 %0A%0A      
@@ -1932,66 +1932,88 @@
     
-distributionName.setText(%0A                item.getA
+StringBuilder authorDist = new StringBuilder();%0A        authorDist.append(a
 uthor
-()
 .get
@@ -2021,16 +2021,18 @@
 auseId()
+);
 %0A       
@@ -2036,33 +2036,59 @@
     
-        + %22/%22 + item.getD
+authorDist.append('/');%0A        authorDist.append(d
 istr
@@ -2090,26 +2090,24 @@
 distribution
-()
 .getName()%0A 
@@ -2104,16 +2104,18 @@
 etName()
+);
 %0A       
@@ -2119,33 +2119,59 @@
     
-        + %22-%22 + item.getD
+authorDist.append('-');%0A        authorDist.append(d
 istr
@@ -2173,26 +2173,24 @@
 distribution
-()
 .getVersion(
@@ -2188,24 +2188,70 @@
 tVersion());
+%0A        distributionName.setText(authorDist);
 %0A%0A        //
@@ -2396,25 +2396,17 @@
 (float) 
-item.getD
+d
 istribut
@@ -2400,34 +2400,32 @@
 at) distribution
-()
 .getRating());%0A%0A
@@ -2419,24 +2419,33 @@
 tRating());%0A
+        %09
 %0A        // 
@@ -2563,28 +2563,25 @@
 count);%0A    
-    
+%09
 distRatingCo
@@ -2607,25 +2607,17 @@
 valueOf(
-item.getD
+d
 istribut
@@ -2615,26 +2615,24 @@
 distribution
-()
 .getRatingCo
@@ -2636,24 +2636,29 @@
 gCount()));%0A
+    %09
 %0A        // 
@@ -2776,33 +2776,25 @@
         if (
-item.getD
+d
 istribution(
@@ -2784,34 +2784,32 @@
 if (distribution
-()
 .getFavoriteCoun
@@ -2853,25 +2853,17 @@
 setText(
-item.getD
+d
 istribut
@@ -2865,18 +2865,16 @@
 ribution
-()
 .getFavo
@@ -3396,25 +3396,17 @@
     if (
-item.getD
+d
 istribut
@@ -3408,18 +3408,16 @@
 ribution
-()
 .isMyFav
@@ -3691,32 +3691,22 @@
     if (
-item.getA
+a
 uthor
-()
 .getGrav
@@ -3762,32 +3762,22 @@
 eBitmap(
-item.getA
+a
 uthor
-()
 .getGrav

Fix a possible NPE in ModuleHelper