package com.app.simplitend.welcome.welcomepatient.fragments.contacts.mvvm;

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

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

import com.app.simplitend.R;
import com.app.simplitend.apputils.RetrofitHelper;
import com.app.simplitend.databinding.FiftyContactViewholderBinding;
import com.app.simplitend.welcome.welcomepatient.fragments.contacts.AddContactAdapter;
import com.app.simplitend.welcome.welcomepatient.fragments.contacts.mvvm.models.ContactData;
import com.bumptech.glide.Glide;

public class FiftyContactsAdapter extends ListAdapter<ContactData, FiftyContactsAdapter.ContactViewHolder> {

    private static final DiffUtil.ItemCallback<ContactData> DIFF_UTIL = new DiffUtil.ItemCallback<ContactData>() {
        @Override
        public boolean areItemsTheSame(@NonNull ContactData oldItem, @NonNull ContactData newItem) {
            return oldItem.first_name.equals(newItem.first_name);
        }

        @Override
        public boolean areContentsTheSame(@NonNull ContactData oldItem, @NonNull ContactData newItem) {
            return oldItem.equals(newItem);
        }
    };

    private AddContactAdapter.ContactClickListener contactClickListener;

    public FiftyContactsAdapter() {
        super(DIFF_UTIL);
    }

    public void setContactClickListener(AddContactAdapter.ContactClickListener contactClickListener) {
        this.contactClickListener = contactClickListener;
    }

    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        FiftyContactViewholderBinding binding = FiftyContactViewholderBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ContactViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
        holder.setData(getItem(position), position);

        holder.binding.card.setOnClickListener(v -> {
            if (contactClickListener != null){
                contactClickListener.onContactClick(getItem(position), position);
            }
        });
    }

    public static class ContactViewHolder extends RecyclerView.ViewHolder {
        public FiftyContactViewholderBinding binding;

        public ContactViewHolder(FiftyContactViewholderBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void setData(ContactData contact, int position) {
            if (contact.id == -1){
                // no contact yet added
                binding.addImg.setVisibility(View.VISIBLE);
                binding.image.setVisibility(View.GONE);

                binding.name.setText(itemView.getContext().getString(R.string.add_contact));

                binding.card.setCardBackgroundColor(itemView.getContext().getColor(R.color.color_accent));

                binding.cgIc.setVisibility(View.GONE);
                binding.docIc.setVisibility(View.GONE);
                binding.sos.setVisibility(View.GONE);

            }else{
                binding.name.setText(contact.first_name);

                if (contact.contact_photo == null || contact.contact_photo.isEmpty()){
                    binding.addImg.setVisibility(View.VISIBLE);
                    binding.image.setVisibility(View.GONE);
                }else{
                    binding.addImg.setVisibility(View.GONE);
                    binding.image.setVisibility(View.VISIBLE);

                    Glide.with(itemView.getContext())
                            .load(RetrofitHelper.IMAGE_BASE_URL + contact.contact_photo)
                            .placeholder(android.R.color.darker_gray)
                            .error(R.drawable.ic_contact)
                            .fitCenter().into(binding.image);
                }

                binding.card.setCardBackgroundColor(itemView.getContext().getColor(R.color.white_bg));

                if (contact.care_giver_data.id != null){
                    // this contact is caregiver
                    binding.cgIc.setVisibility(View.VISIBLE);
                }else{
                    binding.cgIc.setVisibility(View.GONE);
                }

                if (contact.is_sos != null && contact.is_sos.equals("1")){
                    binding.sos.setVisibility(View.VISIBLE);
                }else{
                    binding.sos.setVisibility(View.GONE);
                }

                if (contact.is_doctor != null && contact.is_doctor.equals("1")){
                    binding.docIc.setVisibility(View.VISIBLE);
                }else{
                    binding.docIc.setVisibility(View.GONE);
                }
            }
        }

    }
}
