package com.base.locationsharewhite.helper

import android.app.Activity
import android.app.Application
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.RemoteViews
import com.base.locationsharewhite.R
import com.base.locationsharewhite.bean.ConstObject.topic_number
import com.base.locationsharewhite.ui.main.MainActivity
import com.base.locationsharewhite.ui.splash.SplashActivity
import com.base.locationsharewhite.utils.AppPreferences
import com.base.locationsharewhite.utils.KotlinExt.toFormatMinute
import com.base.locationsharewhite.utils.LogEx
import com.example.mydemo.strategy.fcm.MsgConfig
import com.example.mydemo.strategy.notification.NotificationBean
import java.util.Locale
import java.util.UUID


class MyApplication : Application() {
    private val TAG = "MyApplication"
    var uuid = ""

    companion object {
        lateinit var context: MyApplication


        var splashLanguage: String = Locale.getDefault().language + "_" + Locale.getDefault().country
        var mainLanguage: String = Locale.getDefault().language + "_" + Locale.getDefault().country
        var setLanguage: String = Locale.getDefault().language + "_" + Locale.getDefault().country
        var settingsLanguage: String = Locale.getDefault().language + "_" + Locale.getDefault().country
        var howUseLanguage: String = Locale.getDefault().language + "_" + Locale.getDefault().country
        var renameLanguage: String = Locale.getDefault().language + "_" + Locale.getDefault().country
        var locationMapLanguage: String = Locale.getDefault().language + "_" + Locale.getDefault().country

        var umpCanAd: Boolean = true

        @JvmField
        var PAUSED_VALUE = 0
    }


    override fun onCreate() {
        super.onCreate()
        context = this
        initUUid()
        initApp()
    }

    private fun initUUid() {
        uuid = AppPreferences.getInstance().getString("uuid", "")
        if (TextUtils.isEmpty(uuid)) {
            uuid = UUID.randomUUID().toString() + System.currentTimeMillis()
            AppPreferences.getInstance().put("uuid", uuid)
            LogEx.logDebug(TAG, "uuid=$uuid")
        }
        LogEx.logDebug(TAG, "uuid=${AppPreferences.getInstance().getString("uuid", "")}")
    }

    fun initApp() {
//        FacebookSdk.sdkInitialize(applicationContext)
        var topicNumber = System.currentTimeMillis().toFormatMinute()
        LogEx.logDebug(TAG, "topicNumber=$topicNumber")
        if (topic_number.isNotEmpty()) {
            topicNumber = topic_number
        } else {
            topic_number = topicNumber
        }
        val topic = ConfigHelper.packageName + "_push_$topicNumber"
        LogEx.logDebug(TAG, "topic=${topic}")
//        FCMManager.initFirebase(this)
//        FCMManager.subscribeToTopic(topic)
//        InstallHelps.init()
        initLifeListener()
//        ScreenStatusReceiver.setupScreenStatusListener(this)

        initAdSdk()
    }


    private var lastTimePause = 0L
    private var lastTimeResume = 0L
    private fun isHotLaunch(): Boolean {
        if ((lastTimeResume - lastTimePause) > 5000) {
            return true
        }
        return false
    }

    private fun initLifeListener() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            private var count = 0
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                ActivityManagerUtils.getInstance().addActivity(activity)
            }

            override fun onActivityStarted(activity: Activity) {
                count++
                lastTimeResume = System.currentTimeMillis()
                if (count == 1 && isHotLaunch()) {

                    val topActivity: Activity? = ActivityManagerUtils.getInstance().topActivity

                    val flag = if (topActivity == null) {
                        true
                    } else {
                        ConfigHelper.noLoadingActivities.all { !topActivity.localClassName.contains(it, true) }
                    }

                    LogEx.logDebug(TAG, "flag=$flag" + "   activity:" + activity.localClassName)

                    if (flag) {
                        topActivity?.startActivity(
                            Intent(
                                topActivity, SplashActivity::class.java
                            ).apply {
                                putExtra("isHotLaunch", true)
                                putExtra("type", -1)
                            })
                    }
                }
                lastTimeResume = 0
            }

            override fun onActivityResumed(activity: Activity) {
//                LogEx.logDebug(TAG, "onActivityResumed")
                PAUSED_VALUE = 1
            }


            override fun onActivityPaused(activity: Activity) {
//                LogEx.logDebug(TAG, "onActivityPaused")
                PAUSED_VALUE = 2
                lastTimePause = System.currentTimeMillis()
            }

            override fun onActivityStopped(activity: Activity) {
                count--
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}

            override fun onActivityDestroyed(activity: Activity) {
                ActivityManagerUtils.getInstance().removeActivity(activity)
            }
        })
    }

    fun initAdSdk() {
        GoogleSdkMgr.init(
            this, this.packageName, MsgConfig.Builder()
                //设置自己的自定义通知栏
//            .addRemoteBean(
//                //自定义的通知事件id，可以自己添加
//                ConstConfig.ACTION_0, NotificationBean(
//                    Intent(this, MainActivity::class.java).apply {
//                        //悬停点击标记，用来进入当相关页面进行悬停关闭
//                        putExtra("hover", true)
//                    },
//                    RemoteViews(packageName, R.layout.notification_message).apply {
//                        setTextViewText(R.id.tv_tittle, "大标题0")
//                        setTextViewText(R.id.tv_desc, "大内容0")
//                    },
//                    RemoteViews(packageName, R.layout.notification_message).apply {
//                        setTextViewText(R.id.tv_tittle, "小标题0")
//                        setTextViewText(R.id.tv_desc, "小内容0")
//                    }
//                )
//            )
//            .addRemoteBean(
//                ConstConfig.ACTION_1, NotificationBean(
//                    Intent(this, MainActivity::class.java).apply {
//                        putExtra("hover", true)
//                    },
//                    RemoteViews(packageName, R.layout.notification_message).apply {
//                        setTextViewText(R.id.tv_tittle, "大标题1")
//                        setTextViewText(R.id.tv_desc, "大内容1")
//                    },
//                    RemoteViews(packageName, R.layout.notification_message).apply {
//                        setTextViewText(R.id.tv_tittle, "小标题1")
//                        setTextViewText(R.id.tv_desc, "小内容1")
//                    }
//                )
//            )
//            .addRemoteBean(
//                ConstConfig.ACTION_2, NotificationBean(
//                    Intent(this, MainActivity::class.java).apply {
//                        putExtra("hover", true)
//                    },
//                    RemoteViews(packageName, R.layout.notification_junk_big).apply {
//                        setTextViewText(R.id.tv_size, "大标题2")
//                        setTextViewText(R.id.tv_desc, "大内容2")
//                    },
//                    RemoteViews(packageName, R.layout.notification_junk_small).apply {
//                        setTextViewText(R.id.tv_size, "小标题2")
//                        setTextViewText(R.id.tv_desc, "小内容2")
//                    }
//                )
//            )
                .build()
        )
    }

}