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

import static com.app.simplitend.welcome.welcomepatient.fragments.contacts.CreateContactFragment.TO_EDIT_KEY;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

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

public class AddContactAdapter extends ListAdapter<ContactData, AddContactAdapter.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 ContactClickListener contactClickListener;

    public AddContactAdapter() {
        super(DIFF_UTIL);
    }

    @NonNull
    @Override
    public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        AddContactViewholderBinding binding = AddContactViewholderBinding.inflate(LayoutInflater.from(parent.getContext()));
        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 void setContactClickListener(ContactClickListener contactClickListener) {
        this.contactClickListener = contactClickListener;
    }

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

        private AlertDialog contactDialog;

        public ContactViewHolder(AddContactViewholderBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            this.contactDialog = new AlertDialog.Builder(itemView.getContext())
                    .setTitle("Add contact")
                    .setMessage("Select an option to add contact")
                    .setPositiveButton("Add manually", ((dialog1, which) -> {

                        // navigating to add contact fragment
                        Bundle bundle = new Bundle();
                        bundle.putBoolean(TO_EDIT_KEY, false);
                        Navigation.findNavController(binding.getRoot()).navigate(R.id.action_addContactFragment_to_createContactFragment, bundle);

                    }))
                    .setNegativeButton("Select contact", ((dialog1, which) -> {
                        Navigation.findNavController(binding.getRoot()).navigate(R.id.action_addContactFragment_to_contactListFragment);
                    }))
                    .create();
        }

        public void setData(ContactData contact, int position) {
            if (contact.id == -1){
                // no contact yet added
                binding.addImeg.setVisibility(View.VISIBLE);
                binding.imag.setVisibility(View.GONE);
                binding.relationship.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.relationship != null && !contact.relationship.trim().isEmpty()){
                    binding.relationship.setVisibility(View.VISIBLE);
                    binding.relationship.setText(contact.relationship);
                }else{
                    binding.relationship.setVisibility(View.GONE);
                }

               if (contact.contact_photo == null || contact.contact_photo.isEmpty()){
                   binding.addImeg.setVisibility(View.VISIBLE);
                   binding.imag.setVisibility(View.GONE);
               }else{
                   binding.addImeg.setVisibility(View.GONE);
                   binding.imag.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.imag);
               }

                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);
               }
            }
        }

    }

    @FunctionalInterface
    public interface ContactClickListener{
        void onContactClick(ContactData contactData, int position);
    }

}
