package com.fusi24.rfid.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.fusi24.rfid.R;
import com.fusi24.rfid.data.model.DataProblems;
import com.fusi24.rfid.util.DateHelper;
import com.google.gson.Gson;

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

import butterknife.BindView;
import butterknife.ButterKnife;

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

    private Context context;
    private List<DataProblems> problemsList;
    private Gson gson = new Gson();

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

    public List<DataProblems> getProblemsList() {
        return problemsList;
    }

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

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

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_document_name)
        TextView tvDocumentName;
        @BindView(R.id.tv_document_status)
        TextView tvDocumentStatus;
        @BindView(R.id.tv_document_date)
        TextView tvDocumentDate;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    @NonNull
    @Override
    public ScanResultAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context)
                .inflate(R.layout.row_document, parent, false);
        return new ViewHolder(itemView);
    }

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

        System.out.println("DEVELOPER 3 : " + gson.toJson(problemsList));

        DataProblems problems = problemsList.get(position);

        System.out.println("DEVELOPER 4 : " + gson.toJson(problems));

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

    }

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