package com.atmob.voiceai.module.result;

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.VoiceListBean;
import com.atmob.voiceai.databinding.ItemVoiceAiListBinding;
import com.atmob.voiceai.databinding.ItemVoiceAiResultRecommendBinding;
import com.atmob.voiceai.databinding.ItemVoiceListFootBinding;

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

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

    private final AsyncListDiffer<VoiceListBean> listDiffer;

    private final LifecycleOwner lifecycleOwner;


    private ActionHandler actionHandler;

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

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

            @Override
            public boolean areContentsTheSame(@NonNull VoiceListBean oldItem, @NonNull VoiceListBean newItem) {
                if (!Objects.equals(oldItem.getName(), newItem.getName())) {
                    return false;
                }
                if (!Objects.equals(oldItem.getAvatarUrl(), newItem.getAvatarUrl())) {
                    return false;
                }
                return true;
            }
        });
    }

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


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

    public void submit(@NonNull List<VoiceListBean> itemBeanList) {
        listDiffer.submitList(itemBeanList);
    }

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


    public class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemVoiceAiResultRecommendBinding binding;

        public ViewHolder(@NonNull ItemVoiceAiResultRecommendBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.setLifecycleOwner(lifecycleOwner);
            binding.setChoiceClick(v -> {
                if (actionHandler != null) actionHandler.voiceItemClick(binding.getBean());
            });
        }


        public void bind(VoiceListBean itemBean) {
            binding.setBean(itemBean);
        }
    }

    public interface ActionHandler {

        void voiceItemClick(VoiceListBean voiceListBean);
    }

}
