package com.wdkl.app.ncs.application

import android.content.Context
import android.content.res.Configuration
import com.enation.javashop.android.jrouter.JRouter
import com.wdkl.ncs.android.lib.base.BaseApplication
import com.enation.javashop.net.engine.config.NetEngineConfig
import com.enation.javashop.net.engine.plugin.exception.RestfulExceptionInterceptor
import com.enation.javashop.utils.base.config.BaseConfig
import com.wdkl.app.ncs.callingbed.helper.LocaleMangerUtils
//import com.enation.javashop.utils.base.config.BaseConfig
//import com.wdkl.app.ncs.callingbed.helper.AnrFcExceptionUtil
import com.wdkl.app.ncs.callingbed.helper.NetHelper
import com.wdkl.app.ncs.callingbed.helper.Utils
import com.wdkl.app.ncs.callingbed.helper.XCrashUtils
import com.wdkl.app.ncs.callingbed.settings.SettingConfig
import serialporttest.utils.SerialPortUtil

/**
 * @author LDD
 * @Date   2018/1/11 下午12:48
 * @From   com.enation.javashop.android.application
 * @Note   应用Application
 */
class Application : BaseApplication() {


    /**
     * @author LDD
     * @From   Application
     * @Date   2018/1/11 下午12:50
     * @Note   应用启动时调用
     */
    override fun onCreate() {
        super.onCreate()
        initRouter()
        initFrame()
        initLeaks()
    }

    /**
     * @author  LDD
     * @From    Application
     * @Date   2018/1/11 下午12:50
     * @Note   初始化路由
     * @return rx观察者
     */
    private fun initRouter() {
        JRouter.init(this)
        JRouter.openDebug()
        JRouter.openLog()
        JRouter.prepare().create("/welcome/launch").seek()
        JRouter.prepare().create("/home/launch").seek()
        //JRouter.prepare().create("/setting/launch").seek()
        //JRouter.prepare().create("/shop/launch").seek()
        JRouter.prepare().create("/extra/launch").seek()

        //JRouter.prepare().create("/hello/launch").seek()
        //5寸分机
        JRouter.prepare().create("/callingbed/launch").seek()
    }

    /**
     * @author  LDD
     * @From    Application
     * @Date   2018/1/11 下午12:50
     * @Note   初始化内存检测器
     * @return rx观察者
     */
    private fun initLeaks() {
//        if (JavaShopConfigCenter.INSTANCE.APP_DEV) {
//            LeakCanary.install(this)
//        }
    }

    /**
     * @author  LDD
     * @From    Application
     * @Date   2018/1/11 下午12:50
     * @Note   初始化内部框架
     * @return rx观察者
     */
    private fun initFrame() {
        //禁止滑动退出
        BaseConfig.getInstance().addActivity("AppUpdateActivity","CallingbedActivity")

        NetEngineConfig.init(baseContext)
                .openLogger()
                .addNetInterceptor(RestfulExceptionInterceptor())

        //init net
        NetHelper.getInstance().init(applicationContext)
        //open serial port
        SerialPortUtil.getInstance().openSerialPort()

        //anr catcher
        //AnrFcExceptionUtil.getInstance(this).initFCException()

        //xCrash catcher
        XCrashUtils().init(this)

        Utils.checkCameraSupport()
    }

    override fun attachBaseContext(base: Context?) {
        val languageId: Int = SettingConfig.getLanguageId(base)
        LocaleMangerUtils.setApplicationLanguageByIndex(base, languageId)
        super.attachBaseContext(base)
    }

    override fun onConfigurationChanged(newConfig: Configuration?) {
        super.onConfigurationChanged(newConfig)
        val languageId: Int = SettingConfig.getLanguageId(baseContext)
        LocaleMangerUtils.setApplicationLanguageByIndex(baseContext, languageId)
    }
}