package rowedto.jockeyedto.sheetedto.atticedto.fadeedto
import android.os.Handler
import android.view.View
import androidx.core.view.isVisible
import com.base.datarecovery.databinding.YogaedtoBinding

class Continentaledto {


    private val binding: YogaedtoBinding

    private var mHandler: Handler

    private var mIsPaused = false
    private var mProgress = 0

    private val listener: ProgressListener

    constructor(binding: YogaedtoBinding, listener: ProgressListener) {
        this.binding = binding
        this.listener = listener
        mHandler = Handler()
        initView()
    }

    var loadTime: Int = 15

    private fun initView() {
        //loadTime = SPUtils.getInstance().getInt("loading_page_time", 15)
        binding.sockedto.max = loadTime
        binding.sockedto.progress = 0

    }

    fun startProgress() {
        binding.stuckedto.isVisible = false
        binding.offeredto.visibility = View.GONE
        binding.rainbowedto.visibility = View.VISIBLE
        val mRunnable: Runnable = object : Runnable {
            override fun run() {
                if (!mIsPaused) {
                    mProgress++ // 计算进度
                    binding.sockedto.progress = mProgress
                    if (mProgress < loadTime) {
                        mHandler.postDelayed(this, 1000) // 每秒钟更新一次进度
                    } else {
                        listener.onProgressMax()
                        pauseProgress()
                    }
                }
            }
        }
        mHandler.postDelayed(mRunnable, 1000)
    }

    fun pauseProgress() {
        if (!mIsPaused) {
            mIsPaused = true
            mHandler.removeCallbacksAndMessages(null)
        }
    }

    fun maxProgress() {
        binding.sockedto.progress = binding.sockedto.max
        listener.onProgressMax()
    }

    interface ProgressListener {
        fun onProgressMax()
    }
}