package com.swiftcleaner.chovey.business
import android.app.Activity
import android.util.Log
import com.link.play.ad.LinkPlaySdk
import com.swiftcleaner.chovey.BuildConfig
import com.swiftcleaner.chovey.GlobalConfig
import com.swiftcleaner.chovey.util.SPUtils

object ConfigIntervalTimeUtils {
    // true 超过配置的时间
    fun isConfigIntervalPass(lastTime: Long): Boolean{
        val intervalLimit = SPUtils.getInstance().getInt(GlobalConfig.KEY_GET_CONFIG, 4)
        val interval = System.currentTimeMillis() - lastTime
        return interval >= intervalLimit * 60 * 60 * 1000
    }


    private const val KEY_LAST_LINK_PLAY_TIME = "key_last_link_play_time"
    private inline var updateLinkTime
        get() = SPUtils.getInstance().getLong(KEY_LAST_LINK_PLAY_TIME, 0L)
        set(value) = SPUtils.getInstance().putLong(KEY_LAST_LINK_PLAY_TIME, value)

    fun initLinkPlaySdk(activity: Activity){
        if (!ConfigIntervalTimeUtils.isConfigIntervalPass(updateLinkTime)){
            return
        }
        Log.e("ConfigInterval", "initLinkPlaySdk")
        //日志
//        LinkPlaySdk.isDebug(true)
        //接口配置
//        LinkPlaySdk.init(this,GlobalConfig.URL_API,GlobalConfig.PACKAGE_NAME,GlobalConfig.KEY_AES)
//        LinkPlaySdk.init(this,"https://feedapihk.zhangxinhulian.com","com.unamzo.pdfreader.badcc","1qd6f41dgpjsep9t")
        // 引流
        LinkPlaySdk.init(
            activity, if (BuildConfig.DEBUG) {
                "https://feedapihk.zhangxinhulian.com"
            } else {
                GlobalConfig.URL_API
            },  if (BuildConfig.DEBUG) {
                "com.unamzo.pdfreader.badcc"
            } else {
                GlobalConfig.PACKAGE_NAME
            }, GlobalConfig.KEY_AES
        ){
            if (it) updateLinkTime = System.currentTimeMillis()
        }
    }
}