package com.app.simplitend.faqs;

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.databinding.FaqViewholderBinding;

public class FAQAdapter extends ListAdapter<FAQResult, FAQAdapter.FAQViewHolder> {

    private int lastOpenedFaqPosition;

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

        @Override
        public boolean areContentsTheSame(@NonNull FAQResult oldItem, @NonNull FAQResult newItem) {
            return false;
        }
    };

    public FAQAdapter(){
        super(DIFF_UTIL);
        this.lastOpenedFaqPosition = -1;
    }

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

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

        if (position == lastOpenedFaqPosition){
            // this is opened
            holder.binding.addRemove.setImageResource(R.drawable.ic_remove_primary);
            holder.binding.answer.setVisibility(View.VISIBLE);
        }else{
            holder.binding.addRemove.setImageResource(R.drawable.ic_add_primary);
            holder.binding.answer.setVisibility(View.GONE);
        }

        final int cardPosition = position;
        holder.binding.addRemove.setOnClickListener(v -> {
            int visibility = holder.binding.answer.getVisibility();

            if (visibility == View.VISIBLE){
                holder.binding.addRemove.setImageResource(R.drawable.ic_add_primary);
                holder.binding.answer.setVisibility(View.GONE);

                this.lastOpenedFaqPosition = -1;
            }else{
                holder.binding.addRemove.setImageResource(R.drawable.ic_remove_primary);
                holder.binding.answer.setVisibility(View.VISIBLE);

                // closing already opened FAQ
                if (lastOpenedFaqPosition != -1 && lastOpenedFaqPosition < getItemCount()){
                    int lastOpenedFaqPosition = this.lastOpenedFaqPosition;
                    this.lastOpenedFaqPosition = cardPosition;
                    notifyItemChanged(lastOpenedFaqPosition);
                }else{
                    this.lastOpenedFaqPosition = cardPosition;
                }
            }
        });

    }

    public static class FAQViewHolder extends RecyclerView.ViewHolder{
        public FaqViewholderBinding binding;

        public FAQViewHolder(@NonNull FaqViewholderBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public void setDate(FAQResult result){
            if (result == null) return;

            binding.title.setText(result.faq_question);
            binding.answer.setText(result.faq_answer);
            binding.answer.setVisibility(View.GONE);
            binding.addRemove.setImageResource(R.drawable.ic_add_primary);
        }
    }

}
