package com.base.browserwhite.fcm

import android.util.Log
import com.base.browserwhite.bean.ConstObject
import com.base.browserwhite.utils.AppPreferences
import com.base.browserwhite.utils.LogEx
import com.base.browserwhite.utils.TimeUtils
import com.base.browserwhite.utils.TimeUtils.formatTimeH

object NotificationHelp {


    private var currentNotificationIdIndex = -1


    fun getNotificationId(): Int {
        var id = 0
        NOTIFICATION_IDS.forEach {
            val boolean = canPushNotification(it)
            if (boolean) {
                id = it
                Log.d("glc", "forEach" + it)
                return id
            }
        }
        return id
    }

    /**
     * 是否可以推送判断
     */
    fun canPushNotification(actionId: Int): Boolean {
        //默认可以推送
        var canPush: Boolean = true
        when (actionId) {
            ConstObject.ID_JUNK_CLEANER -> {
                canPush = canPushJunkClean()
            }

            ConstObject.ID_CLEAN_JUNK_MEMORY -> {

            }

            ConstObject.ID_NEWS -> {
                canPush = canPushNews()
            }

            ConstObject.ID_WEATHER -> {
                canPush = canPushWeather()
            }

            ConstObject.ID_APP_PROCESS -> {
                canPush = canPushAppProcess()
            }

        }
        return canPush
    }


    //天气
    private fun canPushWeather(): Boolean {
        val lastPushTime = AppPreferences.getInstance().getLong("last_notification_time", 0)
        return lastPushTime == 0L || System.currentTimeMillis() - lastPushTime > 3 * 60 * 60 * 1000
    }

    //清理
    private fun canPushJunkClean(): Boolean {
        //last push
        val lastPushTime = AppPreferences.getInstance().getLong("last_junk_notification_time", 0)
        val flag1 =
            lastPushTime == 0L || System.currentTimeMillis() - lastPushTime > 24 * 60 * 60 * 1000

        //last use
        val lastUseJunkTime = AppPreferences.getInstance().getLong("last_use_junk_clean", 0)
        val flag2 =
            lastUseJunkTime == 0L || System.currentTimeMillis() - lastUseJunkTime > 24 * 60 * 60 * 1000
        return flag1 && flag2
    }

    //新闻
    private fun canPushNews(): Boolean {
        val time = System.currentTimeMillis().formatTimeH()
        val lashPushTime =
            AppPreferences.getInstance().getLong("last_news_notification_time", 0).formatTimeH()
        return TimeUtils.isTimeBetweenEightAndTenPM() && time != lashPushTime

    }

    //app进程
    private fun canPushAppProcess(): Boolean {
        val lastPushTime =
            AppPreferences.getInstance().getLong("last_process_notification_time", 0)
        val flag1 =
            lastPushTime == 0L || System.currentTimeMillis() - lastPushTime > 24 * 60 * 60 * 1000

        val lastUseTime = AppPreferences.getInstance().getLong("last_process_use_time", 0)
        val flag2 =
            lastUseTime == 0L || System.currentTimeMillis() - lastUseTime > 24 * 60 * 60 * 1000
        return flag1 && flag2

    }


    private fun getNextNotificationId(): Int {
        // 将当前通知 ID 索引加 1
        currentNotificationIdIndex++

        // 如果当前通知 ID 索引超出列表范围，则将其重置为 0
        if (currentNotificationIdIndex >= NOTIFICATION_IDS.size) {
            currentNotificationIdIndex = 0
        }

        // 返回下一个通知 ID
        return NOTIFICATION_IDS[currentNotificationIdIndex]
    }

    private val NOTIFICATION_IDS = intArrayOf(
        ConstObject.ID_JUNK_CLEANER,
        ConstObject.ID_CLEAN_JUNK_MEMORY,
        ConstObject.ID_NEWS,
        ConstObject.ID_APP_PROCESS,
        ConstObject.ID_WEATHER
    )
}
