package com.base.scanqrclear.ui.start

import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.ViewModel
import com.base.scanqrclear.bean.ConstObject
import com.base.scanqrclear.bean.config.AdConfigBean
import com.base.scanqrclear.utils.LogEx
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class StartViewModel : ViewModel() {

    private val TAG = "StartViewModel"

    private var jumpJob: Job? = null

    private var loadingTime = AdConfigBean.adsConfigBean.openAdLoading

    var startJumpJob: Boolean = false


    /**
     * 超时跳转
     */
    fun startJumpJob(lifecycleCoroutineScope: LifecycleCoroutineScope, jumpNext: () -> Unit) {
        if (ConstObject.ifAgreePrivacy && startJumpJob) {
            if (jumpJob == null) {
                val startTime = System.currentTimeMillis()
                jumpJob = lifecycleCoroutineScope.launch {
                    LogEx.logDebug(TAG, "loadingTime=$loadingTime")
                    delay(loadingTime * 1000L)
                    val endTime = System.currentTimeMillis()
                    LogEx.logDebug(TAG, "超时跳转 time=${endTime - startTime}")
                    jumpNext.invoke()
                }
            }
        }

    }

    /**
     * 暂停超时跳转
     */
    fun cancelJumpJob() {
        jumpJob?.cancel()
        jumpJob = null
    }

}
