package com.test.basd.onecleanmaster.display

import com.blankj.utilcode.util.SPUtils
import com.test.basd.onecleanmaster.MyApplication
import com.test.basd.onecleanmaster.bean.ConfigBean
import com.test.basd.onecleanmaster.bean.ConfigBean.Companion.getActionPushInterval
import com.test.basd.onecleanmaster.helps.ComUtils
import com.test.basd.onecleanmaster.helps.LogEx


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

    private val TAG = "PushStrategy"

    /**
     * 新用户是否显示
     */
    fun newUserPush(newUserAvoidTime: Int): Boolean {
        val firstLaunchTime = SPUtils.getInstance().getLong("install_info", 0)
        val haveFirstLaunchTime = (System.currentTimeMillis() - firstLaunchTime) / 1000
        val flag = haveFirstLaunchTime > newUserAvoidTime
        return flag
    }

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

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

        // 新用户是否显示
        if (!newUserPush(configBean.newuser_avoid_time)) {
            LogEx.logDebug(TAG, "===============新用户推送拦截 actionId=$actionId=================")
            return false
        }

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

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

        //当前类型通知推送间隔是否大于配置间隔
        val pushInterval = configBean.getActionPushInterval(actionId)
        if (!actionTypeCanPsh(actionId, pushInterval)) {
            LogEx.logDebug(
                TAG, "============当前通知类型间隔拦截 actionId=$actionId=============="
            )
            return false
        }

        //是否在前台推送
        val isResumed = MyApplication.PAUSED_VALUE == 1
        LogEx.logDebug(TAG, "isResumed=$isResumed")
        if (isResumed) {
            LogEx.logDebug(TAG, "============app在前台拦截 actionId=$actionId=============")
            return false
        }
        return true
    }

    /**
     * 当前类型通知推送间隔是否大于配置间隔
     */
    private fun actionTypeCanPsh(actionId: Int, pushInterval: Int = 0): Boolean {
        val lastTypePushTime = SPUtils.getInstance().getLong(actionId.toString(), 0L)
        val actionInterval = (System.currentTimeMillis() - lastTypePushTime) / 1000
        LogEx.logDebug(TAG, "actionId=$actionId actionInterval=$actionInterval pushInterval=$pushInterval")
        return actionInterval > pushInterval
    }

}