package com.base.scanqr.ui.start

import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.ViewModel
import com.base.scanqr.bean.ConstObject
import com.base.scanqr.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 = 5

    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
    }

}
