package com.datarecovery.master.module.member;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.AsyncListDiffer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.datarecovery.master.databinding.ItemMemberEvaluateBinding;
import com.datarecovery.master.module.member.bean.EvaluateBean;

import java.util.List;
import java.util.Objects;

public class MemberEvaluateAdapter extends RecyclerView.Adapter<MemberEvaluateAdapter.ViewHolder> {

    private final AsyncListDiffer<EvaluateBean> listDiffer;

    public MemberEvaluateAdapter() {
        listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<EvaluateBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull EvaluateBean oldItem, @NonNull EvaluateBean newItem) {
                return Objects.equals(oldItem.getName(), newItem.getName());
            }

            @Override
            public boolean areContentsTheSame(@NonNull EvaluateBean oldItem, @NonNull EvaluateBean newItem) {
                return Objects.equals(oldItem.getContent(), newItem.getContent());
            }
        });
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        return new ViewHolder(ItemMemberEvaluateBinding.inflate(layoutInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        EvaluateBean evaluateBean = listDiffer.getCurrentList().get(position);
        holder.bind(evaluateBean);
    }

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

    public void submit(List<EvaluateBean> evaluateBeans) {
        listDiffer.submitList(evaluateBeans);
    }

    protected static class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemMemberEvaluateBinding binding;

        public ViewHolder(@NonNull ItemMemberEvaluateBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void bind(EvaluateBean evaluateBean) {
            binding.setEvaluateBean(evaluateBean);
        }
    }
}