package com.example.chemical.utils

import android.os.Handler
import com.rc.core.log.RcLog
import java.text.SimpleDateFormat
import java.util.*

class TimeUpdater(private val handler: Handler, private val timeUpdatedListener: (String) -> Unit) {

    private val dateFormat = SimpleDateFormat("MM月dd日 EEEE HH:mm", Locale.getDefault())
    private val updateInterval: Long = 1000 // 1秒钟

    private val updateRunnable = object : Runnable {
        override fun run() {
            val formattedDateTime =  getFormattedDateTime()
            // 调用回调函数，传递时间数据
            timeUpdatedListener("$formattedDateTime")
            handler.postDelayed(this, updateInterval)
        }
    }

    fun startUpdating() {
        handler.postDelayed(updateRunnable, updateInterval)
    }

    fun stopUpdating() {
        RcLog.info("==========时间显示器关闭过")
        handler.removeCallbacks(updateRunnable)
    }


    fun getFormattedDateTime(): String {
        val calendar = Calendar.getInstance()
        val dateFormat = SimpleDateFormat("MM月dd日 E HH:mm", Locale.getDefault())
        return dateFormat.format(calendar.time)
    }
}