package com.ez.ertc

import android.os.Looper
import android.util.Log
import androidx.multidex.MultiDexApplication
import com.ez.baselib.utils.LogUtil
import com.ez.ertc.meeting.ErtcEzmHelper
import com.ez.ertcengine.ERTCEngine
import com.ez.network.RestfulUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MyApp : MultiDexApplication() {

    companion object{
        var instance : MyApp? = null
        @JvmField
        var ertcEngine : ERTCEngine? = null
    }

    override fun onCreate() {
        super.onCreate()
        instance = MyApp()
        CrashHandler.getInstance().init(this)
        ErtcEzmHelper.getInstance().ertcInit(this)
        RestfulUtils.init(this)
        setErtcXlog()
    }

    fun setErtcXlog() {
        LogUtil.setErtcLogCallback { logLevel, tag, content ->
            if (Looper.getMainLooper() == Looper.myLooper()) {
                CoroutineScope(Dispatchers.IO).launch {
                    doXLog(logLevel, tag, content)
                }
            } else {
                doXLog(logLevel, tag, content)
            }
        }
    }

    private fun doXLog(logLevel: Int, tag: String, content: String) {
        when (logLevel) {
            0 -> Log.v(tag, content)
            1 -> Log.d(tag, content)
            2 -> Log.i(tag, content)
            3 -> Log.w(tag, content)
            4 -> Log.e(tag, content)
        }
    }
}