package com.abbidot.tracker.adapter

import android.content.Context
import android.text.TextUtils
import android.text.format.DateUtils
import android.view.View
import androidx.core.content.ContextCompat
import com.abbidot.baselibrary.list.BaseRecyclerAdapter
import com.abbidot.baselibrary.list.RecyclerViewHolder
import com.abbidot.baselibrary.util.Utils
import com.abbidot.tracker.R
import com.abbidot.tracker.bean.MessageBean
import com.abbidot.tracker.widget.TypefaceRoundButton

/**
 *Created  by .yzq on 2023/12/26/026.
 * @link
 * @description:
 */
class NotificationV2Adapter(
    ctx: Context, list: MutableList<MessageBean>?
) : BaseRecyclerAdapter<MessageBean>(ctx, list) {

    override fun getEmptyLayoutId(viewType: Int) = 0

    override fun getItemLayoutId(viewType: Int) = R.layout.item_home_route_notification

    override fun bindData(holder: RecyclerViewHolder?, position: Int, item: MessageBean) {
        val roundButton =
            holder!!.getView(R.id.iv_home_route_notification_state) as TypefaceRoundButton
        val textColor = if (item.isRead) {
            roundButton.visibility = View.GONE
            R.color.black70
        } else {
            roundButton.visibility = View.VISIBLE
            R.color.data_black_color
        }
        holder.getTextView(R.id.tv_home_route_notification_content).apply {
            text = item.message
            if (item.isSelect) {
                ellipsize = null
                isSingleLine = false
            } else {
                ellipsize = TextUtils.TruncateAt.END
                isSingleLine = true
            }
            setTextColor(ContextCompat.getColor(mContext, textColor))
        }
        holder.getTextView(R.id.tv_home_route_notification_type).apply {
            text = item.title
            setTextColor(ContextCompat.getColor(mContext, textColor))
        }
        holder.getTextView(R.id.tv_home_route_notification_date).apply {
            text = getRelativeTimeString(Utils.stringToTimestamp(item.sendTime))
            setTextColor(ContextCompat.getColor(mContext, textColor))
        }
    }

    private fun getRelativeTimeString(time: Long): String {
        var formatDateStr = ""
        val now = System.currentTimeMillis()
        formatDateStr = if (DateUtils.isToday(time)) {
            mContext.getString(R.string.txt_today) + " " + Utils.formatTime(
                time, Utils.DATE_FORMAT_PATTERN_EN8
            )
        } else if (now - time < (48 * 60 * 60 * 1000)) {
            mContext.getString(R.string.txt_yesterday)
        } else {
            Utils.formatTime(time, Utils.DATE_FORMAT_PATTERN_EN9)
        }
        return formatDateStr
    }
}