package com.mjsheng.myappstore.adapter;

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


import com.mjsheng.myappstore.R;
import com.mjsheng.myappstore.bean.CommentInfo;
import com.mjsheng.myappstore.view.StarRatingBar;

import java.util.ArrayList;

public class CommentsListAdapter extends BaseAdapter {

    private ArrayList<CommentInfo> mDatas = new ArrayList<>();
    private Context mContext;

    public CommentsListAdapter(Context context) {
        mContext = context;
    }

    public void setData(ArrayList<CommentInfo> datas) {
        if (mDatas != null) {
            mDatas.clear();
        }
        this.mDatas.addAll(datas);
    }

    @Override
    public int getCount() {
        if (mDatas == null) {
            return 0;
        }
        return mDatas.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mDatas.get(arg0);
    }

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


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.comments_list_item, null);
            holder.starViews = (StarRatingBar) convertView.findViewById(R.id.ratingbar);
            holder.commentDate = (TextView) convertView.findViewById(R.id.comment_date);
            holder.commentAuthor = (TextView) convertView.findViewById(R.id.comment_author);
            holder.commentContent = (TextView) convertView.findViewById(R.id.comment_content);
            holder.starViews.setmClickable(false);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CommentInfo info = (CommentInfo) getItem(position);
        if (info != null) {
            holder.starViews.setStar(Integer.parseInt(info.getPingfen()));
//            holder.commentDate.setText(info.getPinglun_time());
//            if (info.getNiming().equals("true")) {
//                holder.commentAuthor.setText(info.getUser_name().substring(0, 3) + "******");
//            } else {
//                holder.commentAuthor.setText(info.getUser_name());
//            }
            holder.commentContent.setText(info.getPinglun_content());

        }
        return convertView;
    }

    public final class ViewHolder {
        public StarRatingBar starViews;
        public TextView commentDate;
        public TextView commentAuthor;
        public TextView commentContent;
    }
}
