package com.renrenliao.im

import android.content.Context
import androidx.multidex.MultiDexApplication
import com.renrenliao.im.di.appModule
import com.renrenliao.im.im.IMClientManager
import com.renrenliao.im.utils.LogUtils
import com.renrenliao.im.utils.ntp.NtpSync
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.tencent.mmkv.MMKV
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidFileProperties
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import org.koin.core.logger.Level
import kotlin.properties.Delegates

/**
 **@ClassName MyApp
 **@Description TODO
 **@Author ganzhe
 **@Date 2022/5/20 11:51
 **/
class MyApp : MultiDexApplication() {


    companion object {

        var CONTEXT: Context by Delegates.notNull()

        //类似java静态代码块
        init {
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
                layout.setPrimaryColorsId(R.color.transparent, R.color.common_title_bar_slave) //全局设置主题颜色
                ClassicsHeader(context)
            }
        }
    }

    override fun onCreate() {
        super.onCreate()

        CONTEXT = applicationContext
        initMMKV()
        initKoin()

        //时间同步
        NtpSync.getInstance().initNtp(this)
        NtpSync.getInstance().start(true)

        //初始化IM
        IMClientManager.getInstance(this)

        //设置全局的Header构建器
//        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
//            layout.setPrimaryColorsId(R.color.transparent, R.color.white) //全局设置主题颜色
//            ClassicsHeader(context)
//        }
    }

    private fun initMMKV() {
        val rootDir = MMKV.initialize(this)
        LogUtils.info("mmkv root: $rootDir")
    }

    private fun initKoin() {
        startKoin {
            androidLogger(Level.DEBUG)
            androidContext(CONTEXT)
            androidFileProperties()
            modules(appModule)
        }
    }

}