package com.datarecovery.master.module.filerecover.fragment;

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.ItemDataFileBinding;
import com.datarecovery.master.sdk.bugly.BuglyHelper;
import com.datarecovery.master.utils.FileUtil;
import com.datarecovery.master.utils.FilesSearch;

import java.util.List;

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

    private final int fileType;
    private List<FilesSearch.DocumentFile> list;

    private final LifecycleOwner lifecycleOwner;


    private int size;

    private onItemClick onItemClick;

    public FileRecoverAdapter(LifecycleOwner lifecycleOwner, int fileType) {
        this.lifecycleOwner = lifecycleOwner;
        this.fileType = fileType;
    }


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


    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);
    }

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

    @Override
    public void onBindViewHolder(@NonNull FileRecoverAdapter.ViewHolder holder, int position) {
        if (position < 0 || position >= list.size()) {
            BuglyHelper.postCatchedException(new IndexOutOfBoundsException("文件列表onBindViewHolder出现数组越界：" + position + "，size：" + list.size()));
            return;
        }
        holder.bind(list.get(position));
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemDataFileBinding binding;

        public ViewHolder(@NonNull ItemDataFileBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            binding.setLifecycleOwner(lifecycleOwner);
            binding.ivFileType.setImageDrawable(FileUtil.getFileRecoverIcon(fileType));
            binding.getRoot().setOnClickListener(v -> {
                if (onItemClick != null) {
                    onItemClick.onCheck(binding.getFile());
                }
            });
        }

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

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