package com.fusi24.rfid.adapter;

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

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

import com.fusi24.rfid.data.entity.DataProblems;
import com.fusi24.rfid.databinding.RowDocumentBinding;
import com.fusi24.rfid.util.DateHelper;

import java.util.ArrayList;
import java.util.List;

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

    private Context context;
    private List<DataProblems> problemsList;

    public ScanResultAdapter(Context context) {
        this.context = context;
        this.problemsList = new ArrayList<>();
    }

    private void setProblemsList(List<DataProblems> problemsList) {
        this.problemsList = problemsList;
    }

    public void updateProblemList(List<DataProblems> problemsList){
        setProblemsList(problemsList);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private RowDocumentBinding binding;

        ViewHolder(RowDocumentBinding itemBinding) {
            super(itemBinding.getRoot());
            this.binding = itemBinding;
        }
    }


    @NonNull
    @Override
    public ScanResultAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RowDocumentBinding itemBinding = RowDocumentBinding.inflate(LayoutInflater.from(context), parent, false);
        return new ViewHolder(itemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull ScanResultAdapter.ViewHolder holder, int position) {

        DataProblems problems = problemsList.get(position);

        holder.binding.tvDocumentName.setText(problems.getDataDocumentType().getName());
        holder.binding.tvDocumentStatus.setText(problems.getReason());
        holder.binding.tvDocumentDate.setText(DateHelper.convertDateToReadableDate(problems.getDataDocumentType().getUpdateDate()));

    }

    @Override
    public int getItemCount() {
        return problemsList.size();
    }
}
