package ai.pai.client.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.common.utils.TimeUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;

import ai.pai.client.R;
import ai.pai.client.beans.CommentBean;
import ai.pai.client.utils.PhotoLoader;

public class CommentListAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    private DisplayImageOptions options;
    private ArrayList<CommentBean> commentList;

    public CommentListAdapter(Context context, ArrayList<CommentBean> commentList){
        this.context = context;
        this.commentList = commentList;
        inflater = LayoutInflater.from(context);
        options = PhotoLoader.getUserAvatarOptions();
    }

    @Override
    public int getCount() {
        return commentList.size();
    }

    @Override
    public Object getItem(int position) {
        return commentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(position>=commentList.size()){
            return null;
        }
        final CommentBean info = commentList.get(position);
        ViewHolder holder;
        if(convertView==null){
            convertView = inflater.inflate(R.layout.comment_list_item,null);
            holder = new ViewHolder();
            holder.commentNameText = (TextView)convertView.findViewById(R.id.tv_commenter_name);
            holder.commentContentText = (TextView)convertView.findViewById(R.id.tv_comment_content);
            holder.avatarImg = (ImageView)convertView.findViewById(R.id.iv_commenter_avatar);
            holder.commentTimeText = (TextView)convertView.findViewById(R.id.tv_comment_time);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.commentNameText.setText(info.nickname);
        holder.commentContentText.setText(info.comment);
        holder.commentTimeText.setText(TimeUtils.getFormattedTime(info.time));
        PhotoLoader.getInstance(context).displayImage(info.avatarUrl,holder.avatarImg,options);
        return convertView;
    }

    private  static class ViewHolder {
        ImageView avatarImg;
        TextView commentNameText;
        TextView commentContentText;
        TextView commentTimeText;
    }

}
