package com.base.locationsharewhite.ui.splash

import androidx.lifecycle.LifecycleCoroutineScope
import com.base.locationsharewhite.bean.ConstObject
import com.base.locationsharewhite.utils.LogEx
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SplashPresenter(
    val splashView: SplashView
) {

    private val TAG = "SplashActivity"

    private var jumpJob: Job? = null

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

    var startJumpJob: Boolean = false

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

    }

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

}