package com.ym.klccy;


import android.app.Activity
import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.ym.admodule.config.AdManager
import com.ym.modulecommon.base.BaseApplication
import com.ym.modulecommon.base.SplashResumeActivity
import com.ym.modulecommon.net.NetConfig
import com.ym.modulecommon.utils.LogUtils

class BaseApplication : BaseApplication() {
    // 正常状态
    val STATE_NORMAL = 0

    // 从后台回到前台
    val STATE_BACK_TO_FRONT = 1

    // 从前台进入后台
    val STATE_FRONT_TO_BACK = 2

    // APP状态
    private var sAppState = STATE_NORMAL

    // 标记程序是否已进入后台(依据onStop回调)
    private var flag = false

    // 标记程序是否已进入后台(依据onTrimMemory回调)
    private var background = false

    // 从前台进入后台的时间
    private var frontToBackTime: Long = 0

    // 从后台返回前台的时间
    private var backToFrontTime: Long = 0

    override fun onCreate() {
        super.onCreate()
        //第一步初始化 application
        NetConfig.setBase_Url(NetConfig.Environment.DEV)
        AdManager.init(this,"b59e7d03c0f91730")
        registerActivityLifecycleCallbacks(activityLifecycleCallbacks)
        LogUtils.getConfig().logSwitch = true
    }


    override fun onTrimMemory(level: Int) {
        super.onTrimMemory(level)
        // TRIM_MEMORY_UI_HIDDEN是UI不可见的回调, 通常程序进入后台后都会触发此回调,大部分手机多是回调这个参数
        // TRIM_MEMORY_BACKGROUND也是程序进入后台的回调, 不同厂商不太一样, 魅族手机就是回调这个参数
        if (level == Application.TRIM_MEMORY_UI_HIDDEN || level == TRIM_MEMORY_BACKGROUND) {
            background = true
        } else if (level == Application.TRIM_MEMORY_COMPLETE) {
            background = !isCurAppTop(this)
        }
        if (background) {
            frontToBackTime = System.currentTimeMillis()
            sAppState = STATE_FRONT_TO_BACK
        } else {
            sAppState = STATE_NORMAL
        }
    }

    /**
     * 当前Activity个数
     */
    private var activityCount = 0
    var activityLifecycleCallbacks: Application.ActivityLifecycleCallbacks =
        object : Application.ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
            override fun onActivityStarted(activity: Activity) {
                if (background || flag) {
                    background = false
                    flag = false
                    sAppState = STATE_BACK_TO_FRONT
                    backToFrontTime = System.currentTimeMillis()
                    if (canShowAd()) {
////                ShowADActivity.show(activity);
                        val intent = Intent(applicationContext, SplashResumeActivity::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        startActivity(intent)
                    }
                } else {
                    sAppState = STATE_NORMAL
                }
            }
            override fun onActivityResumed(activity: Activity) {}
            override fun onActivityPaused(activity: Activity) {}
            override fun onActivityStopped(activity: Activity) {
                if (!isCurAppTop(activity)) {
                    // 从前台进入后台
                    sAppState = STATE_FRONT_TO_BACK
                    frontToBackTime = System.currentTimeMillis()
                    flag = true
                } else {
                    // 否则是正常状态
                    sAppState = STATE_NORMAL
                }
            }
            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
            override fun onActivityDestroyed(activity: Activity) {}
        }

    fun canShowAd(): Boolean {
        return sAppState == STATE_BACK_TO_FRONT &&
                backToFrontTime - frontToBackTime > 10000
    }

    fun isCurAppTop(context: Context?): Boolean {
        if (context == null) {
            return false
        }
        val curPackageName = context.packageName
        val am =
            context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val list =
            am.getRunningTasks(1)
        if (list != null && list.size > 0) {
            val info = list[0]
            val topPackageName = info.topActivity!!.packageName
            val basePackageName = info.baseActivity!!.packageName
            if (topPackageName == curPackageName && basePackageName == curPackageName) {
                return true
            }
        }
        return false
    }
}