package com.qimai.log.ui

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.qimai.log.R
import com.qimai.log.bean.LogUpBean

/**
 * Created by chuxiaoshan on 2021/11/15 14:42
 *
 * Description: 日志渲染adapter
 */
class LogUpAdapter(
    private var dataSet: List<LogUpBean?> = listOf()
) : RecyclerView.Adapter<LogUpAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_log, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = dataSet[position]
        val content = "时间：${TimeUtils.millis2String(item?.l ?: 0, "yyyy-MM-dd HH:mm:ss")}\n" +
                "类型：${item?.f}\n" +
                "线程：${item?.i}  ${item?.n}\n" +
                "内容：\n${item?.c}\n"
        holder.tvContent.text = content
        holder.btnCopy.setOnClickListener {
            ClipboardUtils.copyText(content)
            ToastUtils.showShort("内容已复制到粘贴板")
        }
    }

    override fun getItemCount() = dataSet.size

    @SuppressLint("NotifyDataSetChanged")
    fun setNewData(data: List<LogUpBean?>) {
        if (dataSet === data) return
        dataSet = data
        notifyDataSetChanged()
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val tvContent: TextView = view.findViewById(R.id.tv_content)
        val btnCopy: TextView = view.findViewById(R.id.btn_copy)
    }
}