package com.datarecovery.master.module.example;

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.datarecovery.master.data.api.bean.ExampleBean;
import com.datarecovery.master.databinding.ItemExampleBinding;

import java.util.List;

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

    private final AsyncListDiffer<ExampleBean> listDiffer;
    private final LifecycleOwner lifecycleOwner;

    private OnItemClickListener onItemClickListener;

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

            @Override
            public boolean areContentsTheSame(@NonNull ExampleBean oldItem, @NonNull ExampleBean newItem) {
                return false;
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

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


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

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

    public void submit(List<ExampleBean> itemBeanList) {
        listDiffer.submitList(itemBeanList);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemExampleBinding binding;

        public ViewHolder(@NonNull ItemExampleBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            this.binding.setLifecycleOwner(lifecycleOwner);
            binding.getRoot().setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(binding.getBean());
                }
            });
        }

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


    public interface OnItemClickListener {
        void onItemClick(ExampleBean itemBean);
    }

}
