package com.gh.common

import com.gh.common.exposure.ExposureManager
import com.gh.common.filter.RegionSettingHelper
import com.gh.common.videolog.VideoRecordUtils
import com.gh.download.DownloadDataHelper
import com.gh.gamecenter.common.loghub.LoghubUtils
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.common.utils.tryCatchInRelease
import com.gh.gamecenter.core.runOnUiThread
import com.gh.gamecenter.entity.TimeEntity
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import com.lightgame.utils.Utils
import io.reactivex.schedulers.Schedulers
import kotlin.concurrent.fixedRateTimer

object FixedRateJobHelper {
    private const val CHECKER_PERIOD: Long = 15 * 1000L
    private const val TIME_PERIOD: Long = 24 * 60 * 60 * 1000L
    private const val LOGHUB_PERIOD: Long = 2 * 60 * 1000L
    private const val EXPOSURE_PERIOD: Long = 1 * 60 * 1000L
    private const val REGION_SETTING_PERIOD: Long = 60 * 1000L
    private const val VIDEO_RECORD_PERIOD: Long = 60 * 1000L

    private const val DOWNLOAD_HEARTBEAT_PERIOD: Long = 60 * 1000L
    private const val DOWNLOAD_HEARTBEAT_SHEET_PERIOD: Long = 15 * 1000L

    private var mExecuteCount: Int = 0

    var timeDeltaBetweenServerAndClient: Long = 0

    @JvmStatic
    fun begin() {
        // 时间检查，每15秒检查一次
        fixedRateTimer("Global-Fixed-Rate-Timer", initialDelay = 100, period = CHECKER_PERIOD) {
            val elapsedTime = mExecuteCount * CHECKER_PERIOD
            // 时间校对，24 小时一次
            if (elapsedTime % TIME_PERIOD == 0L) {
                RetrofitManager.getInstance().api.time
                    .subscribeOn(Schedulers.io())
                    .subscribe(object : Response<TimeEntity>() {
                        override fun onResponse(response: TimeEntity?) {
                            val serverTime = response?.time
                            serverTime?.let {
                                timeDeltaBetweenServerAndClient = it * 1000 - System.currentTimeMillis()
                            }
                        }
                    })
            }

            // 提交曝光数据
            if (elapsedTime % EXPOSURE_PERIOD == 0L) {
                ExposureManager.commitSavedExposureEvents(true)
            }

            // 分片检测下载进度
            if (elapsedTime % DOWNLOAD_HEARTBEAT_SHEET_PERIOD == 0L) {
                tryCatchInRelease {
                    val upload = (mExecuteCount * CHECKER_PERIOD) % DOWNLOAD_HEARTBEAT_PERIOD == 0L
                    DownloadDataHelper.uploadDownloadHeartbeat(upload)
                }
            }

            // 提交普通 loghub 数据
            if (elapsedTime % LOGHUB_PERIOD == 0L) {
                runOnUiThread {
                    LoghubUtils.commitSavedLoghubEvents(true)
                }
            }

            // 更新游戏屏蔽信息
            if (elapsedTime % REGION_SETTING_PERIOD == 0L) {
                if (HaloApp.getInstance().isRunningForeground) {
                    RegionSettingHelper.getRegionSetting()
                }
            }

            // 提交视频浏览记录数据
            if (elapsedTime % VIDEO_RECORD_PERIOD == 0L) {
                VideoRecordUtils.commitVideoRecord()
            }

            mExecuteCount++
        }
    }
}