package com.gh.gamecenter.common.utils

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationManagerCompat
import com.alibaba.android.arouter.launcher.ARouter
import com.gh.gamecenter.common.HaloApp
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.common.dialog.NotificationHintDialogFragment
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.core.utils.TimeUtils
import com.gh.gamecenter.common.entity.NotificationUgc
import com.gh.gamecenter.core.provider.IPackageUtilsProvider
import com.lightgame.utils.AppManager
import com.lightgame.utils.Utils

object NotificationHelper {

    @JvmStatic
    fun showNotificationHintDialog(ugc: NotificationUgc, callBack: ((isShow: Boolean) -> Unit)? = null) {
        val showedNewVersion = SPUtils.getInt(Constants.SP_SHOWED_NOTIFICATION_NEW_VERSION, 0)
        val packageUtilsConfig =
            ARouter.getInstance().build(RouteConsts.provider.packageUtils).navigation() as? IPackageUtilsProvider
        val currentVersion = packageUtilsConfig?.getGhVersionCode() ?: 0
        // 版本升级后重置数据
        if (currentVersion > showedNewVersion) {
            SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_LOGIN, false)
            SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_QUESTION, false)
            SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_ANSWER, false)
            SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_ARTICLE, false)
            SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_VIDEO, false)
            SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_RATING, false)
            SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_GIFT, false)
            SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_RESERVE_GAME, false)
            SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_FEEDBACK, false)
            SPUtils.setString(Constants.SP_IS_SHOWED_NOTIFICATION_TODAY, "")

            SPUtils.setInt(Constants.SP_SHOWED_NOTIFICATION_NEW_VERSION, currentVersion)
        }


        val isShowedToday = SPUtils.getString(Constants.SP_IS_SHOWED_NOTIFICATION_TODAY, "")
        // 每天最多只需提示一次
        if (isShowedToday == TimeUtils.getToday()) {
            callBack?.invoke(false)
            return
        }

        val isShowedLogin = SPUtils.getBoolean(Constants.SP_SHOWED_NOTIFICATION_LOGIN, false)
        val isShowedQuestion = SPUtils.getBoolean(Constants.SP_SHOWED_NOTIFICATION_QUESTION, false)
        val isShowedAnswer = SPUtils.getBoolean(Constants.SP_SHOWED_NOTIFICATION_ANSWER, false)
        val isShowedArticle = SPUtils.getBoolean(Constants.SP_SHOWED_NOTIFICATION_ARTICLE, false)
        val isShowedVideo = SPUtils.getBoolean(Constants.SP_SHOWED_NOTIFICATION_VIDEO, false)
        val isShowedRating = SPUtils.getBoolean(Constants.SP_SHOWED_NOTIFICATION_RATING, false)
        val isShowedGift = SPUtils.getBoolean(Constants.SP_SHOWED_NOTIFICATION_GIFT, false)
        val isShowedReserveGame = SPUtils.getBoolean(Constants.SP_SHOWED_NOTIFICATION_RESERVE_GAME, false)
        val isShowedFeedback = SPUtils.getBoolean(Constants.SP_SHOWED_NOTIFICATION_FEEDBACK, false)

        if (isShowedLogin && isShowedQuestion && isShowedAnswer && isShowedArticle && isShowedVideo && isShowedRating && isShowedGift && isShowedReserveGame && isShowedFeedback) {
            callBack?.invoke(false)
            return
        }

        when (ugc) {
            NotificationUgc.LOGIN -> {
                if (!isShowedLogin) {
                    show(AppManager.getInstance().recentActiveActivity as? AppCompatActivity, ugc, callBack)
                    SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_LOGIN, true)
                    // 设置今天的时间，表示今天已经触发过了
                    SPUtils.setString(Constants.SP_IS_SHOWED_NOTIFICATION_TODAY, TimeUtils.getToday())
                } else {
                    callBack?.invoke(false)
                }
            }
            NotificationUgc.QUESTION -> {
                if (!isShowedQuestion) {
                    show(AppManager.getInstance().recentActiveActivity as? AppCompatActivity, ugc, callBack)
                    SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_QUESTION, true)
                    SPUtils.setString(Constants.SP_IS_SHOWED_NOTIFICATION_TODAY, TimeUtils.getToday())
                } else {
                    callBack?.invoke(false)
                }
            }
            NotificationUgc.ANSWER -> {
                if (!isShowedAnswer) {
                    show(AppManager.getInstance().recentActiveActivity as? AppCompatActivity, ugc, callBack)
                    SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_ANSWER, true)
                    SPUtils.setString(Constants.SP_IS_SHOWED_NOTIFICATION_TODAY, TimeUtils.getToday())
                } else {
                    callBack?.invoke(false)
                }
            }
            NotificationUgc.ARTICLE -> {
                if (!isShowedArticle) {
                    show(AppManager.getInstance().recentActiveActivity as? AppCompatActivity, ugc, callBack)
                    SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_ARTICLE, true)
                    SPUtils.setString(Constants.SP_IS_SHOWED_NOTIFICATION_TODAY, TimeUtils.getToday())
                } else {
                    callBack?.invoke(false)
                }
            }
            NotificationUgc.VIDEO -> {
                if (!isShowedVideo) {
                    show(AppManager.getInstance().recentActiveActivity as? AppCompatActivity, ugc, callBack)
                    SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_VIDEO, true)
                    SPUtils.setString(Constants.SP_IS_SHOWED_NOTIFICATION_TODAY, TimeUtils.getToday())
                } else {
                    callBack?.invoke(false)
                }
            }
            NotificationUgc.RATING -> {
                if (!isShowedRating) {
                    show(AppManager.getInstance().recentActiveActivity as? AppCompatActivity, ugc, callBack)
                    SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_RATING, true)
                    SPUtils.setString(Constants.SP_IS_SHOWED_NOTIFICATION_TODAY, TimeUtils.getToday())
                } else {
                    callBack?.invoke(false)
                }
            }
            NotificationUgc.GIFT -> {
                if (!isShowedGift) {
                    show(AppManager.getInstance().recentActiveActivity as? AppCompatActivity, ugc, callBack)
                    SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_GIFT, true)
                    SPUtils.setString(Constants.SP_IS_SHOWED_NOTIFICATION_TODAY, TimeUtils.getToday())
                } else {
                    callBack?.invoke(false)
                }
            }
            NotificationUgc.RESERVE_GAME -> {
                if (!isShowedReserveGame) {
                    show(AppManager.getInstance().recentActiveActivity as? AppCompatActivity, ugc, callBack)
                    SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_RESERVE_GAME, true)
                    SPUtils.setString(Constants.SP_IS_SHOWED_NOTIFICATION_TODAY, TimeUtils.getToday())
                } else {
                    callBack?.invoke(false)
                }
            }
            NotificationUgc.FEEDBACK -> {
                if (!isShowedFeedback) {
                    show(AppManager.getInstance().recentActiveActivity as? AppCompatActivity, ugc, callBack)
                    SPUtils.setBoolean(Constants.SP_SHOWED_NOTIFICATION_FEEDBACK, true)
                    SPUtils.setString(Constants.SP_IS_SHOWED_NOTIFICATION_TODAY, TimeUtils.getToday())
                } else {
                    callBack?.invoke(false)
                }
            }
        }
    }

    @SuppressLint("CheckResult")
    fun show(activity: AppCompatActivity?, ugc: NotificationUgc, callBack: ((isShow: Boolean) -> Unit)? = null) {
        if (activity == null) return
        showEnableNotificationDialogIfItsDisabled(activity, ugc, callBack)
    }

    @JvmStatic
    fun showEnableNotificationDialogIfItsDisabled(
        activity: AppCompatActivity,
        ugc: NotificationUgc,
        callBack: ((isShow: Boolean) -> Unit)? = null
    ) {
        if (notificationIsEnable()) {
            Utils.log("notification is enable")
            callBack?.invoke(false)
        } else {
            callBack?.invoke(true)
            if (activity.supportFragmentManager.isStateSaved) return
            NotificationHintDialogFragment.getInstance(ugc).show(activity.supportFragmentManager, "notification")
        }
    }

    @JvmStatic
    fun notificationIsEnable(): Boolean {
        val manager = NotificationManagerCompat.from(HaloApp.getInstance())
        return manager.areNotificationsEnabled()
    }

}