package com.app.simplitend.chats;

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.chats.mvvm.ChatItem;
import com.app.simplitend.databinding.ChatCardViewholderBinding;

public class ChatListAdapter extends ListAdapter<ChatItem, ChatListAdapter.ChatCardViewHolder> {

    private OnChatItemClickListener chatItemClickListener;

    private static final DiffUtil.ItemCallback<ChatItem> DIFF_UTIL = new DiffUtil.ItemCallback<ChatItem>() {
        @Override
        public boolean areItemsTheSame(@NonNull ChatItem oldItem, @NonNull ChatItem newItem) {
            // TODO: 11-07-2023
            return false;
        }

        @Override
        public boolean areContentsTheSame(@NonNull ChatItem oldItem, @NonNull ChatItem newItem) {
            // TODO: 11-07-2023
            return false;
        }
    };

    public ChatListAdapter(){
        super(DIFF_UTIL);
    }

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

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

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

    public static class ChatCardViewHolder extends RecyclerView.ViewHolder{

        ChatCardViewholderBinding binding;

        public ChatCardViewHolder(ChatCardViewholderBinding binding){
            super(binding.getRoot());
            this.binding = binding;
        }

        public void setData(ChatItem chatItem, int position){
            binding.type.setText(chatItem.type);
            binding.time.setText(chatItem.time);
            binding.name.setText(chatItem.name);
            binding.lastMsg.setText(chatItem.last_message);
        }

    }

    // setters

    public void setChatItemClickListener(OnChatItemClickListener chatItemClickListener) {
        this.chatItemClickListener = chatItemClickListener;
    }


    // interfaces

    @FunctionalInterface
    public interface OnChatItemClickListener{
        void onChatItemClick(ChatItem chatItem, int position);
    }

}
