package com.zxhyis.toolss.ad


import android.text.TextUtils
import com.blankj.utilcode.util.AppUtils
import com.zxhyis.toolss.FileApp
import com.zxhyis.toolss.ad.bean.EventParams
import com.zxhyis.toolss.ad.bean.PushCfg
import com.zxhyis.toolss.common.notification.JinChengCheck
import com.zxhyis.toolss.logic.LogEx

/**
 * 推送的配置类，定义了是否推送的判断条件
 */
object PushManager {

    private val TAG = "PushManager"

    /**
     * 新用户是否显示
     */
    fun newUserPush(newUserAvoidTime: Int): Boolean {
        val firstLaunchTime = FileApp.fContext.queryDataStoreBlock(FIRST_LAUNCH_TIME_KEY, 0L)
        val flag = (System.currentTimeMillis() - firstLaunchTime) / 1000 > newUserAvoidTime
        return flag
    }

    /**
     * 所有推送是否推送大于间隔时间
     * @param interval 秒，目前默认自然和渠道用户都配的 60s
     */
    fun canPushNextTime(interval: Int): Boolean {
        val lastPushTime = FileApp.fContext.queryDataStoreBlock(ALL_LAST_PUSH_TIME, 0)
        return (System.currentTimeMillis() - lastPushTime) / 1000 > interval
    }

    /**
     * 所有的推送条件过滤
     * 根据id判断通知是否可推送
     * @param actionId 功能或者场景的id
     */
    fun isPush(actionId: Int, eventParams: EventParams? = null): Boolean {
        val pushManagement = ComUtils.getPushConfig()

        // 新用户是否显示
        val newUserAvoidFlag = newUserPush(pushManagement.newuser_avoid_time)
        val newUserLog = "新用户推送 newuser_avoid_time=${pushManagement.newuser_avoid_time} newUserFlag=$newUserAvoidFlag"
        LogEx.logDebug(TAG, newUserLog)
        if (!newUserAvoidFlag) {
            EventHelper.event("pushCircleOrder_isPush", newUserLog, eventParams = eventParams)
            return false
        }

        // 渠道用户是否推送
        if (!UserChancelEx.getUserChancelSwitch(pushManagement.push_show)) {
            LogEx.logDebug(TAG, "渠道用户不推送")
            EventHelper.event("pushCircleOrder_isPush", "渠道用户不推送", eventParams = eventParams)
            return false
        }

        //是否上次推送间隔大于配置间隔
        val isOganic = UserChancelEx.isOrganicUser()
        val interval = if (isOganic) pushManagement.o_push_interval else pushManagement.all_push_interval
        val intervalFlog = canPushNextTime(interval)
        val intervalLog = "推送总间隔推送 interval=$interval intervalFlog=$intervalFlog"
        LogEx.logDebug(TAG, intervalLog)
        if (!intervalFlog) {
            EventHelper.event("pushCircleOrder_isPush", intervalLog, eventParams = eventParams)
            return false
        }

        //当前类型通知推送间隔是否大于配置间隔
        val pushCfg: PushCfg? = ComUtils.getPushTypeData(actionId.toString())
        val pushInterval = pushCfg?.push_interval ?: 0
        val actionIdIntervalFlag = actionTypeCanPsh(actionId)
        val log = "当前通知类型间隔推送 actionId=$actionId pushInterval=$pushInterval actionIdIntervalFlag=$actionIdIntervalFlag"
        LogEx.logDebug(TAG, log)
        if (!actionIdIntervalFlag) {
            EventHelper.event("pushCircleOrder_isPush", log, eventParams = eventParams)
            return false
        }

        //是否在前台推送
        if (AppUtils.isAppForeground()) {
            LogEx.logDebug(TAG, "app在前台不推送")
            EventHelper.event("pushCircleOrder_isPush", "app在前台不推送", eventParams = eventParams)
            return false
        }


        //进程是否是活的
        if (!TextUtils.isEmpty(JinChengCheck.getAliveProcessName(FileApp.fContext))) {
            LogEx.logDebug(TAG, "app当前进程未拉起不推送")
            EventHelper.event("pushCircleOrder_isPush", "app当前进程未拉起不推送", eventParams = eventParams)
            return false
        }


        return true
    }

    /**
     * 当前类型通知推送间隔是否大于配置间隔
     */
    private fun actionTypeCanPsh(actionId: Int): Boolean {
        val pushCfg: PushCfg? = ComUtils.getPushTypeData(actionId.toString())
        val pushInterval = pushCfg?.push_interval ?: 0
        val lastTypePushTime = FileApp.fContext.queryDataStoreBlock(createByActionId(actionId), 0L)

        return (System.currentTimeMillis() - lastTypePushTime) / 1000 > pushInterval
    }

}