package kz.arta.synergy.workflow.mobile.android.comments.adapters;

import android.content.Context;
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 java.util.List;

import kz.arta.synergy.workflow.mobile.android.R;
import kz.arta.synergy.workflow.mobile.android.comments.CommentConst;
import kz.arta.synergy.workflow.mobile.android.comments.models.CommentItem;
import kz.arta.synergy.workflow.mobile.android.common.utils.Fonts;

/**
 * Created by neox on 05.03.14.
 * Адаптер для списка комментариев
 */
public class AdapterAllComments extends ArrayAdapter<CommentItem> {

    private Context context;
    private List<CommentItem> comments;

    public AdapterAllComments(Context context, List<CommentItem> comments) {
        super(context, R.layout.item_all_comments, comments);
        this.context = context;
        this.comments = comments;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;

        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            rowView = inflater.inflate(R.layout.item_all_comments, parent, false);
            holder = new ViewHolder();
            holder.commentIcon = (ImageView) rowView.findViewById(R.id.all_comment_icon);
            holder.author = (TextView) rowView.findViewById(R.id.all_comment_name);
            holder.commentText = (TextView) rowView.findViewById(R.id.all_comment_text);
            holder.date = (TextView) rowView.findViewById(R.id.all_comment_time);
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        if (comments.get(position).getComment_icon_type().equals(CommentConst.COMMENT_ICON)) {
            holder.commentIcon.setImageResource(R.drawable.comment_icon);
        } else if (comments.get(position).getComment_icon_type().equals(CommentConst.COMMENT_ICON_GRAY)) {
            holder.commentIcon.setImageResource(R.drawable.comment_icon_gray);
        } else if (comments.get(position).getComment_icon_type().equals(CommentConst.COMMENT_ICON_GREEN)) {
            holder.commentIcon.setImageResource(R.drawable.comment_icon_green);
        } else if (comments.get(position).getComment_icon_type().equals(CommentConst.COMMENT_ICON_RED)) {
            holder.commentIcon.setImageResource(R.drawable.comment_icon_red);
        } else if (comments.get(position).getComment_icon_type().equals(CommentConst.COMMENT_ICON_RESULT)) {
            holder.commentIcon.setImageResource(R.drawable.comment_icon_result);
        } else if (comments.get(position).getComment_icon_type().equals(CommentConst.COMMENT_ICON_BLUE)) {
            holder.commentIcon.setImageResource(R.drawable.comment_icon_blue);
        }

        holder.author.setText(comments.get(position).getAuthor().getName());
        holder.commentText.setText(comments.get(position).getComment());
        holder.date.setText(comments.get(position).getCreated_label());

        holder.author.setTypeface(Fonts.getMedium(context));
        holder.date.setTypeface(Fonts.getMedium(context));
        holder.commentText.setTypeface(Fonts.getMedium(context));

        return rowView;
    }

    static class ViewHolder {
        ImageView commentIcon;
        TextView author;
        TextView commentText;
        TextView date;
    }
}
