package com.casic.dcms.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.casic.dcms.R;
import com.casic.dcms.bean.NoticeBean;
import com.qmuiteam.qmui.recyclerView.QMUISwipeAction;
import com.qmuiteam.qmui.recyclerView.QMUISwipeViewHolder;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : Pengxh
 * @time : 2021/4/15 9:47
 * @email : 290677893@qq.com
 **/
public class SwipeViewAdapter extends RecyclerView.Adapter<QMUISwipeViewHolder> {

    private List<NoticeBean> mData = new ArrayList<>();
    private final QMUISwipeAction mDeleteAction;

    public SwipeViewAdapter(Context context) {
        QMUISwipeAction.ActionBuilder builder = new QMUISwipeAction.ActionBuilder()
                .textSize(QMUIDisplayHelper.sp2px(context, 18))
                .textColor(Color.WHITE)
                .paddingStartEnd(QMUIDisplayHelper.dp2px(context, 18));
        mDeleteAction = builder.text("删除").backgroundColor(Color.RED).build();
    }

    public void setData(@Nullable List<NoticeBean> list) {
        mData.clear();
        if (list != null) {
            mData.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void remove(int pos) {
        mData.remove(pos);
        notifyItemRemoved(pos);
    }

    @NonNull
    @Override
    public QMUISwipeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_notice_list, parent, false);
        final QMUISwipeViewHolder swipeViewHolder = new QMUISwipeViewHolder(view);
        swipeViewHolder.addSwipeAction(mDeleteAction);
        return swipeViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull QMUISwipeViewHolder holder, int position) {
        TextView noticeTitleView = holder.itemView.findViewById(R.id.noticeTitleView);
        TextView noticeDateView = holder.itemView.findViewById(R.id.noticeDateView);
        TextView noticeContentView = holder.itemView.findViewById(R.id.noticeContentView);
        TextView messageTagView = holder.itemView.findViewById(R.id.messageTagView);

        NoticeBean noticeBean = mData.get(position);
        noticeTitleView.setText(noticeBean.getTitle());
        noticeDateView.setText(noticeBean.getPushDate());
        noticeContentView.setText(noticeBean.getContent());
        if (noticeBean.getIsRead().equals("0")) {
            messageTagView.setVisibility(View.GONE);
        } else {
            messageTagView.setVisibility(View.VISIBLE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickListener.onClick(position);
            }
        });
    }

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

    private OnItemClickListener clickListener;

    public interface OnItemClickListener {
        void onClick(int position);
    }

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