package com.jld.vod.base

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.preference.PreferenceManager
import android.util.Log
import com.facebook.drawee.backends.pipeline.Fresco
import com.jld.vod.model.bean.ContextBean
import com.jld.vod.ui.XBasicLibInit
import com.jld.vod.utils.http.RetrofitUtils
import com.xuexiang.xui.XUI
import com.zzhoujay.richtext.RichText
import org.mapsforge.map.android.graphics.AndroidGraphicFactory
import org.mapsforge.map.layer.renderer.MapWorkerPool
import org.mapsforge.map.model.DisplayModel
import org.mapsforge.map.reader.MapFile

/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2019/11/26 14:47
 */
class MyApplication : Application(){
    val TAG = "Mapsforge Samples"

    val SETTING_DEBUG_TIMING = "debug_timing"
    val SETTING_LANGUAGE_SHOWLOCAL = "language_showlocal"
    val SETTING_PREFERRED_LANGUAGE = "language_selection"
    val SETTING_RENDERING_THREADS = "rendering_threads"
    val SETTING_SCALE = "scale"
    val SETTING_TEXTWIDTH = "textwidth"
    val SETTING_TILECACHE_PERSISTENCE = "tilecache_persistence"
    val SETTING_WAYFILTERING = "wayfiltering"
    val SETTING_WAYFILTERING_DISTANCE = "wayfiltering_distance"
    private var callbacks:ActivityLifecycleCallbacks? = null

    /**
     * 内存检测
     */
    companion object{
        fun isDebug(): Boolean {
            return false
        }
        lateinit var app: Application
    }
    override fun onCreate() {
        super.onCreate()
        app = this
        RetrofitUtils.init()
        Fresco.initialize(app)
        /**
         * 地图
         */
        AndroidGraphicFactory.createInstance(this)
        Log.e(TAG, "Device scale factor " + java.lang.Float.toString(DisplayModel.getDeviceScaleFactor()))
        val preferences = PreferenceManager.getDefaultSharedPreferences(this)
        val fs = java.lang.Float.valueOf(preferences.getString(SETTING_SCALE, java.lang.Float.toString(DisplayModel.getDefaultUserScaleFactor()))!!)
        Log.e(TAG, "User ScaleFactor " + java.lang.Float.toString(fs))
        if (fs != DisplayModel.getDefaultUserScaleFactor()) { DisplayModel.setDefaultUserScaleFactor(fs) }

        MapFile.wayFilterEnabled = preferences.getBoolean(SETTING_WAYFILTERING, true)
        if (MapFile.wayFilterEnabled) {
            MapFile.wayFilterDistance = preferences.getString(SETTING_WAYFILTERING_DISTANCE, "20")!!.toInt()
        }
        MapWorkerPool.DEBUG_TIMING = preferences.getBoolean(SETTING_DEBUG_TIMING, false)
        //XUI
        initUI()
        //初始化基础库
        XBasicLibInit.init(this)
        //html
        RichText.initCacheDir(this)
        RichText.debugMode = true

        initActivityLifecycle()
    }

    private fun initActivityLifecycle() {
        app.registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks{
            override fun onActivityPaused(activity: Activity) {
            }
            override fun onActivityStarted(activity: Activity) {
            }

            override fun onActivityDestroyed(activity: Activity) {
                if (activity == ContextBean.getInstance().getActivity()) {
                    ContextBean.getInstance().setActivity(null)
                }
            }

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

            override fun onActivityStopped(activity: Activity) {
            }

            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                ContextBean.getInstance().setActivity(activity);
            }

            override fun onActivityResumed(activity: Activity) {
            }
        })

    }
    /**
     * 初始化XUI 框架
     */

    private fun initUI() {
        XUI.init(this)
        XUI.debug(isDebug())
    }
}