package com.datarecovery.master.module.member;

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

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

import com.datarecovery.master.data.api.bean.MemberGoodsBean;
import com.datarecovery.master.databinding.ItemMemberGoodsBinding;

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

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

    private final AsyncListDiffer<MemberGoodsBean> listDiffer;


    private final LifecycleOwner lifecycleOwner;

    private onItemSelectListener onItemSelectListener;

    private MemberGoodsBean selectBean;

    public void setOnItemSelectListener(GoodsItemAdapter.onItemSelectListener onItemSelectListener) {
        this.onItemSelectListener = onItemSelectListener;
    }

    public GoodsItemAdapter(LifecycleOwner lifecycleOwner) {
        this.lifecycleOwner = lifecycleOwner;
        listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<MemberGoodsBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull MemberGoodsBean oldItem, @NonNull MemberGoodsBean newItem) {
                return oldItem.getId() == newItem.getId();
            }

            @Override
            public boolean areContentsTheSame(@NonNull MemberGoodsBean oldItem, @NonNull MemberGoodsBean newItem) {
                return Objects.equals(oldItem.getName(), newItem.getName())
                        && Objects.equals(oldItem.getOriginalAmount(), newItem.getOriginalAmount())
                        && Objects.equals(oldItem.getRMBAmount(), newItem.getRMBAmount())
                        && Objects.equals(oldItem.getDescription(), newItem.getDescription());
            }
        });
    }

    public void submit(List<MemberGoodsBean> list) {
        listDiffer.submitList(list, () -> {
            if (list == null) {
                return;
            }
            for (MemberGoodsBean memberGoodsBean : list) {
                if (memberGoodsBean.isPopular()) {
                    memberGoodsBean.setSelect(true);
                    setSelectBean(memberGoodsBean);
                    break;
                }
            }
        });
    }

    public void setSelectBean(MemberGoodsBean memberGoodsBean) {
        this.selectBean = memberGoodsBean;
        if (onItemSelectListener != null) {
            onItemSelectListener.onItemSelect(memberGoodsBean);
        }
    }

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

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

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemMemberGoodsBinding binding;

        public ViewHolder(@NonNull ItemMemberGoodsBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.setLifecycleOwner(lifecycleOwner);
            binding.getRoot().setOnClickListener(v -> {
                MemberGoodsBean clickBean = binding.getGoodsBean();
                if (selectBean != null && selectBean.getId() != clickBean.getId()) {
                    selectBean.setSelect(false);
                    clickBean.setSelect(true);
                    setSelectBean(clickBean);
                }
            });
        }

        public void bind(MemberGoodsBean goodsBean) {
            binding.setGoodsBean(goodsBean);
        }
    }

    public interface onItemSelectListener {
        void onItemSelect(MemberGoodsBean goodsBean);
    }


}
