package com.kaixed.kchat.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kaixed.kchat.R;
import com.kaixed.kchat.database.entity.ChatLists;
import com.kaixed.kchat.view.activity.ChatActivity;
import com.kaixed.kchat.view.i.OnChatListItemClickListener;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * @Author: kaixed
 * @Date: 2024/5/20 10:13
 * @Description: TODO
 */
public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.MyViewHolder> {
    private final List<ChatLists> chatLists;
    private final Context mContext;

    private OnChatListItemClickListener onChatListItemClickListener;

    public void setItemListener(OnChatListItemClickListener i) {
        this.onChatListItemClickListener = i;
    }

    public ChatListAdapter(List<ChatLists> chatLists, Context mContext) {
        this.chatLists = chatLists;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.chat_main_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.bindData(chatLists.get(position));
        holder.itemView.setOnClickListener(v -> {
            onChatListItemClickListener.onItemClick(chatLists.get(position).getTalkerId());
            Intent intent = new Intent(mContext, ChatActivity.class);
            intent.putExtra("friendId", chatLists.get(position).getTalkerId());
            mContext.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return chatLists.isEmpty() ? 0 : chatLists.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private final TextView mTvContent;
        private final TextView mTvNickname;
        private final TextView mTvTimestamp;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            mTvContent = itemView.findViewById(R.id.tv_content);
            mTvNickname = itemView.findViewById(R.id.tv_nickname);
            mTvTimestamp = itemView.findViewById(R.id.tv_timestamp);
        }

        private void bindData(ChatLists chatList) {
            mTvContent.setText(chatList.getLastContent());
            mTvNickname.setText(chatList.getTalkerId());

            Instant instant = Instant.ofEpochMilli(chatList.getTimestamp());
            // 将时间戳转换为当前系统默认时区的时间
            ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
            // 格式化成小时：分钟格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
            String formattedTime = zdt.format(formatter);
            mTvTimestamp.setText(formattedTime);
        }
    }

}
