package com.nurulwafa.nufakuproject;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_WALI = 0;
    private static final int TYPE_ADMIN = 1;

    private List<ChatModel> chatList;
    private String currentUser; // "wali" or "admin"

    public ChatAdapter(List<ChatModel> chatList, String currentUser) {
        this.chatList = chatList;
        this.currentUser = currentUser;
    }

    @Override
    public int getItemViewType(int position) {
        if (chatList.get(position).getSender().equals(currentUser)) {
            return TYPE_WALI;
        } else {
            return TYPE_ADMIN;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_WALI) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_right, parent, false);
            return new WaliViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_left, parent, false);
            return new AdminViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ChatModel chat = chatList.get(position);
        if (holder instanceof WaliViewHolder) {
            ((WaliViewHolder) holder).message.setText(chat.getMessage());
        } else {
            ((AdminViewHolder) holder).message.setText(chat.getMessage());
        }
    }

    @Override
    public int getItemCount() {
        return chatList.size();
    }

    static class WaliViewHolder extends RecyclerView.ViewHolder {
        TextView message;

        public WaliViewHolder(View itemView) {
            super(itemView);
            message = itemView.findViewById(R.id.textMessageRight);
        }
    }

    static class AdminViewHolder extends RecyclerView.ViewHolder {
        TextView message;

        public AdminViewHolder(View itemView) {
            super(itemView);
            message = itemView.findViewById(R.id.textMessageLeft);
        }
    }
}

