package com.abbidot.tracker.adapter

import android.content.Context
import android.text.format.DateUtils
import android.view.View
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

/**
 *Created  by .yzq on 2026/4/10/周五.
 * @link
 * @description:
 */
class HistoryFenceAdapter(
    ctx: Context, list: MutableList<MessageBean>?
) : BaseRecyclerAdapter<MessageBean>(ctx, list) {
    override fun getEmptyLayoutId(viewType: Int) = 0

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

    override fun bindData(holder: RecyclerViewHolder?, position: Int, item: MessageBean) {
        holder?.apply {
            getImageView(R.id.iv_history_fence_type_image).let {
                when (item.deviceMessageType) {
                    ConstantInt.Type1, ConstantInt.Type3 -> it.setImageResource(R.drawable.icon_history_danger_fence_image)
                    ConstantInt.Type2 -> it.setImageResource(R.drawable.icon_history_save_fence_image)
                    ConstantInt.Type4 -> it.setImageResource(R.drawable.icon_history_no_save_fence_image)
                }
            }
            getTextView(R.id.tv_history_fence_type_time).text =
                getRelativeTimeString(item.timeStamp)
            getTextView(R.id.tv_history_fence_type_content).text = item.message
            getImageView(R.id.iv_history_fence_type_line).let {
                it.visibility = if (position == getData().size - 1) View.GONE
                else View.VISIBLE
            }
        }
    }

    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_EN15
            )
        }
        return formatDateStr
    }
}