package be.ugent.zeus.resto.client.data;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import be.ugent.zeus.resto.client.R;
import java.util.Comparator;
import java.util.List;

public class NewsList extends ArrayAdapter<NewsItem> {

  /**
   * The length of the article previews, in number of characters.
   */
  private static final int SHORT_TEXT_LENGTH = 150;
  
  public NewsList(Context context, List<NewsItem> objects) {
    super(context, R.layout.news_list_item, objects);
    sort(new NewsItemComparator());
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (row == null) {
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      row = inflater.inflate(R.layout.news_list_item, parent, false);
    }

    NewsItem item = getItem(position);

    TextView title = (TextView) row.findViewById(R.id.news_item_title);
    title.setText(Html.fromHtml(item.title));

    // TODO: add date as well
    TextView association = (TextView) row.findViewById(R.id.news_item_association);
    association.setText(Html.fromHtml(item.club));

    // TODO: can we cut off this text in a more intelligent way?
    String shorty = intelligentCutOff(item.description);

    TextView shorttxt = (TextView) row.findViewById(R.id.news_item_short);
    shorttxt.setText(Html.fromHtml(shorty));
    return row;
  }
 
  private String intelligentCutOff(String description) {
	  String shorty;
	  
	  if(description.length() < SHORT_TEXT_LENGTH) {
		  return description;
	  } 
	  
	  shorty = description.substring(0, SHORT_TEXT_LENGTH);
	  
	  while(shorty.charAt(shorty.length()-1) != ' ') {
		  shorty = shorty.substring(0, shorty.length() - 1);
	  }
	  
	  shorty += "...";
	  return shorty;
  }
  
  private class NewsItemComparator implements Comparator<NewsItem> {

    public int compare(NewsItem item1, NewsItem item2) {
      return item1.date.compareTo(item2.date);
    }
  }
 
}

@@ -1345,73 +1345,8 @@
 );%0A%0A
-    // TODO: can we cut off this text in a more intelligent way?%0A
     
@@ -1394,24 +1394,24 @@
 scription);%0A
+
 %0A    TextVie
@@ -1602,30 +1602,8 @@
 ) %7B%0A
-%09  String shorty;%0A%09  %0A
 %09  i
@@ -1686,44 +1686,24 @@
 %09  %0A
-%09
   
-shorty = description.substring(0,
+  int cutoff =
  SHO
@@ -1716,22 +1716,17 @@
 T_LENGTH
-);%0A%09  
+;
 %0A%09  whil
@@ -1731,151 +1731,157 @@
 ile(
-shorty.charAt(shorty.length()-1) != ' ') %7B%0A%09%09  shorty = shorty.substring(0, shorty.length() - 1);%0A%09  %7D%0A%09  %0A%09  shorty += %22...%22;%0A%09  return shorty
+cutoff %3E 100 && !Character.isWhitespace(description.charAt(cutoff - 1))) %7B%0A      cutoff--;%0A%09  %7D%09  %0A%09  return description.substring(0, cutoff) + %22...%22
 ;%0A  

Improve header cutoff algorithm.