package com.module.backstage.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.module.backstage.databinding.BackstageItemErrorstatBinding
import com.module.database.room.entity.WarringBean
import java.text.SimpleDateFormat
import java.util.Date

class ErrorOldAdapter(var productList: MutableList<WarringBean>) :
    RecyclerView.Adapter<ErrorOldAdapter.MyViewHolder>() {
    inner class MyViewHolder(binding: BackstageItemErrorstatBinding) :
        RecyclerView.ViewHolder(binding.root) {
        private val mBinding = binding
        fun bind(warringBean: WarringBean) {
            mBinding.run {
                if (warringBean.time == 0.toLong()) {
                    tvTime.text = "时间"
                    tvThingError.text = warringBean.thingError
                    tvRemark.text = warringBean.remark
                    return
                }
                var formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                val curDate = Date(warringBean.time)
                val time = formatter.format(curDate)
                tvTime.text = "$layoutPosition. $time"
                tvThingError.text = warringBean.thingError
                tvRemark.text = warringBean.remark
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ErrorOldAdapter.MyViewHolder {
        val projectVH = MyViewHolder(
            BackstageItemErrorstatBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )

        return projectVH
    }

    override fun onBindViewHolder(holder: ErrorOldAdapter.MyViewHolder, position: Int) {
        if (position == 0) {
            holder.bind(
                WarringBean(
                    time = 0L, thingError = "错误事件", remark = "备注"
                )
            )

        } else {
            holder.bind(productList.get(position - 1))

        }

    }

    override fun getItemCount(): Int {
        return productList.size + 1
    }
}