package com.bifan.txtreaderlib.adapter;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bifan.txtreaderlib.R;
import com.bifan.txtreaderlib.bean.Message;

import java.util.List;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
    private List<Message> messages;
    private Context context;
    private OnRegenerateClickListener onRegenerateClickListener;
    public void setOnRegenerateClickListener(OnRegenerateClickListener listener) {
        this.onRegenerateClickListener = listener;
    }

    public interface OnRegenerateClickListener {
        void onRegenerateClick(int position);
    }

    public MessageAdapter(List<Message> messages, Context context) {
        this.messages = messages;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        Message message = messages.get(position);
        return message.getIsuser();
    }

    @NonNull
    @Override
    public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view;
        if (viewType == 1) {
            view = inflater.inflate(R.layout.item_message_user, parent, false);
        } else {
            view = inflater.inflate(R.layout.item_message_bot, parent, false);
        }
        return new MessageViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull MessageViewHolder holder, @SuppressLint("RecyclerView") int position) {
        Message message = messages.get(position);
        if (message.getIsuser() == 1) { // user
            holder.textViewMessage.setText(message.getContent());
            if(message.getCite()!=""&&message.getCite()!=null){
                Log.d("test",message.getCite());
                holder.messageUser_Cite.setVisibility(View.VISIBLE);
                holder.messageUser_Cite.setText(message.getCite());
            }
        } else { // bot
            holder.textViewMessage.setText(message.getContent());
            // 检查是否为最后一条机器人消息
            if (position == getLastBotMessagePosition()) {
                holder.imageViewRegenerate.setVisibility(View.VISIBLE);
            } else {
                holder.imageViewRegenerate.setVisibility(View.GONE);
            }
            setupClickListeners(holder, message, position);
        }
    }

    // 获取最后一条机器人消息的位置
    public int getLastBotMessagePosition() {
        for (int i = messages.size() - 1; i >= 0; i--) {
            if (messages.get(i).getIsuser() == 0) { // Assuming 0 is the bot
                return i;
            }
        }
        return -1;
    }

    private void setupClickListeners(MessageViewHolder holder, Message message, int position) {
        holder.imageViewCopy.setOnClickListener(v -> {
            copyTextToClipboard(message.getContent());
        });

        holder.imageViewRegenerate.setOnClickListener(view -> {
            if (onRegenerateClickListener != null) {
                onRegenerateClickListener.onRegenerateClick(position);
            }
        });
    }

    private void copyTextToClipboard(String text) {
        ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = ClipData.newPlainText("copiedText", text);
        clipboardManager.setPrimaryClip(clipData);
        Toast.makeText(context, "已复制到剪贴板", Toast.LENGTH_SHORT).show();
    }

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

    static class MessageViewHolder extends RecyclerView.ViewHolder {
        TextView textViewMessage,messageUser_Cite;
        ImageView imageViewCopy,imageViewRegenerate;
        MessageViewHolder(View itemView, int viewType) {
            super(itemView);
            if (viewType == 1) {  // 假设 1 是用户消息
                textViewMessage = itemView.findViewById(R.id.textViewMessageUser);
                messageUser_Cite=itemView.findViewById(R.id.MessageUser_Cite);
            } else {  // 非 1 即机器人消息
                textViewMessage = itemView.findViewById(R.id.textViewMessageBot);
                imageViewCopy = itemView.findViewById(R.id.imageViewCopy);
                imageViewRegenerate=itemView.findViewById(R.id.imageViewRegenerate);
            }
        }
    }
}
