package com.younki.maidmatching.services.Adapter;

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

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

import com.squareup.picasso.Picasso;
import com.younki.maidmatching.R;
import com.younki.maidmatching.services.ApiClient;
import com.younki.maidmatching.utils.OnUserClickListener;

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

    UserData[] userData;
    Context context;
    OnUserClickListener onUserClickListener;

    public MaidAdapter(UserData[] userData, Context context, OnUserClickListener onUserClickListener) {
        this.userData = userData;
        this.context = context;
        this.onUserClickListener = onUserClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        View view = layoutInflater.inflate(R.layout.adapter_maid_list,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final Context context = holder.itemView.getContext();
        final UserData userDataList = userData[position];
        holder.username.setText(userDataList.getUsername());
        holder.name.setText(userDataList.getName());
        holder.email.setText(userDataList.getEmail());
        holder.skill.setText("Skill: "+userDataList.getSkill().size());
        Picasso.get().load(ApiClient.getBaseUrl()+userDataList.getProfile().getImage()).into(holder.photo);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onUserClickListener != null) {
                    onUserClickListener.onClickedUserData(userDataList);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return userData.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView username, name, email, skill;
        ImageView photo;
        int userId;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.username);
            name = itemView.findViewById(R.id.name);
            email = itemView.findViewById(R.id.email);
            skill = itemView.findViewById(R.id.skill);
            photo = itemView.findViewById(R.id.photo);
        }
    }
}
