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.dateview.DateUtil;
import com.sheep.gamegroup.model.api.OnItemClickListener;
import com.sheep.gamegroup.model.entity.SystemNotification;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.TimeUtil;
import com.sheep.gamegroup.util.ViewUtil;
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.SNHolder> {
    private Context mContext;
    private List<SystemNotification> mList;
    private OnItemClickListener<SystemNotification> itemClick;

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


    @NonNull
    @Override
    public SNHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view;
        if (ListUtil.isEmpty(mList)) {
            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 SNHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SNHolder viewHolder, int i) {
        final int position = - 1 + viewHolder.getAdapterPosition();
        viewHolder.viewLine.setVisibility(ListUtil.size(mList) == position + 1 ? View.INVISIBLE : View.VISIBLE);//最后一条数据的线条是否隐藏
        final SystemNotification item = ListUtil.getItem(mList, position);
        if (item == null) {
            ViewUtil.setDefaultText(viewHolder.tvTitle);
            ViewUtil.setDefaultText(viewHolder.tvTime);
            ViewUtil.setDefaultText(viewHolder.tvContent);
        } else {
            ViewUtil.setText(viewHolder.tvTitle, item.getTitle());
//            viewHolder.tvTime.setText(TimeUtil.TimeStamp2Date(item.getCreated_at(), "yyyy-MM-dd HH:mm"));
            viewHolder.tvTime.setText(DateUtil.getTimeText(item.getCreated_at()));
            ViewUtil.setText(viewHolder.tvContent, item.getContent());
            if (item.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.onClickItem(view, item, position);
                }
            });
        }
    }

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

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


        SNHolder(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);

        }
    }
}
