package com.atmob.voiceai.module.voiceai;

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.ItemVoiceListFootBinding;

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

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

    private final AsyncListDiffer<VoiceListBean> listDiffer;

    private final LifecycleOwner lifecycleOwner;

    public static final int TYPE_FOOT = 3;

    private ActionHandler actionHandler;

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

    public VoiceAIListAdapter(@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 (oldItem.getViewType() != newItem.getViewType()) {
                    return false;
                }
                if (oldItem.getVoicePlayState() != newItem.getVoicePlayState()) {
                    return false;
                }
                if (oldItem.isCheck() != newItem.isCheck()) {
                    return false;
                }
                if (oldItem.isAddIcon() != newItem.isAddIcon()) {
                    return false;
                }
                if (!Objects.equals(oldItem.getName(), newItem.getName())) {
                    return false;
                }
                if (oldItem.isHasNew() != newItem.isHasNew()) {
                    return false;
                }
                if (oldItem.isHasPro() != newItem.isHasPro()) {
                    return false;
                }
                if (!Objects.equals(oldItem.getAvatarUrl(), newItem.getAvatarUrl())) {
                    return false;
                }
                return true;
            }
        });
    }

    public int getBeanPosition(@NonNull VoiceListBean bean) {
        return listDiffer.getCurrentList().indexOf(bean);
    }

    public void notify(@NonNull VoiceListBean bean) {
        int position = listDiffer.getCurrentList().indexOf(bean);
        if (position == -1) {
            return;
        }
        notifyItemChanged(position);
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        if (viewType == TYPE_FOOT) {
            return new ViewFootHolder(ItemVoiceListFootBinding.inflate(layoutInflater));
        } else {
            return new ViewHolder(ItemVoiceAiListBinding.inflate(layoutInflater));
        }
    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        VoiceListBean voiceListBean = listDiffer.getCurrentList().get(position);
        if (voiceListBean.getViewType() != TYPE_FOOT) {
            ((ViewHolder) holder).bind(voiceListBean);
        }
    }

    public void submit(@NonNull List<VoiceListBean> itemBeanList, Runnable runnable) {
        VoiceListBean voiceListBean = new VoiceListBean();
        voiceListBean.setViewType(TYPE_FOOT);
        itemBeanList.add(voiceListBean);
        listDiffer.submitList(itemBeanList, runnable);
    }

    @Override
    public int getItemViewType(int position) {
        VoiceListBean voiceListBean = listDiffer.getCurrentList().get(position);
        if (voiceListBean.getViewType() == TYPE_FOOT) {
            return TYPE_FOOT;
        }
        return super.getItemViewType(position);
    }

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


    public class ViewFootHolder extends RecyclerView.ViewHolder {

        private final ItemVoiceListFootBinding binding;

        public ViewFootHolder(@NonNull ItemVoiceListFootBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemVoiceAiListBinding binding;

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


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

    public interface ActionHandler {
        void addClick();

        void voiceItemClick(VoiceListBean voiceListBean);
    }

}
