package com.datarecovery.master.module.imgrecover;

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.utils.ImageDeepDetector;

import java.util.List;

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


    private List<ImageDeepDetector.ImageFile> list;
    private final LifecycleOwner lifecycleOwner;
    private final int category;
    private int size;

    private onItemClick onItemClick;

    public ImageItemAdapter(int category, LifecycleOwner lifecycleOwner) {
        this.category = category;
        this.lifecycleOwner = lifecycleOwner;
    }

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

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

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

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

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        ItemDataImgBinding binding;

        public ViewHolder(@NonNull ItemDataImgBinding 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(ImageDeepDetector.ImageFile imageFile) {
            binding.setFile(imageFile);
        }
    }

    public interface onItemClick {
        void onCheck(ImageDeepDetector.ImageFile imageFile);

        void onItemClick(ImageDeepDetector.ImageFile imageFile);
    }
}
