package com.phlx.wool.ui.search.adapter;

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

import androidx.recyclerview.widget.RecyclerView;


import com.phlx.wool.R;
import com.phlx.wool.entity.Veterinary;
import com.phlx.wool.ui.search.interf.IOnVeterinaryItemClickListener;

import java.util.ArrayList;

public class VeterinaryAdapter extends RecyclerView.Adapter<VeterinaryAdapter.MyViewHolder> implements View.OnClickListener{

    private Context context;

    private ArrayList<Veterinary> veterinaries = new ArrayList<>();

    public VeterinaryAdapter(Context context, ArrayList<Veterinary> list) {
        this.context = context;
        this.veterinaries = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).
                inflate(R.layout.item_search, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.name.setText(veterinaries.get(position).getVeterinaryName());
        holder.phone.setText(veterinaries.get(position).getVeterinaryPhone());
        holder.itemView.setTag(position);
    }

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

    @Override
    public void onClick(View v) {
        if (onVeterinaryItemClickListener != null && veterinaries.size() > 0) {
            onVeterinaryItemClickListener.onItemClick(veterinaries.get((Integer) v.getTag()));
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView name;
        TextView phone;

        public MyViewHolder(View view) {
            super(view);
            name = (TextView) view.findViewById(R.id.name_tv);
            phone = (TextView) view.findViewById(R.id.phone_tv);
        }
    }

    private IOnVeterinaryItemClickListener onVeterinaryItemClickListener;

    public void setOnItemClickListener(IOnVeterinaryItemClickListener onVeterinaryItemClickListener) {
        this.onVeterinaryItemClickListener = onVeterinaryItemClickListener;
    }

}
