package com.develop.food.base.widgets

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import com.develop.base.R
import com.develop.base.databinding.TimeShowViewBinding
import com.develop.food.base.manager.wifi.runOnMainThread
import com.develop.food.base.utils.TimerUtils

class ShowTimeView : RelativeLayout {

    private var binding: TimeShowViewBinding
    private var timerUtils: TimerUtils

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    init {
        val root = View.inflate(context, R.layout.time_show_view, this)
        binding = TimeShowViewBinding.bind(root)
        timerUtils = TimerUtils(context)
        timerUtils.startTicker(object : TimerUtils.OnTimeCountCallback {
            override fun onCount(
                timeStr: String,
                dateStr: String,
                weakStr: String,
                isNight: Boolean
            ) {
                runOnMainThread {
                    binding.timeClock.text = timeStr
                    binding.tvDate.text = dateStr
                    binding.tvWeak.text = weakStr
                    if (isNight) {
                        binding.ivDayNight.setBackgroundResource(R.drawable.ic_night)
                    } else {
                        binding.ivDayNight.setBackgroundResource(R.drawable.ic_day)
                    }
                }
            }
        })
    }
    fun stopTime(){
        timerUtils.stopTicker()
    }
}