package com.assimilate.alltrans

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Build
import android.webkit.WebView
import com.assimilate.alltrans.keepmodel.LanguagesConstants
import com.assimilate.alltrans.keepmodel.PreferenceLanguageUtils

class MyApp : Application() {
    init {
        instance = this
    }

    private fun setSystemLanguage() {
        // 检查是否是第一次进入应用
        if (PreferenceLanguageUtils.isFirstTime()) {
            // 第一次进入应用的逻辑
            val config = applicationContext().resources.configuration
            val locale = config.locale.toString()  // 获取完整的语言环境信息，如 "en_US" 或 "zh_CN"
            val languages = LanguagesConstants.getInstance().getLanguageByLanguageCode(locale, this)

            PreferenceLanguageUtils.putString(
                "language_target",
                languages.language.toString().trim()
            )
            // 设置已经不是第一次进入应用了
            PreferenceLanguageUtils.setNotFirstTime()
        }

    }

    companion object {
        private var instance: MyApp? = null

        fun applicationContext(): Context {
            return instance!!.applicationContext
        }
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        //  ScreenUtils.init(this)

        setSystemLanguage()

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val processName = getUniqueProcessName()
            if (processName != null && processName != packageName) {
                WebView.setDataDirectorySuffix(processName)
            }
        }
    }

    private fun getUniqueProcessName(): String? {
        val pid = android.os.Process.myPid()
        val manager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        for (processInfo in manager.runningAppProcesses) {
            if (processInfo.pid == pid) {
                return processInfo.processName
            }
        }
        return null
    }

    object Config {
        const val openSusViewMode: String = "open_sus_view"


        // firebase_event_key
        const val launchPv = "launch_pv"
        const val homePv = "home_pv"
        const val languageChoose = "language_choose"
        const val languageFrom = "language_from"
        const val textTransSwitch = "text_trans_switch"
        const val textInput = "text_input"
        const val textTransClick = "text_trans_click"
        const val textTransResult = "text_trans_result"
        const val textTransPaste = "text_trans_paste"

        const val textVoiceClick = "text_voice_click"
        const val textVoiceResult = "text_voice_result"
        const val textTransPlay = "text_trans_play"
        const val textTransCopy = "text_trans_copy"
        const val textTransShare = "text_trans_share"
        const val textTransLike = "text_trans_like"


        const val hoverButtonClick = "hover_button_click"
        const val hoverButtonCancel = "hover_button_cancel"
        const val hover_button_agree = "hover_button_agree"
        const val hover_screen_click = "hover_screen_click"
        const val hover_screen_imp = "hover_screen_imp"
        const val hover_screen_global = "hover_screen_global"
        const val hover_global_result = "hover_global_result"
        const val hover_screen_district = "hover_screen_district"
        const val hover_district_result = "hover_district_result"

        const val image_click = "image_click"
        const val image_trans_camera = "image_trans_camera"
        const val image_camera_result = "image_camera_result"
        const val image_trans_photo = "image_trans_photo"
        const val image_photo_result = "image_photo_result"

        const val history_click = "history_click"
        const val history_delete = "history_delete"


    }
}
