package com.base.datarecovery.activity.splash
import android.os.Handler
import android.view.View
import androidx.core.view.isVisible
import com.base.datarecovery.databinding.ActivitySplashBinding

class TaskManager {


    private val binding: ActivitySplashBinding

    private var mHandler: Handler

    private var mIsPaused = false
    private var mProgress = 0

    private val listener: ProgressListener

    constructor(binding: ActivitySplashBinding, 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.pb.max = loadTime
        binding.pb.progress = 0

    }

    fun startProgress() {
        binding.idLlTishi.isVisible=false
        binding.idTvStart.isVisible = false
        binding.llStart.visibility = View.GONE
        binding.llProgress.visibility = View.VISIBLE
        val mRunnable: Runnable = object : Runnable {
            override fun run() {
                if (!mIsPaused) {
                    mProgress++ // 计算进度
                    binding.pb.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.pb.progress = binding.pb.max
        listener.onProgressMax()
    }

    interface ProgressListener {
        fun onProgressMax()
    }
}