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 haveFirstLaunchTime = (System.currentTimeMillis() - firstLaunchTime) / 1000
        LogEx.logDebug(TAG, "haveFirstLaunchTime=$haveFirstLaunchTime newUserAvoidTime=$newUserAvoidTime")
        val flag = haveFirstLaunchTime > newUserAvoidTime
        return flag
    }

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

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

        // 新用户是否显示
        if (!newUserPush(pushManagement.newuser_avoid_time)) {
            LogEx.logDebug(TAG, "================================新用户推送拦截===============================")
            EventHelper.event("pushCircleOrder_isPush", "新用户推送拦截", 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
        if (!canPushNextTime(interval)) {
            LogEx.logDebug(TAG, "================================总间隔拦截===============================")
            return false
        }

        //当前类型通知推送间隔是否大于配置间隔
        if (!actionTypeCanPsh(actionId)) {
            LogEx.logDebug(TAG, "================================当前通知类型间隔拦截===============================")
            EventHelper.event("pushCircleOrder_isPush", "当前通知类型间隔拦截", 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)
        val actionInterval = (System.currentTimeMillis() - lastTypePushTime) / 1000
        LogEx.logDebug(TAG, "actionId=$actionId actionInterval=$actionInterval pushInterval=$pushInterval")
        return actionInterval > pushInterval
    }

}