package com.base.pdfviewerscannerwhite.ui.splash

import androidx.lifecycle.LifecycleCoroutineScope
import com.base.pdfviewerscannerwhite.ads.FirebaseDatabase.open_ad_loading
import com.base.pdfviewerscannerwhite.utils.AppPreferences
import com.base.pdfviewerscannerwhite.utils.LogEx
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SplashPresenter(
    val splashView: SplashView
) {
    private val TAG = "SplashPresenter"

    private var jumpJob: Job? = null

    var loadingTime = AppPreferences.getInstance().getString(open_ad_loading, "15").toInt()

    /**
     * 超时跳转
     */
    fun startJumpJob(lifecycleCoroutineScope: LifecycleCoroutineScope) {
        loadingTime = 30
        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}")
                splashView.jumpNext()
            }
        }
    }

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

}