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

import android.net.Uri;
import android.view.LayoutInflater;
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.databinding.ContactViewHolderBinding;
import com.app.simplitend.welcome.welcomepatient.fragments.contacts.mvvm.Contact;

import java.util.ArrayList;

public class ContactListAdapter extends ListAdapter<ArrayList<Contact>, ContactListAdapter.ContactViewHolder> {

    private OnContactClickListener contactClickListener;

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

        @Override
        public boolean areContentsTheSame(@NonNull ArrayList<Contact> oldItem, @NonNull ArrayList<Contact> newItem) {
            return false;
        }
    };

    public ContactListAdapter(){
        super(DIFF_UTIL);
    }

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

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

        ArrayList<Contact> contacts = getItem(position);

        holder.binding.card.setOnClickListener(v -> {
            if (contactClickListener != null && contacts != null){
                Contact contact = new Contact(contacts);

                contactClickListener.onClick(contact);
            }
        });

        // below code is about showing a dropdown of multiple contact number menu when user clicks on one of the contact

//        if (contacts == null || contacts.isEmpty()) return;
//
//        holder.binding.name.setLifecycleOwner(holder.binding.name.getLifecycleOwner());
//        holder.binding.name.setDismissWhenNotifiedItemSelected(true);
//        holder.binding.name.setIsFocusable(true);
//
//        if (contacts.size() > 1){
//            // need to show dropdown menu
//            ArrayList<String> phone_list = new ArrayList<>();
//
//            for (Contact contact :
//                    contacts) {
//                if (contact.first_name == null) return;
//
//                phone_list.add(contact.phone_number);
//            }
//
//            holder.binding.name.setItems(phone_list);
//
//        }
//
//        holder.binding.name.setOnClickListener(v -> {
//            if (contactClickListener != null){
//
//                if (contacts.size() == 1){
//                    // only one item
//                    contactClickListener.onClick(contacts.get(0));
//                }else{
//                    // more than one item
//                    // thus, show dropdown menu
//                    holder.binding.name.show();
//                }
//
//            }
//        });
//
//        holder.binding.name.setOnSpinnerItemSelectedListener((OnSpinnerItemSelectedListener<String>) (i, s, i1, t1) -> {
//
//            if (i1 >= contacts.size()) return;
//
//            Contact contact = contacts.get(i1);
//
//            if (contact != null){
//                holder.binding.name.setText(contact.first_name);
//
//                if (contactClickListener != null){
//                    contactClickListener.onClick(contact);
//                }
//            }
//        });
    }

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

    public static class ContactViewHolder extends RecyclerView.ViewHolder{

        private final ContactViewHolderBinding binding;

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

        public void setData(ArrayList<Contact> contacts){
            if (contacts == null || contacts.isEmpty()) return;

            Contact contact = contacts.get(0);

            if (contact.first_name == null || contact.first_name.isEmpty()){
                contact.first_name = "No name";
            }

            binding.name.setText(contact.first_name);
            binding.initial.setText(contact.first_name.toUpperCase());

            if (contact.imageUri != null){
                binding.image.setImageURI(Uri.parse(contact.imageUri));
            }else{
                binding.image.setImageResource(R.drawable.ic_user_filled);
            }
        }

    }

    @FunctionalInterface
    public interface OnContactClickListener{

        void onClick(Contact contact);
    }

}
