package com.datarecovery.master.module.videorecover;

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.ItemDataImgBinding;
import com.datarecovery.master.databinding.ItemDataVideoBinding;
import com.datarecovery.master.utils.FilesSearch;
import com.datarecovery.master.utils.ImageDeepDetector;

import java.util.List;

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


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

    private onItemClick onItemClick;

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

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

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

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

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

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        ItemDataVideoBinding binding;

        public ViewHolder(@NonNull ItemDataVideoBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.setLifecycleOwner(lifecycleOwner);
            binding.setCheckBoxClick(v -> {
                if (onItemClick != null) {
                    onItemClick.onCheck(binding.getFile());
                }
            });
            binding.getRoot().setOnClickListener(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);
    }
}
