package com.gh.base

import android.app.Activity
import android.app.Application
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.gh.common.util.FloatingBackViewManager
import com.gh.download.DownloadManager
import com.gh.gamecenter.MainActivity
import com.gh.gamecenter.SplashScreenActivity
import com.gh.gamecenter.common.utils.PackageFlavorHelper
import com.gh.gamecenter.forum.detail.ForumDetailActivity
import com.gh.gamecenter.forum.list.ForumListActivity
import com.gh.gamecenter.qa.article.detail.ArticleDetailActivity
import com.gh.gamecenter.qa.questions.newdetail.NewQuestionDetailActivity
import com.gh.gamecenter.qa.video.detail.ForumVideoDetailActivity
import com.gh.vspace.VHelper
import com.halo.assistant.HaloApp

// TODO：移动到对应的模块
class GlobalActivityLifecycleObserver : Application.ActivityLifecycleCallbacks {

    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        // do nothing
    }

    override fun onActivityStarted(activity: Activity) {
        GlobalActivityManager.currentActivity = activity
    }

    override fun onActivityResumed(activity: Activity) {
        // 判断是否需要显示或隐藏返回小浮窗
        if (FloatingBackViewManager.getType().isNotEmpty()) {
            if ((activity::class.java.name == "EnergyCenterActivity")
                && FloatingBackViewManager.getType() == FloatingBackViewManager.TYPE_TASK
            ) {
                FloatingBackViewManager.disableBackView()
            } else if (!shouldShowActivityBackView(activity)
                && FloatingBackViewManager.getType() == FloatingBackViewManager.TYPE_ACTIVITY
            ) {
                FloatingBackViewManager.disableBackView()
            } else {
                FloatingBackViewManager.showBackView(activity)
            }
        }

        if (HaloApp.isUserAcceptPrivacyPolicy(activity)) {
            // FIXME 这里应该只是部分Activity需要
            try {
                // 初始化gameMap
                if (activity !is SplashScreenActivity) {
                    DownloadManager.getInstance().initGameMap()
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        if (PackageFlavorHelper.IS_TEST_FLAVOR && activity is AppCompatActivity) {
            DarkModeSwitchHelper.showDarkModeSwitchFloatingView(activity)
        }

        if (activity is AppCompatActivity && activity !is SplashScreenActivity) {
            VHelper.showFeedbackDialogIfLastSuccessfulLaunchedGameExitUnexpectedly(activity)
        }
    }

    private fun shouldShowActivityBackView(activity: Activity): Boolean {
        return (activity is MainActivity
                || activity is ArticleDetailActivity
                || activity is ForumVideoDetailActivity
                || activity is ForumDetailActivity
                || activity is ForumListActivity
                || activity is NewQuestionDetailActivity)
    }

    override fun onActivityPaused(activity: Activity) {
        FloatingBackViewManager.dismissBackView()
        if (PackageFlavorHelper.IS_TEST_FLAVOR && activity is AppCompatActivity) {
            DarkModeSwitchHelper.dismissDarkModeSwitchFloatingView()
        }
    }

    override fun onActivityStopped(activity: Activity) {
        // do nothing
    }

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

    override fun onActivityDestroyed(activity: Activity) {
        // do nothing
    }

}