package com.atmob.voiceai.module.subscription;

import android.content.Context;
import android.view.LayoutInflater;
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.atmob.voiceai.data.api.bean.GoodsBean;

import com.atmob.voiceai.databinding.ItemSubGoodsListBinding;

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

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


    private ActionHandler actionHandler;
    private final AsyncListDiffer<GoodsBean> listDiffer;
    private final LifecycleOwner lifecycleOwner;

    private GoodsBean checkedGoodsBean;


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

            @Override
            public boolean areContentsTheSame(@NonNull GoodsBean oldItem, @NonNull GoodsBean newItem) {
                return Objects.equals(oldItem.getName(), newItem.getName()) &&
                        oldItem.getAmount() == newItem.getAmount() &&
                        oldItem.getOriginalAmount() == newItem.getOriginalAmount() &&
                        Objects.equals(oldItem.getDescription(), newItem.getDescription()) &&
                        Objects.equals(oldItem.getContent(), newItem.getContent()) &&
                        oldItem.isPopular() == newItem.isPopular();
            }
        });
    }

    public void setActionHandler(ActionHandler actionHandler) {
        this.actionHandler = actionHandler;
    }

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

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

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

    public void submitList(List<GoodsBean> list) {
        if (list != null && !list.isEmpty()) {
            checkedGoodsBean = list.get(0);
            checkedGoodsBean.setSelect(true);
        } else {
            checkedGoodsBean = null;
        }
        if (actionHandler != null) {
            actionHandler.onItemClick(checkedGoodsBean);
        }
        listDiffer.submitList(list);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ItemSubGoodsListBinding binding;

        public ViewHolder(@NonNull ItemSubGoodsListBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.setLifecycleOwner(lifecycleOwner);
            binding.setOnItemClick(v -> {
                GoodsBean viewBean = binding.getGoodsBean();
                if (viewBean == checkedGoodsBean) {
                    return;
                }
                if (checkedGoodsBean != null) {
                    checkedGoodsBean.setSelect(false);
                }
                checkedGoodsBean = viewBean;
                checkedGoodsBean.setSelect(true);
                if (actionHandler != null) {
                    actionHandler.onItemClick(viewBean);
                }
            });
        }

        public void bind(GoodsBean goodsBean, int position) {
            binding.setShowDivider(position != listDiffer.getCurrentList().size() - 1);
            binding.setGoodsBean(goodsBean);
        }
    }

    public interface ActionHandler {
        void onItemClick(GoodsBean goodsBean);
    }
}
