package com.zxhyis.toolss.ad


import android.os.Process
import com.zxhyis.toolss.FileApp
import com.zxhyis.toolss.ad.bean.EventParams
import com.zxhyis.toolss.ad.bean.PushCfg
import com.zxhyis.toolss.logic.LogEx
import com.zxhyis.toolss.notification.provider.MyContentProviderUtils

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

    private val TAG = "PushStrategy"

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

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

        //是否在前台推送
        val isResumed = MyContentProviderUtils.getMyIntValue(FileApp.fContext) == MyContentProviderUtils.RESUMED_VALUE
        LogEx.logDebug(TAG, "isResumed=$isResumed")
        if (isResumed) {
            LogEx.logDebug(TAG, "================================app在前台拦截===============================")
            EventHelper.event("pushCircleOrder_isPush", "app在前台拦截", eventParams = eventParams)
            return false
        }
        val pid = Process.myPid()
        LogEx.logDebug(TAG, "pid=$pid 可以发送通知了。。。")
        return true
    }

    /**
     * 当前类型通知推送间隔是否大于配置间隔
     */
    private fun actionTypeCanPsh(actionId: Int): Boolean {
        val pid = Process.myPid()
        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, "pid=$pid actionId=$actionId actionInterval=$actionInterval pushInterval=$pushInterval")
        return actionInterval > pushInterval
    }

}