
/*
 * Copyright 2014 Uwe Trottmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.battlelancer.seriesguide.adapters;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.battlelancer.seriesguide.R;
import com.battlelancer.seriesguide.util.ServiceUtils;
import com.uwetrottmann.trakt.v2.entities.Comment;
import java.util.List;

/**
 * Custom ArrayAdapter which binds {@link Comment} items to views using the ViewHolder pattern.
 */
public class TraktCommentsAdapter extends ArrayAdapter<Comment> {

    private final LayoutInflater mInflater;

    public TraktCommentsAdapter(Context context) {
        super(context, R.layout.item_comment);
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setData(List<Comment> data) {
        clear();
        if (data != null) {
            for (Comment item : data) {
                add(item);
            }
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid
        // unnecessary calls to findViewById() on each row.
        TraktCommentsAdapter.ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_comment, parent, false);

            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.shout = (TextView) convertView.findViewById(R.id.shout);
            holder.timestamp = (TextView) convertView.findViewById(R.id.timestamp);
            holder.avatar = (ImageView) convertView.findViewById(R.id.avatar);

            convertView.setTag(holder);
        } else {
            holder = (TraktCommentsAdapter.ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        final Comment comment = getItem(position);

        holder.name.setText(comment.user.username);
        ServiceUtils.getPicasso(getContext())
                .load(comment.user.images.avatar.full)
                .into(holder.avatar);

        if (comment.spoiler) {
            holder.shout.setText(R.string.isspoiler);
            holder.shout.setTextAppearance(getContext(), R.style.TextAppearance_Body_Highlight_Red);
        } else {
            holder.shout.setText(comment.comment);
            holder.shout.setTextAppearance(getContext(), R.style.TextAppearance_Body);
        }

        String timestamp = (String) DateUtils.getRelativeTimeSpanString(
                comment.created_at.getMillis(), System.currentTimeMillis(),
                DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_ALL);
        holder.timestamp.setText(timestamp);

        return convertView;
    }

    static class ViewHolder {
        TextView name;

        TextView shout;

        TextView timestamp;

        ImageView avatar;
    }
}

@@ -2670,32 +2670,343 @@
 tion);%0A%0A        
+String name = null;%0A        String avatarPath = null;%0A        if (comment.user != null) %7B%0A            name = comment.user.username;%0A            if (comment.user.images != null && comment.user.images.avatar != null) %7B%0A                avatarPath = comment.user.images.avatar.full;%0A            %7D%0A        %7D%0A        
 holder.name.setT
@@ -3009,33 +3009,16 @@
 setText(
-comment.user.user
 name);%0A 
@@ -3088,39 +3088,18 @@
 oad(
-comment.user.images.
 avatar
-.full
+Path
 )%0A  

Check for non-available trakt user image.