package com.project.ilsaenterprise.adapter;

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

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.project.ilsaenterprise.R;
import com.project.ilsaenterprise.models.Attendance;
import com.project.ilsaenterprise.models.ListGroup;
import com.project.ilsaenterprise.models.MonitoringListModel;
import com.squareup.picasso.Picasso;

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

public class GroupListAdapter extends RecyclerView.Adapter<GroupListAdapter.GroupViewHolder> {
    private Context context;
    private ArrayList<ListGroup> listGroups;
    private GroupListAdapter.onItemClickListener clickListener;

    public interface onItemClickListener{
        void onItemClick(View view, int position);
    }

    public GroupListAdapter(Context context, ArrayList<ListGroup> listGroups, onItemClickListener clickListener) {
        this.context = context;
        this.listGroups = listGroups;
        this.clickListener = clickListener;
    }

    public class GroupViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView groupName, countMessages;

        public GroupViewHolder(@NonNull View itemView) {
            super(itemView);

            groupName = itemView.findViewById(R.id.tvGroupName);
            countMessages = itemView.findViewById(R.id.tvCount);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            clickListener.onItemClick(v, getAdapterPosition());
        }

    }

    @NonNull
    @Override
    public GroupViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_chat, parent, false);

        return new GroupViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull GroupViewHolder holder, int position) {
        ListGroup currentItem = listGroups.get(position);

//        String imgUrl = currentItem.getImageUrl();
//        Picasso.get().load(imgUrl).into(holder.imageData);
//        if(currentItem.getImgLink() != null){
//            String imgProfile = currentItem.getImgLink();
//            RequestOptions options = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL);
//            Glide.with(context).load(imgProfile).thumbnail(0.25f).apply(options).into(holder.userImage);
//        }
//        else
//            holder.userImage.setImageResource(R.drawable.user_farmer);
        holder.groupName.setText(currentItem.getGroupName());
        holder.countMessages.setText(currentItem.getCountMessages());

    }

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


}
