package com.datarecovery.master.module.audiorecover;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.datarecovery.master.databinding.ItemDataAudioBinding;
import com.datarecovery.master.utils.FilesSearch;

import java.util.List;

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


    private List<FilesSearch.DocumentFile> list;
    private final LifecycleOwner lifecycleOwner;
    private int size;

    private AudioItemAdapter.onItemClick onItemClick;

    public AudioItemAdapter(LifecycleOwner lifecycleOwner) {
        this.lifecycleOwner = lifecycleOwner;
    }

    public void setOnItemClick(onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

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

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

    public void submit(List<FilesSearch.DocumentFile> videoList) {
        if (videoList == null) {
            return;
        }
        int index;
        if (list != null) {
            index = videoList.size() - size;
        } else {
            index = videoList.size();
        }
        this.size = videoList.size();
        this.list = videoList;
        notifyItemRangeInserted(0, index);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<FilesSearch.DocumentFile> videoList) {
        this.list = videoList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return (list == null) ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ItemDataAudioBinding binding;

        public ViewHolder(@NonNull ItemDataAudioBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.setLifecycleOwner(lifecycleOwner);
            binding.setOnCheckClick(v -> {
                if (onItemClick != null) {
                    onItemClick.onCheck(binding.getFile());
                }
            });
            binding.setOnDetailClick(v -> {
                if (onItemClick != null) {
                    onItemClick.onItemClick(binding.getFile());
                }
            });
        }

        public void bind(FilesSearch.DocumentFile file) {
            binding.setFile(file);
        }
    }

    public interface onItemClick {
        void onCheck(FilesSearch.DocumentFile file);

        void onItemClick(FilesSearch.DocumentFile file);
    }
}