package com.sheep.gamegroup.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
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.LinearLayout;
import android.widget.TextView;

import com.sheep.gamegroup.util.GlideImageLoader;
import com.sheep.jiuyan.samllsheep.R;


import java.util.List;


/**
 * created on：2018/8/28 on 16:08
 * created by: YSL
 * 描述:
 */
public class MessageLeftAdapter extends RecyclerView.Adapter<MessageLeftAdapter.Myholder> {
    private Context mContext;
    private List<?> mList;
    private ItemOnClickListener itemClick;
    private int position;

    public MessageLeftAdapter(Context mContext, List<?> mList, ItemOnClickListener itemClick) {
        this.mContext = mContext;
        this.mList = mList;
        this.itemClick = itemClick;
    }


    @NonNull
    @Override
    public Myholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = null;
        if (mList == null || mList.isEmpty()) {
            view = LayoutInflater.from(mContext).inflate(R.layout.empty_view, null);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.adapter_message_left_fragment, null);
        }
        return new Myholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Myholder viewHolder, int i) {
        if (mList == null || mList.isEmpty()) {
            return;
        } else {
            if (i == mList.size() - 1) {
                viewHolder.viewLine.setVisibility(View.GONE);//最后一条数据隐藏线条
            }
            GlideImageLoader.centerImage(viewHolder.ivIcon, "");
            viewHolder.tvTitle.setText("");
            viewHolder.tvTime.setText("");
            viewHolder.tvContent.setText("");
            position = i;
            viewHolder.itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemClick.itemClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class Myholder extends RecyclerView.ViewHolder {
        private ImageView ivIcon;//头像
        private TextView tvTitle;//标题
        private TextView tvTime;//时间
        private TextView tvContent;//每条消息的内容，这里最多只显示一行
        private View viewLine;//item之间的线条
        private LinearLayout itemLayout;//整个item布局

        public Myholder(View view) {
            super(view);
            ivIcon = view.findViewById(R.id.iv_icon);
            tvTitle = view.findViewById(R.id.tv_title);
            tvTime = view.findViewById(R.id.tv_time);
            tvContent = view.findViewById(R.id.tv_content);
            viewLine = view.findViewById(R.id.view_line);
            itemLayout = view.findViewById(R.id.item_layout);
        }
    }

    /**
     * 设置每一条消息的回调
     */
    public interface ItemOnClickListener {
        /**
         * @param position，当前消息在获取数据集合中的对应位置
         */
        void itemClick(int position);
    }
}
