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.constant.ConstantInt
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 == ConstantInt.Type1) {
            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(item.timeStamp)
            setTextColor(ContextCompat.getColor(mContext, textColor))
        }
    }

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