package com.fusi24.entryPermitScanner.adapter;

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

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

import com.fusi24.entryPermitScanner.R;
import com.fusi24.entryPermitScanner.data.entity.DataGroup;
import com.fusi24.entryPermitScanner.data.entity.DataProblems;
import com.fusi24.entryPermitScanner.databinding.RowGroupBinding;

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

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

    private Context context;
    private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
    private List<DataGroup> groupList;
    private List<DataProblems> problemsList;

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

    private void setGroupList(List<DataGroup> groupList) {
        this.groupList = groupList;
    }

    public void updateGroupList(List<DataGroup> groupList){
        setGroupList(groupList);
    }

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

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

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

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


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

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

        DataGroup group = groupList.get(position);
        holder.binding.tvGroupName.setText(group.getName());

        // Create layout manager with initial prefetch item count
        LinearLayoutManager layoutManager = new LinearLayoutManager(
                holder.binding.rvSubGroup.getContext(),
                LinearLayoutManager.VERTICAL, false);

        layoutManager.setInitialPrefetchItemCount(problemsList.size());

        //Create Sub Item View Adapter
        ProblemDocumentAdapter documentAdapter = new ProblemDocumentAdapter(context, problemsList);
        holder.binding.rvSubGroup.setLayoutManager(layoutManager);
        holder.binding.rvSubGroup.setAdapter(documentAdapter);
        holder.binding.rvSubGroup.setRecycledViewPool(viewPool);

        holder.binding.rlGroup.setOnClickListener(v -> {
            documentAdapter.getFilter().filter(String.valueOf(group.getId()));
            // Set Layout
            if(holder.binding.rvSubGroup.getVisibility() == View.GONE){
                holder.binding.rvSubGroup.setVisibility(View.VISIBLE);
                holder.binding.ivChevron.setImageDrawable(context.getDrawable(R.drawable.chevron_up));
            } else {
                holder.binding.rvSubGroup.setVisibility(View.GONE);
                holder.binding.ivChevron.setImageDrawable(context.getDrawable(R.drawable.chevron_down));
            }
        });
    }

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