package com.datarecovery.master.module.preview;

import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import com.datarecovery.master.databinding.ItemPreviewImgBinding;
import com.datarecovery.master.utils.ImageDeepDetector;

import java.util.List;

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

    private final List<ImageDeepDetector.ImageFile> imageFileList;

    public PreviewImagePagerAdapter(List<ImageDeepDetector.ImageFile> imageFileList) {
        this.imageFileList = imageFileList;
    }

    @NonNull
    @Override
    public PreviewImagePagerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemPreviewImgBinding imgBinding = ItemPreviewImgBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(imgBinding);
    }

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

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


    public class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemPreviewImgBinding binding;

        public ViewHolder(@NonNull ItemPreviewImgBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void bind(ImageDeepDetector.ImageFile imageFile) {
            binding.previewImg.setImageURI(imageFile.getUri());
        }
    }
}
