package com.dowerx.quack.fragment;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.dowerx.quack.R;
import com.dowerx.quack.activity.SwitcherActivity;
import com.dowerx.quack.databinding.FragmentUserBinding;
import com.dowerx.quack.model.User;

import java.util.List;

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

    private final List<User> mValues;
    private final SwitcherActivity activity;

    public UserRecyclerViewAdapter(List<User> items, SwitcherActivity activity) {
        this.mValues = items;
        this.activity = activity;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(FragmentUserBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.username.setText(holder.mItem.getUsername());
        holder.profile_button.setOnClickListener(v -> {
            activity.setUser(holder.mItem.getId());
            activity.findViewById(R.id.nav_profile).performClick();
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView username;
        public final Button profile_button;
        public User mItem;

        public ViewHolder(FragmentUserBinding binding) {
            super(binding.getRoot());
            username = binding.username;
            profile_button = binding.profileButton;
        }

        @Override
        public String toString() {
            return super.toString() + " '" + username.getText() + "'";
        }
    }
}