package com.atmob.voiceai.module.history;

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.UserVoiceBean;
import com.atmob.voiceai.databinding.ItemHistoryCloneVoiceBinding;

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

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


    private final AsyncListDiffer<UserVoiceBean> listDiffer;

    private final LifecycleOwner lifecycleOwner;

    private ActionHandler actionHandler;

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

            @Override
            public boolean areContentsTheSame(@NonNull UserVoiceBean oldItem, @NonNull UserVoiceBean newItem) {
                return Objects.equals(oldItem.getVoiceAvatar(), newItem.getVoiceAvatar())
                        && Objects.equals(oldItem.getVoiceName(), newItem.getVoiceName())
                        && Objects.equals(oldItem.getContent(), newItem.getContent())
                        && Objects.equals(oldItem.getCreateTime(), newItem.getCreateTime());
            }
        });
    }

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

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

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

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

    public void submitList(List<UserVoiceBean> list, Runnable runnable) {
        listDiffer.submitList(list, runnable);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemHistoryCloneVoiceBinding binding;

        public ViewHolder(@NonNull ItemHistoryCloneVoiceBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.setLifecycleOwner(lifecycleOwner);
            binding.setItemClick(v -> {
                if (actionHandler != null) {
                    UserVoiceBean bean = binding.getHistoryBean();
                    if (bean != null) {
                        actionHandler.onItemClicked(bean);
                    }
                }
            });
        }

        public void bind(UserVoiceBean cloneHistoryBean) {
            binding.setHistoryBean(cloneHistoryBean);
        }
    }


    public interface ActionHandler {
        void onItemClicked(@NonNull UserVoiceBean cloneHistoryBean);
    }
}
