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.model.entity.MessageReplyEntity;
import com.sheep.gamegroup.util.TimeUtil;
import com.sheep.jiuyan.samllsheep.R;

import java.util.List;

/**
 * created on：2018/9/3 on 17:31
 * created by: YSL
 * 描述:
 */
public class MessageRightAdapter extends RecyclerView.Adapter<MessageRightAdapter.MyHolder> {
    private Context mContext;
    private List<MessageReplyEntity> mList;
    private ItemOnClickListener itemClick;

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

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view;
        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 MessageRightAdapter.MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder viewHolder, final int i) {
        if (i == mList.size() - 1) {
            viewHolder.viewLine.setVisibility(View.GONE);//最后一条数据隐藏线条
        }
        //GlideImageLoader.centerImage(viewHolder.ivIcon, "");
        viewHolder.ivIcon.setImageResource(R.mipmap.message_unread);
        viewHolder.tvTitle.setText(mList.get(i).getTitleText());
        viewHolder.tvTime.setText(TimeUtil.TimeStamp2Date(mList.get(i).getCreated_at(), "yyyy-MM-dd HH:mm"));
        viewHolder.tvContent.setText(mList.get(i).getContent());
        if (mList.get(i).getIs_look()==1){
            viewHolder.ivIcon.setImageResource(R.mipmap.icon_unread);//已读
        }else {
            viewHolder.ivIcon.setImageResource(R.mipmap.message_unread);//未读
        }
        viewHolder.itemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClick.itemClick(mList.get(i).getId());
            }
        });
    }

    @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);
    }
}
