package com.fusi24.entryPermitScanner.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;

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

import com.annimon.stream.Stream;
import com.fusi24.entryPermitScanner.data.entity.DataProblems;
import com.fusi24.entryPermitScanner.databinding.RowDocumentBinding;
import com.fusi24.entryPermitScanner.util.DateHelper;

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

public class ProblemDocumentAdapter extends RecyclerView.Adapter<ProblemDocumentAdapter.ViewHolder> implements Filterable {

    private Context context;
    private List<DataProblems> problemsList;
    private List<DataProblems> problemsListFiltered;

    public ProblemDocumentAdapter(Context context, List<DataProblems> problemsList) {
        this.context = context;
        this.problemsList = problemsList;
        this.problemsListFiltered = problemsList;
    }

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

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


    @NonNull
    @Override
    public ProblemDocumentAdapter.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 ProblemDocumentAdapter.ViewHolder holder, int position) {

        DataProblems problems = problemsListFiltered.get(position);

        holder.binding.tvDocumentName.setText(problems.getDataDocumentType().getName());
        holder.binding.tvDocumentStatus.setText(problems.getReason());
        if (problems.getExpiryDate() != null){
            holder.binding.tvDocumentDate.setText(DateHelper.convertStringToMyPattern(problems.getExpiryDate(), "yyyy-MM-dd", "dd MMM yyyy"));
        } else {
            holder.binding.tvDocumentDate.setText("");
        }
    }

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

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                if (TextUtils.isEmpty(constraint.toString())){
                    problemsListFiltered = problemsList;
                } else {
                    Integer idGroup = Integer.valueOf(constraint.toString());
                    List<DataProblems> filterList = new ArrayList<>();

                    for (DataProblems problems : problemsList){
                        if (Stream.of(problemsList).allMatch(v -> v.getDataDocumentType().getGroup() == null)){
                            filterList.add(problems);
                        } else if (problems.getDataDocumentType().getGroup().getId().equals(idGroup)){
                            filterList.add(problems);
                        }
                    }
                    problemsListFiltered = filterList;
                }

                FilterResults results = new FilterResults();
                results.values = problemsListFiltered;
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                problemsListFiltered = (List<DataProblems>) results.values;
                notifyDataSetChanged();
            }
        };
    }

}
