package bolianeducation.bolianchild.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import bolianeducation.bolianchild.R;
import bolianeducation.bolianchild.modle.message.GroupEntity;

/**
 * Created by admin on 2017/5/2.
 */

public class MessageGroupAdapter extends RecyclerView.Adapter<MessageGroupAdapter.MessageGroupViewHolder> {
    private OnItemClickListener onItemClickListener;
    private final int ADD_GROUP = 1;
    Context context;
    List<GroupEntity> list;

    public MessageGroupAdapter(Context context, List<GroupEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MessageGroupViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == ADD_GROUP) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_view, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false);
        }
        MessageGroupViewHolder holder = new MessageGroupViewHolder(view);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return ADD_GROUP;
        }
        return 0;
    }

    public interface OnItemClickListener {
        void onItemClickListener(String groupname, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }


    @Override
    public void onBindViewHolder(MessageGroupViewHolder holder, int position) {
        if (position == getItemCount() - 1) {
            holder.tvAddGroup.setText("新建群");
        } else {
            GroupEntity groupEntity = list.get(position);
            holder.tvUsername.setText(groupEntity.getGroupName());
//        Picasso.with(context).load(groupEntity.getPortraitUri()).into(holder.ivHead);
            holder.iv_online.setVisibility(View.GONE);
            holder.tv_msg.setText("item " + position + " 最后一条消息");
        }


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MessageGroupViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView tvUsername, tv_msg;
        ImageView ivHead, iv_online;
        TextView tvAddGroup;

        public MessageGroupViewHolder(View itemView) {
            super(itemView);
            tvUsername = (TextView) itemView.findViewById(R.id.tv_username);
            tv_msg = (TextView) itemView.findViewById(R.id.tv_msg);
            ivHead = (ImageView) itemView.findViewById(R.id.iv_head);
            iv_online = (ImageView) itemView.findViewById(R.id.iv_online);
            tvAddGroup = (TextView) itemView.findViewById(R.id.tv_filter_item);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                if (getPosition() == getItemCount() - 1) {
                    onItemClickListener.onItemClickListener("Join a new GroupEntityOld", getPosition());
                } else {
                    onItemClickListener.onItemClickListener(tvUsername.getText().toString(), getPosition());
                }
            }
        }
    }
}
