package com.kaviz.heal.Adapters;

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

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

import com.kaviz.heal.Interfaces.ClickEvents;
import com.kaviz.heal.Model.Pharmacy;
import com.kaviz.heal.R;
import com.kaviz.heal.databinding.PharmacyListDesignBinding;

import java.util.ArrayList;

public class PharmacyAdapter extends RecyclerView.Adapter<PharmacyAdapter.PharmacyViewHolder> {

    private Context context;
    private ArrayList<Pharmacy>pharmacyList;
    private ClickEvents clickEvents;

    public PharmacyAdapter(Context context, ArrayList<Pharmacy> pharmacyList, ClickEvents clickEvents) {
        this.context = context;
        this.pharmacyList = pharmacyList;
        this.clickEvents = clickEvents;
    }

    @NonNull
    @Override
    public PharmacyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new PharmacyViewHolder(LayoutInflater.from(context).inflate(R.layout.pharmacy_list_design, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PharmacyViewHolder holder, int position) {

        Pharmacy pharmacy = pharmacyList.get(position);

        String pharmacyName = "Pharmacy Name : "+pharmacy.getName();
        String distance = "Distance : "+pharmacy.getDistance();
        String availableMedicines = "Available Medicines : "+pharmacy.getAvailable_medicines()
                .replace("[", "").replace("]", "").replace("'", "");


        holder.binding.pharmacyName.setText(pharmacyName);
        holder.binding.distanceText.setText(distance);
        holder.binding.availableMedicinesText.setText(availableMedicines);


    }

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

    public class PharmacyViewHolder extends RecyclerView.ViewHolder {

        PharmacyListDesignBinding binding;

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

            binding = PharmacyListDesignBinding.bind(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    clickEvents.onClick(getAdapterPosition());

                }
            });

        }
    }

}
